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