Added rte_ring wrapper functions to support dpdk-17.05 and older version
[tldk.git] / lib / libtle_l4p / tcp_ctl.h
1 /*
2  * Copyright (c) 2016  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 /*
17  * Some helper stream control functions definitions.
18  */
19
20 #ifndef _TCP_CTL_H_
21 #define _TCP_CTL_H_
22
23 #include "tcp_stream.h"
24 #include "tcp_ofo.h"
25
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29
30 static inline void
31 tcp_stream_down(struct tle_tcp_stream *s)
32 {
33         rwl_down(&s->rx.use);
34         rwl_down(&s->tx.use);
35 }
36
37 static inline void
38 tcp_stream_up(struct tle_tcp_stream *s)
39 {
40         rwl_up(&s->rx.use);
41         rwl_up(&s->tx.use);
42 }
43
44 /* calculate RCV.WND value based on size of stream receive buffer */
45 static inline uint32_t
46 calc_rx_wnd(const struct tle_tcp_stream *s, uint32_t scale)
47 {
48         uint32_t wnd;
49
50         /* peer doesn't support WSCALE option, wnd size is limited to 64K */
51         if (scale == TCP_WSCALE_NONE) {
52                 wnd = _rte_ring_get_mask(s->rx.q) << TCP_WSCALE_DEFAULT;
53                 return RTE_MIN(wnd, (uint32_t)UINT16_MAX);
54         } else
55                 return  _rte_ring_get_mask(s->rx.q) << scale;
56 }
57
58 /* empty stream's receive queue */
59 static void
60 empty_rq(struct tle_tcp_stream *s)
61 {
62         empty_mbuf_ring(s->rx.q);
63         tcp_ofo_reset(s->rx.ofo);
64 }
65
66 /* empty stream's listen queue */
67 static void
68 empty_lq(struct tle_tcp_stream *s, struct stbl *st)
69 {
70         uint32_t i, n;
71         struct rte_mbuf *mb;
72         union pkt_info pi;
73         union seg_info si;
74         struct stbl_entry *se[MAX_PKT_BURST];
75
76         do {
77                 n = _rte_ring_dequeue_burst(s->rx.q, (void **)se, RTE_DIM(se));
78                 for (i = 0; i != n; i++) {
79                         mb = stbl_get_pkt(se[i]);
80                         get_pkt_info(mb, &pi, &si);
81                         stbl_del_pkt_lock(st, se[i], &pi);
82                         rte_pktmbuf_free(mb);
83                 }
84         } while (n != 0);
85 }
86
87 static inline void
88 tcp_stream_reset(struct tle_ctx *ctx, struct tle_tcp_stream *s)
89 {
90         struct stbl *st;
91         uint16_t uop;
92
93         st = CTX_TCP_STLB(ctx);
94
95         /* reset TX armed */
96         rte_atomic32_set(&s->tx.arm, 0);
97
98         /* reset TCB */
99         uop = s->tcb.uop & ~TCP_OP_CLOSE;
100         memset(&s->tcb, 0, sizeof(s->tcb));
101
102         /* reset cached destination */
103         memset(&s->tx.dst, 0, sizeof(s->tx.dst));
104
105         if (uop != TCP_OP_ACCEPT) {
106                 /* free stream's destination port */
107                 stream_clear_ctx(ctx, &s->s);
108                 if (uop == TCP_OP_LISTEN)
109                         empty_lq(s, st);
110         }
111
112         if (s->ste != NULL) {
113                 /* remove entry from RX streams table */
114                 stbl_del_stream_lock(st, s->ste, s);
115                 s->ste = NULL;
116                 empty_rq(s);
117         }
118
119         /* empty TX queue */
120         empty_mbuf_ring(s->tx.q);
121
122         /*
123          * mark the stream as free again.
124          * if there still are pkts queued for TX,
125          * then put this stream to the tail of free list.
126          */
127         put_stream(ctx, &s->s, TCP_STREAM_TX_FINISHED(s));
128 }
129
130 #ifdef __cplusplus
131 }
132 #endif
133
134 #endif /* _TCP_CTL_H_ */