lsquic_conn.h revision 8ca33e0e
1/* Copyright (c) 2017 - 2019 LiteSpeed Technologies Inc.  See LICENSE. */
2/*
3 * lsquic_conn.h -- Connection interface
4 *
5 */
6#ifndef LSQUIC_CONN_H
7#define LSQUIC_CONN_H
8
9#include <sys/queue.h>
10#ifndef WIN32
11#include <sys/socket.h>
12#include <netinet/in.h>
13#else
14#include <ws2ipdef.h>
15#endif
16
17struct lsquic_conn;
18struct lsquic_enc_session;
19struct lsquic_engine_public;
20struct lsquic_packet_out;
21struct lsquic_packet_in;
22struct sockaddr;
23struct parse_funcs;
24struct attq_elem;
25#if LSQUIC_CONN_STATS
26struct conn_stats;
27#endif
28
29enum lsquic_conn_flags {
30    LSCONN_TICKED         = (1 << 0),
31    LSCONN_HAS_OUTGOING   = (1 << 1),
32    LSCONN_HASHED         = (1 << 2),
33    LSCONN_HAS_PEER_SA    = (1 << 4),
34    LSCONN_HAS_LOCAL_SA   = (1 << 5),
35    LSCONN_HANDSHAKE_DONE = (1 << 6),
36    LSCONN_CLOSING        = (1 << 7),
37    LSCONN_PEER_GOING_AWAY= (1 << 8),
38    LSCONN_TCID0          = (1 << 9),
39    LSCONN_VER_SET        = (1 <<10),   /* cn_version is set */
40    LSCONN_EVANESCENT     = (1 <<11),   /* evanescent connection */
41    LSCONN_TICKABLE       = (1 <<12),   /* Connection is in the Tickable Queue */
42    LSCONN_COI_ACTIVE     = (1 <<13),
43    LSCONN_COI_INACTIVE   = (1 <<14),
44    LSCONN_SEND_BLOCKED   = (1 <<15),   /* Send connection blocked frame */
45    LSCONN_NEVER_TICKABLE = (1 <<17),   /* Do not put onto the Tickable Queue */
46    LSCONN_ATTQ           = (1 <<19),
47};
48
49/* A connection may have things to send and be closed at the same time.
50 */
51enum tick_st {
52    TICK_SEND    = (1 << 0),
53    TICK_CLOSE   = (1 << 1),
54};
55
56#define TICK_QUIET 0
57
58struct conn_iface
59{
60    enum tick_st
61    (*ci_tick) (struct lsquic_conn *, lsquic_time_t now);
62
63    void
64    (*ci_packet_in) (struct lsquic_conn *, struct lsquic_packet_in *);
65
66    struct lsquic_packet_out *
67    (*ci_next_packet_to_send) (struct lsquic_conn *);
68
69    void
70    (*ci_packet_sent) (struct lsquic_conn *, struct lsquic_packet_out *);
71
72    void
73    (*ci_packet_not_sent) (struct lsquic_conn *, struct lsquic_packet_out *);
74
75    void
76    (*ci_hsk_done) (struct lsquic_conn *, enum lsquic_hsk_status);
77
78    void
79    (*ci_destroy) (struct lsquic_conn *);
80
81    int
82    (*ci_is_tickable) (struct lsquic_conn *);
83
84    lsquic_time_t
85    (*ci_next_tick_time) (struct lsquic_conn *);
86
87    int
88    (*ci_can_write_ack) (struct lsquic_conn *);
89
90    /* No return status: best effort */
91    void
92    (*ci_write_ack) (struct lsquic_conn *, struct lsquic_packet_out *);
93
94#if LSQUIC_CONN_STATS
95    const struct conn_stats *
96    (*ci_get_stats) (struct lsquic_conn *);
97#endif
98};
99
100struct lsquic_conn
101{
102    void                        *cn_peer_ctx;
103    struct lsquic_enc_session   *cn_enc_session;
104    const struct enc_session_funcs
105                                *cn_esf;
106    lsquic_cid_t                 cn_cid;
107    STAILQ_ENTRY(lsquic_conn)    cn_next_closed_conn;
108    TAILQ_ENTRY(lsquic_conn)     cn_next_ticked;
109    TAILQ_ENTRY(lsquic_conn)     cn_next_out,
110                                 cn_next_hash;
111    const struct conn_iface     *cn_if;
112    const struct parse_funcs    *cn_pf;
113    struct attq_elem            *cn_attq_elem;
114    lsquic_time_t                cn_last_sent;
115    lsquic_time_t                cn_last_ticked;
116    enum lsquic_conn_flags       cn_flags;
117    enum lsquic_version          cn_version;
118    unsigned                     cn_hash;
119    unsigned short               cn_pack_size;
120    unsigned char                cn_local_addr[sizeof(struct sockaddr_in6)];
121    union {
122        unsigned char       buf[sizeof(struct sockaddr_in6)];
123        struct sockaddr     sa;
124    }                            cn_peer_addr_u;
125#define cn_peer_addr cn_peer_addr_u.buf
126};
127
128void
129lsquic_conn_record_sockaddr (lsquic_conn_t *lconn, const struct sockaddr *local,
130                                                  const struct sockaddr *peer);
131
132int
133lsquic_conn_decrypt_packet (lsquic_conn_t *lconn,
134                    struct lsquic_engine_public *, struct lsquic_packet_in *);
135
136int
137lsquic_conn_copy_and_release_pi_data (const lsquic_conn_t *conn,
138                    struct lsquic_engine_public *, struct lsquic_packet_in *);
139
140#define lsquic_conn_adv_time(c) ((c)->cn_attq_elem->ae_adv_time)
141
142#if LSQUIC_CONN_STATS
143struct conn_stats {
144    /* All counters are of the same type, unsigned long, because we cast the
145     * struct to an array to update the aggregate.
146     */
147    unsigned long           n_ticks;            /* How many time connection was ticked */
148    struct {
149        unsigned long       stream_data_sz;     /* Sum of all STREAM frames payload */
150        unsigned long       stream_frames;      /* Number of STREAM frames */
151        unsigned long       packets,            /* Incoming packets */
152                            undec_packets,      /* Undecryptable packets */
153                            dup_packets,        /* Duplicate packets */
154                            err_packets;        /* Error packets(?) */
155        unsigned long       n_acks,
156                            n_acks_proc,
157                            n_acks_merged[2];
158        unsigned long       bytes;              /* Overall bytes in */
159        unsigned long       headers_uncomp;     /* Sum of uncompressed header bytes */
160        unsigned long       headers_comp;       /* Sum of compressed header bytes */
161    }                   in;
162    struct {
163        unsigned long       stream_data_sz;
164        unsigned long       stream_frames;
165        unsigned long       acks;
166        unsigned long       packets;            /* Number of sent packets */
167        unsigned long       retx_packets;       /* Number of retransmitted packets */
168        unsigned long       bytes;              /* Overall bytes out */
169        unsigned long       headers_uncomp;     /* Sum of uncompressed header bytes */
170        unsigned long       headers_comp;       /* Sum of compressed header bytes */
171    }                   out;
172};
173#endif
174
175#endif
176