l4p/tcp: Add ability to adjust/offset tcp timestamps when proxying
[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 struct tcb {
34         volatile uint16_t state;
35         volatile uint16_t uop; /* operations by user performed */
36         struct {
37                 uint32_t nxt;
38                 uint32_t irs; /* initial received sequence */
39                 uint32_t wnd;
40                 uint32_t ts;
41                 struct {
42                         uint32_t seq;
43                         uint32_t on;
44                 } frs;
45                 uint32_t srtt;   /* smoothed round trip time (scaled by >> 3) */
46                 uint32_t rttvar; /* rtt variance */
47                 uint16_t mss;
48                 uint8_t  wscale;
49                 uint8_t  dupack;
50         } rcv;
51         struct {
52                 uint64_t nxt;
53                 uint64_t una;
54                 uint64_t rcvr; /* recover RFC 6582 */
55                 uint64_t fss;  /* FIN sequence # */
56                 uint32_t fastack; /* # of partial acks in fast retransmit */
57                 uint32_t wnd;
58                 union wui wu; /* window update */
59                 uint32_t ack; /* last sent ack */
60                 uint32_t ts;
61                 uint32_t cwnd;     /* congestion window */
62                 uint32_t ssthresh; /* slow start threshold */
63                 uint32_t rto;      /* retransmission timeout */
64                 uint32_t rto_tw;   /* TIME_WAIT retransmission timeout */
65                 uint32_t iss;      /* initial send sequence */
66                 uint16_t mss;
67                 uint8_t  wscale;
68                 uint8_t nb_retx; /* number of retransmission */
69                 uint8_t nb_retm; /**< max number of retx attempts. */
70                 uint8_t close_flags; /* tcp flags to send on close */
71         } snd;
72         struct tle_tcp_syn_opts so; /* initial syn options. */
73 };
74
75 struct tle_tcp_stream {
76
77         struct tle_stream s;
78
79         uint32_t flags;
80         rte_atomic32_t use;
81
82         struct stbl_entry *ste;     /* entry in streams table. */
83         struct tcb tcb;
84
85         int32_t ts_offset; /* TS.VAL offset from TSC calculated */
86
87         struct {
88                 void *handle;
89         } timer;
90
91         struct {
92                 struct tle_event *ev;
93                 struct tle_stream_cb cb;
94                 /*bitmask of remote events */
95                 uint16_t rev;
96         } err;
97
98         struct {
99                 struct rte_ring *q;     /* listen (syn) queue */
100                 struct ofo *ofo;
101                 struct tle_event *ev;    /* user provided recv event. */
102                 struct tle_stream_cb cb; /* user provided recv callback. */
103         } rx __rte_cache_aligned;
104
105         struct {
106                 rte_atomic32_t arm;  /* when > 0 stream is in to-send queue */
107                 struct {
108                         uint32_t nb_elem;  /* number of objects per drb. */
109                         uint32_t nb_max;   /* number of drbs per stream. */
110                         struct rte_ring *r;
111                 } drb;
112                 struct rte_ring *q;  /* (re)tx queue */
113                 struct tle_event *ev;
114                 struct tle_stream_cb cb;
115                 struct tle_dest dst;
116         } tx __rte_cache_aligned;
117
118 } __rte_cache_aligned;
119
120 #define TCP_STREAM(p)   \
121 ((struct tle_tcp_stream *)((uintptr_t)(p) - offsetof(struct tle_tcp_stream, s)))
122
123 #define TCP_STREAM_TX_PENDING(s)        \
124         ((s)->tx.drb.nb_max != rte_ring_count((s)->tx.drb.r))
125
126 #define TCP_STREAM_TX_FINISHED(s)       \
127         ((s)->tx.drb.nb_max == rte_ring_count((s)->tx.drb.r))
128
129 #include "stream_table.h"
130
131 struct sdr {
132         rte_spinlock_t lock;
133         STAILQ_HEAD(, tle_stream) fe;
134         STAILQ_HEAD(, tle_stream) be;
135 };
136
137 /* tempalte sizes/offsets/etc. for tcp stream */
138 struct stream_szofs {
139         uint32_t size;
140         struct {
141                 uint32_t ofs;
142                 uint32_t nb_obj;
143                 uint32_t nb_max;
144         } ofo;
145         struct {
146                 uint32_t ofs;
147                 uint32_t nb_obj;
148         } rxq, txq;
149         struct {
150                 uint32_t ofs;
151                 uint32_t blk_sz;
152                 uint32_t rng_sz;
153                 uint32_t nb_rng;
154                 uint32_t nb_obj;
155                 uint32_t nb_max;
156         } drb;
157 };
158
159 struct tcp_streams {
160         struct stbl st;
161         struct tle_timer_wheel *tmr; /* timer wheel */
162         struct rte_ring *tsq;        /* to-send streams queue */
163         struct tle_memtank *mts;     /* memtank to allocate streams from */
164         struct sdr dr;               /* death row for zombie streams */
165         struct stream_szofs szofs;   /* size and offsets for stream data */
166 };
167
168 #define CTX_TCP_STREAMS(ctx)    ((struct tcp_streams *)(ctx)->streams.buf)
169 #define CTX_TCP_STLB(ctx)       (&CTX_TCP_STREAMS(ctx)->st)
170 #define CTX_TCP_TMWHL(ctx)      (CTX_TCP_STREAMS(ctx)->tmr)
171 #define CTX_TCP_TSQ(ctx)        (CTX_TCP_STREAMS(ctx)->tsq)
172 #define CTX_TCP_SDR(ctx)        (&CTX_TCP_STREAMS(ctx)->dr)
173 #define CTX_TCP_MTS(ctx)        (CTX_TCP_STREAMS(ctx)->mts)
174
175 extern int tcp_stream_fill_prm(struct tle_tcp_stream *s,
176         const struct tle_tcp_stream_param *prm);
177
178 #ifdef __cplusplus
179 }
180 #endif
181
182 #endif /* _TCP_STREAM_H_ */