aboutsummaryrefslogtreecommitdiffstats
path: root/lib/libtle_l4p/tcp_rxq.h
blob: 01f34fa16ceb4cbf758e36db129723aa6491d258 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * Copyright (c) 2016-2017  Intel Corporation.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef _TCP_RXQ_H_
#define _TCP_RXQ_H_

#include "tcp_ofo.h"

#ifdef __cplusplus
extern "C" {
#endif

struct rxq_objs {
	struct rte_mbuf **mb;
	uint32_t num;
};

static inline uint32_t
rx_ofo_enqueue(struct tle_tcp_stream *s, union seqlen *sl,
	struct rte_mbuf *mb[], uint32_t num)
{
	uint32_t i, n;

	n = 0;
	do {
		i = _ofo_step(s->rx.ofo, sl, mb + n, num - n);
		n += i;
	} while (i != 0 && n != num);

	_ofo_compact(s->rx.ofo);
	return n;
}

static inline uint32_t
rx_ofo_reduce(struct tle_tcp_stream *s)
{
	uint32_t i, n, end, seq;
	struct ofo *ofo;
	struct ofodb *db;
	union seqlen sl;

	seq = s->tcb.rcv.nxt;
	ofo = s->rx.ofo;

	n = 0;
	for (i = 0; i != ofo->nb_elem; i++) {

		db = ofo->db + i;

		/* gap still present */
		if (tcp_seq_lt(seq, db->sl.seq))
			break;

		end = db->sl.seq + db->sl.len;

		/* this db is fully overlapped */
		if (tcp_seq_leq(end, seq))
			_ofodb_free(db);
		else
			n += _ofodb_enqueue(s->rx.q, db, &sl);

		seq = sl.seq + sl.len;
	}

	s->tcb.rcv.nxt = seq;
	_ofo_remove(ofo, 0, i);
	return n;
}

static inline uint32_t
rx_ino_enqueue(struct tle_tcp_stream *s, union seqlen *sl,
	struct rte_mbuf *mb[], uint32_t num)
{
	uint32_t i, n;

	n = _rte_ring_enqueue_burst(s->rx.q, (void * const *)mb, num);

	/* error: can'queue some packets into receive buffer. */
	for (i = n; i != num; i++)
		sl->len -= mb[i]->pkt_len;

	s->tcb.rcv.nxt = sl->seq + sl->len;
	return n;
}

static inline uint32_t
rx_data_enqueue(struct tle_tcp_stream *s, uint32_t seq, uint32_t len,
	struct rte_mbuf *mb[], uint32_t num)
{
	uint32_t n, r, t;
	union seqlen sl;

	sl.seq = seq;
	sl.len = len;

	r = rte_ring_count(s->rx.q);

	/* in order packets, ready to be delivered */
	if (seq == s->tcb.rcv.nxt) {

		t = rx_ino_enqueue(s, &sl, mb, num);

		/* failed to queue all input in-order packets */
		if (t != num)
			TCP_LOG(DEBUG,
			"%s(s=%p, seq=%u, len=%u, num=%u) failed to queue "
			"%u packets;\n",
			__func__, s, seq, len, num, num - t);

		/* try to consume some out-of-order packets*/
		else {
			n = rx_ofo_reduce(s);
			if (n != 0)
				TCP_LOG(DEBUG,
				"%s(s=%p, rcv.nxt=%u) failed to queue %u "
				"OFO packets;\n",
				__func__, s, s->tcb.rcv.nxt, n);
		}

	/* queue out of order packets */
	} else {
		t = rx_ofo_enqueue(s, &sl, mb, num);
	}

	n = rte_ring_count(s->rx.q);
	if (r != n) {
		/* raise RX event */
		if (s->rx.ev != NULL)
			tle_event_raise(s->rx.ev);
		/* if RX queue was empty invoke RX notification callback. */
		else if (s->rx.cb.func != NULL && r == 0)
			s->rx.cb.func(s->rx.cb.data, &s->s);
	}

	return t;
}

static inline uint32_t
tcp_rxq_get_objs(struct tle_tcp_stream *s, struct rxq_objs obj[2])
{
	struct rte_ring *r;
	uint32_t n, head, sz;

	r = s->rx.q;

	n = _rte_ring_mcs_dequeue_start(r, UINT32_MAX);
	if (n == 0)
		return 0;

	sz = _rte_ring_get_size(r);
	head = (r->cons.head - n) & _rte_ring_get_mask(r);

	obj[0].mb = (struct rte_mbuf **)(_rte_ring_get_data(r) + head);
	obj[1].mb = (struct rte_mbuf **)_rte_ring_get_data(r);

	if (head + n <= sz) {
		obj[0].num = n;
		obj[1].num = 0;
		return 1;
	} else {
		obj[0].num = sz - head;
		obj[1].num = n + head - sz;
		return 2;
	}
}

static inline void
tcp_rxq_consume(struct tle_tcp_stream *s, uint32_t num)
{
	_rte_ring_mcs_dequeue_finish(s->rx.q, num);
}

#ifdef __cplusplus
}
#endif

#endif /* _TCP_RXQ_H_ */