a36b5fe1f512de99ae9af0061b94fdab3c67fb7f
[tldk.git] / lib / libtle_l4p / tcp_stream.h
1 /*
2  * Copyright (c) 2016-2017  Intel Corporation.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #ifndef _TCP_STREAM_H_
17 #define _TCP_STREAM_H_
18
19 #include <rte_vect.h>
20 #include <tle_dring.h>
21 #include <tle_memtank.h>
22 #include <tle_tcp.h>
23 #include <tle_event.h>
24
25 #include "stream.h"
26 #include "misc.h"
27 #include "tcp_misc.h"
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 enum {
34         TCP_ST_CLOSED,
35         TCP_ST_LISTEN,
36         TCP_ST_SYN_SENT,
37         TCP_ST_SYN_RCVD,
38         TCP_ST_ESTABLISHED,
39         TCP_ST_FIN_WAIT_1,
40         TCP_ST_FIN_WAIT_2,
41         TCP_ST_CLOSE_WAIT,
42         TCP_ST_CLOSING,
43         TCP_ST_LAST_ACK,
44         TCP_ST_TIME_WAIT,
45         TCP_ST_NUM
46 };
47
48 enum {
49         TCP_OP_LISTEN =    0x1,
50         TCP_OP_ACCEPT =    0x2,
51         TCP_OP_CONNECT =   0x4,
52         TCP_OP_ESTABLISH = 0x8,
53         TCP_OP_CLOSE =     0x10,
54 };
55
56 struct tcb {
57         volatile uint16_t state;
58         volatile uint16_t uop; /* operations by user performed */
59         struct {
60                 uint32_t nxt;
61                 uint32_t irs; /* initial received sequence */
62                 uint32_t wnd;
63                 uint32_t ts;
64                 struct {
65                         uint32_t seq;
66                         uint32_t on;
67                 } frs;
68                 uint32_t srtt;   /* smoothed round trip time (scaled by >> 3) */
69                 uint32_t rttvar; /* rtt variance */
70                 uint16_t mss;
71                 uint8_t  wscale;
72                 uint8_t  dupack;
73         } rcv;
74         struct {
75                 uint64_t nxt;
76                 uint64_t una;
77                 uint64_t rcvr; /* recover RFC 6582 */
78                 uint64_t fss;  /* FIN sequence # */
79                 uint32_t fastack; /* # of partial acks in fast retransmit */
80                 uint32_t wnd;
81                 union wui wu; /* window update */
82                 uint32_t ack; /* last sent ack */
83                 uint32_t ts;
84                 uint32_t cwnd;     /* congestion window */
85                 uint32_t ssthresh; /* slow start threshold */
86                 uint32_t rto;      /* retransmission timeout */
87                 uint32_t rto_tw;   /* TIME_WAIT retransmission timeout */
88                 uint32_t iss;      /* initial send sequence */
89                 uint16_t mss;
90                 uint8_t  wscale;
91                 uint8_t nb_retx; /* number of retransmission */
92                 uint8_t nb_retm; /**< max number of retx attempts. */
93         } snd;
94         struct tle_tcp_syn_opts so; /* initial syn options. */
95 };
96
97 struct tle_tcp_stream {
98
99         struct tle_stream s;
100
101         uint32_t flags;
102         rte_atomic32_t use;
103
104         struct stbl_entry *ste;     /* entry in streams table. */
105         struct tcb tcb;
106
107         struct {
108                 void *handle;
109         } timer;
110
111         struct {
112                 struct tle_event *ev;
113                 struct tle_stream_cb cb;
114         } err;
115
116         struct {
117                 struct rte_ring *q;     /* listen (syn) queue */
118                 struct ofo *ofo;
119                 struct tle_event *ev;    /* user provided recv event. */
120                 struct tle_stream_cb cb; /* user provided recv callback. */
121         } rx __rte_cache_aligned;
122
123         struct {
124                 rte_atomic32_t arm;  /* when > 0 stream is in to-send queue */
125                 struct {
126                         uint32_t nb_elem;  /* number of objects per drb. */
127                         uint32_t nb_max;   /* number of drbs per stream. */
128                         struct rte_ring *r;
129                 } drb;
130                 struct rte_ring *q;  /* (re)tx queue */
131                 struct tle_event *ev;
132                 struct tle_stream_cb cb;
133                 struct tle_dest dst;
134         } tx __rte_cache_aligned;
135
136 } __rte_cache_aligned;
137
138 #define TCP_STREAM(p)   \
139 ((struct tle_tcp_stream *)((uintptr_t)(p) - offsetof(struct tle_tcp_stream, s)))
140
141 #define TCP_STREAM_TX_PENDING(s)        \
142         ((s)->tx.drb.nb_max != rte_ring_count((s)->tx.drb.r))
143
144 #define TCP_STREAM_TX_FINISHED(s)       \
145         ((s)->tx.drb.nb_max == rte_ring_count((s)->tx.drb.r))
146
147 #include "stream_table.h"
148
149 struct sdr {
150         rte_spinlock_t lock;
151         STAILQ_HEAD(, tle_stream) fe;
152         STAILQ_HEAD(, tle_stream) be;
153 };
154
155 /* tempalte sizes/offsets/etc. for tcp stream */
156 struct stream_szofs {
157         uint32_t size;
158         struct {
159                 uint32_t ofs;
160                 uint32_t nb_obj;
161                 uint32_t nb_max;
162         } ofo;
163         struct {
164                 uint32_t ofs;
165                 uint32_t nb_obj;
166         } rxq, txq;
167         struct {
168                 uint32_t ofs;
169                 uint32_t blk_sz;
170                 uint32_t rng_sz;
171                 uint32_t nb_rng;
172                 uint32_t nb_obj;
173                 uint32_t nb_max;
174         } drb;
175 };
176
177 struct tcp_streams {
178         struct stbl st;
179         struct tle_timer_wheel *tmr; /* timer wheel */
180         struct rte_ring *tsq;        /* to-send streams queue */
181         struct tle_memtank *mts;     /* memtank to allocate streams from */
182         struct sdr dr;               /* death row for zombie streams */
183         struct stream_szofs szofs;   /* size and offsets for stream data */
184 };
185
186 #define CTX_TCP_STREAMS(ctx)    ((struct tcp_streams *)(ctx)->streams.buf)
187 #define CTX_TCP_STLB(ctx)       (&CTX_TCP_STREAMS(ctx)->st)
188 #define CTX_TCP_TMWHL(ctx)      (CTX_TCP_STREAMS(ctx)->tmr)
189 #define CTX_TCP_TSQ(ctx)        (CTX_TCP_STREAMS(ctx)->tsq)
190 #define CTX_TCP_SDR(ctx)        (&CTX_TCP_STREAMS(ctx)->dr)
191 #define CTX_TCP_MTS(ctx)        (CTX_TCP_STREAMS(ctx)->mts)
192
193 extern int tcp_stream_fill_prm(struct tle_tcp_stream *s,
194         const struct tle_tcp_stream_param *prm);
195
196 #ifdef __cplusplus
197 }
198 #endif
199
200 #endif /* _TCP_STREAM_H_ */