tcp_ctl.h revision 21e7392f
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
27extern "C" {
28#endif
29
30static inline void
31tcp_stream_down(struct tle_tcp_stream *s)
32{
33	rwl_down(&s->rx.use);
34	rwl_down(&s->tx.use);
35}
36
37static inline void
38tcp_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 */
45static inline uint32_t
46calc_rx_wnd(const struct tle_tcp_stream *s, uint32_t scale)
47{
48	return  s->rx.q->prod.mask << scale;
49}
50
51/* empty stream's receive queue */
52static void
53empty_rq(struct tle_tcp_stream *s)
54{
55	empty_mbuf_ring(s->rx.q);
56	tcp_ofo_reset(s->rx.ofo);
57}
58
59/* empty stream's listen queue */
60static void
61empty_lq(struct tle_tcp_stream *s, struct stbl *st)
62{
63	uint32_t i, n;
64	struct rte_mbuf *mb;
65	union pkt_info pi;
66	union seg_info si;
67	struct stbl_entry *se[MAX_PKT_BURST];
68
69	do {
70		n = rte_ring_dequeue_burst(s->rx.q, (void **)se, RTE_DIM(se));
71		for (i = 0; i != n; i++) {
72			mb = stbl_get_pkt(se[i]);
73			get_pkt_info(mb, &pi, &si);
74			stbl_del_pkt_lock(st, se[i], &pi);
75			rte_pktmbuf_free(mb);
76		}
77	} while (n != 0);
78}
79
80static inline void
81tcp_stream_reset(struct tle_ctx *ctx, struct tle_tcp_stream *s)
82{
83	struct stbl *st;
84	uint16_t uop;
85
86	st = CTX_TCP_STLB(ctx);
87
88	/* reset TX armed */
89	rte_atomic32_set(&s->tx.arm, 0);
90
91	/* reset TCB */
92	uop = s->tcb.uop & (TCP_OP_LISTEN | TCP_OP_CONNECT);
93	memset(&s->tcb, 0, sizeof(s->tcb));
94
95	/* reset cached destination */
96	memset(&s->tx.dst, 0, sizeof(s->tx.dst));
97
98	if (uop != 0) {
99		/* free stream's destination port */
100		stream_clear_ctx(ctx, &s->s);
101		if (uop == TCP_OP_LISTEN)
102			empty_lq(s, st);
103	}
104
105	if (s->ste != NULL) {
106		/* remove entry from RX streams table */
107		stbl_del_stream_lock(st, s->ste, s);
108		s->ste = NULL;
109		empty_rq(s);
110	}
111
112	/* empty TX queue */
113	empty_mbuf_ring(s->tx.q);
114
115	/*
116	 * mark the stream as free again.
117	 * if there still are pkts queued for TX,
118	 * then put this stream to the tail of free list.
119	 */
120	put_stream(ctx, &s->s, TCP_STREAM_TX_FINISHED(s));
121}
122
123#ifdef __cplusplus
124}
125#endif
126
127#endif /* _TCP_CTL_H_ */
128