lsquic_conn.h revision 4d83f5bd
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_handshake_ok) (struct lsquic_conn *);
77
78    void
79    (*ci_handshake_failed) (struct lsquic_conn *);
80
81    void
82    (*ci_destroy) (struct lsquic_conn *);
83
84    int
85    (*ci_is_tickable) (struct lsquic_conn *);
86
87    lsquic_time_t
88    (*ci_next_tick_time) (struct lsquic_conn *);
89
90    int
91    (*ci_can_write_ack) (struct lsquic_conn *);
92
93    /* No return status: best effort */
94    void
95    (*ci_write_ack) (struct lsquic_conn *, struct lsquic_packet_out *);
96
97#if LSQUIC_CONN_STATS
98    const struct conn_stats *
99    (*ci_get_stats) (struct lsquic_conn *);
100#endif
101};
102
103struct lsquic_conn
104{
105    void                        *cn_peer_ctx;
106    struct lsquic_enc_session   *cn_enc_session;
107    const struct enc_session_funcs
108                                *cn_esf;
109    lsquic_cid_t                 cn_cid;
110    STAILQ_ENTRY(lsquic_conn)    cn_next_closed_conn;
111    TAILQ_ENTRY(lsquic_conn)     cn_next_ticked;
112    TAILQ_ENTRY(lsquic_conn)     cn_next_out,
113                                 cn_next_hash;
114    const struct conn_iface     *cn_if;
115    const struct parse_funcs    *cn_pf;
116    struct attq_elem            *cn_attq_elem;
117    lsquic_time_t                cn_last_sent;
118    lsquic_time_t                cn_last_ticked;
119    enum lsquic_conn_flags       cn_flags;
120    enum lsquic_version          cn_version;
121    unsigned                     cn_hash;
122    unsigned short               cn_pack_size;
123    unsigned char                cn_local_addr[sizeof(struct sockaddr_in6)];
124    union {
125        unsigned char       buf[sizeof(struct sockaddr_in6)];
126        struct sockaddr     sa;
127    }                            cn_peer_addr_u;
128#define cn_peer_addr cn_peer_addr_u.buf
129};
130
131void
132lsquic_conn_record_sockaddr (lsquic_conn_t *lconn, const struct sockaddr *local,
133                                                  const struct sockaddr *peer);
134
135int
136lsquic_conn_decrypt_packet (lsquic_conn_t *lconn,
137                    struct lsquic_engine_public *, struct lsquic_packet_in *);
138
139int
140lsquic_conn_copy_and_release_pi_data (const lsquic_conn_t *conn,
141                    struct lsquic_engine_public *, struct lsquic_packet_in *);
142
143#define lsquic_conn_adv_time(c) ((c)->cn_attq_elem->ae_adv_time)
144
145#if LSQUIC_CONN_STATS
146struct conn_stats {
147    /* All counters are of the same type, unsigned long, because we cast the
148     * struct to an array to update the aggregate.
149     */
150    unsigned long           n_ticks;            /* How many time connection was ticked */
151    struct {
152        unsigned long       stream_data_sz;     /* Sum of all STREAM frames payload */
153        unsigned long       stream_frames;      /* Number of STREAM frames */
154        unsigned long       packets,            /* Incoming packets */
155                            undec_packets,      /* Undecryptable packets */
156                            dup_packets,        /* Duplicate packets */
157                            err_packets;        /* Error packets(?) */
158        unsigned long       n_acks,
159                            n_acks_proc,
160                            n_acks_merged[2];
161        unsigned long       bytes;              /* Overall bytes in */
162        unsigned long       headers_uncomp;     /* Sum of uncompressed header bytes */
163        unsigned long       headers_comp;       /* Sum of compressed header bytes */
164    }                   in;
165    struct {
166        unsigned long       stream_data_sz;
167        unsigned long       stream_frames;
168        unsigned long       acks;
169        unsigned long       packets;            /* Number of sent packets */
170        unsigned long       retx_packets;       /* Number of retransmitted packets */
171        unsigned long       bytes;              /* Overall bytes out */
172        unsigned long       headers_uncomp;     /* Sum of uncompressed header bytes */
173        unsigned long       headers_comp;       /* Sum of compressed header bytes */
174    }                   out;
175};
176#endif
177
178#endif
179