summaryrefslogtreecommitdiffstats
path: root/VNFs/DPPD-PROX/handle_lb_qinq.c
blob: 18ff7df462823660b7b8292509e09353bc4ed3b2 (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
// Copyright (c) 2010-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.
*/

#include <string.h>

#include <rte_mbuf.h>
#include <rte_ip.h>
#include <rte_byteorder.h>
#include <rte_version.h>
#include <rte_hash_crc.h>

#include "prox_malloc.h"
#include "task_base.h"
#include "tx_pkt.h"
#include "rx_pkt.h"
#include "etypes.h"
#include "log.h"
#include "quit.h"
#include "qinq.h"
#include "lconf.h"
#include "prefetch.h"
#include "defines.h"
#include "prox_cfg.h"
#include "hash_utils.h"
#include "handle_lb_net.h"
#include "toeplitz.h"

#if RTE_VERSION < RTE_VERSION_NUM(1,8,0,0)
#define RTE_CACHE_LINE_SIZE CACHE_LINE_SIZE
#endif

/* Load balancing based on one byte, figures out what type of packet
   is passed and depending on the type, pass the packet to the correct
   worker thread. If an unsupported packet type is used, the packet is
   simply dropped. This Load balancer can only handling QinQ packets
   (i.e. packets comming from the vCPE). */
int handle_lb_qinq_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts);
int handle_lb_qinq_bulk_set_port(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts);

struct task_lb_qinq {
	struct task_base        base;
	uint8_t                 *worker_table;
	uint8_t                 bit_mask;
	uint8_t                 protocols_mask;
	uint8_t                 nb_worker_threads;
	uint16_t                qinq_tag;
};

static void init_task_lb_qinq(struct task_base *tbase, struct task_args *targ)
{
	struct task_lb_qinq *task = (struct task_lb_qinq *)tbase;
	const int socket_id = rte_lcore_to_socket_id(targ->lconf->id);

	task->qinq_tag = targ->qinq_tag;
	task->nb_worker_threads = targ->nb_worker_threads;
	task->bit_mask = rte_is_power_of_2(targ->nb_worker_threads) ? targ->nb_worker_threads - 1 : 0xff;

	/* The load distributor is sending to a set of cores. These
	   cores are responsible for handling a set of flows
	   identified by a qinq tag. The load distributor identifies
	   the flows and forwards them to the appropriate worker. The
	   mapping from flow to worker is stored within the
	   work_table. Build the worker_table by asking each worker
	   which flows are handled. */

	task->worker_table = prox_zmalloc(0x1000000, socket_id);
	for (int i = 0; i < targ->nb_worker_threads; ++i) {
		struct core_task ct = targ->core_task_set[0].core_task[i];
		struct task_args *t = core_targ_get(ct.core, ct.task);

		PROX_PANIC(t->task_init->flow_iter.beg == NULL,
			   "Load distributor can't find flows owned by destination worker %d\n", i);

		struct flow_iter *it = &t->task_init->flow_iter;

		int cnt = 0;
		for (it->beg(it, t); !it->is_end(it, t); it->next(it, t)) {
			uint16_t svlan = it->get_svlan(it, t);
			uint16_t cvlan = it->get_cvlan(it, t);

			task->worker_table[PKT_TO_LUTQINQ(svlan, cvlan)] = i;
		}

	}

	/* Check which protocols we are allowed to send to worker tasks */
	for (int i = 0; i < MAX_PROTOCOLS; ++i) {
		int is_active = !!targ->core_task_set[i].n_elems;
		task->protocols_mask |= is_active << i;
	}
	plog_info("\t\ttask_lb_qinq protocols_mask = 0x%x\n", task->protocols_mask);

	if (targ->task_init->flag_features & TASK_FEATURE_LUT_QINQ_RSS)
		tbase->flags |=  BASE_FLAG_LUT_QINQ_RSS;
	if (targ->task_init->flag_features & TASK_FEATURE_LUT_QINQ_HASH)
		tbase->flags |=  BASE_FLAG_LUT_QINQ_HASH;
	plog_info("\t\ttask_lb_qinq flags = 0x%x\n", tbase->flags);
}

static struct task_init task_init_lb_qinq = {
	.mode_str = "lbqinq",
	.init = init_task_lb_qinq,
	.handle = handle_lb_qinq_bulk,
	.size = sizeof(struct task_lb_qinq)
};

/*
	Add correct port id to mbufs coming from a DPDK ring port in the loadbalancer.
	For the split-bng using DPDK rings between the vSwitch and the VMs
	we need to know the port from which a packet was received.
	The ring PMD in dpdk does not update the port field in the mbuf
	and thus we have no control over the port numbers that are being used.
	This submode allows the loadbalancer to set the port number on which it
	received the mbuf.
*/
static struct task_init task_init_lb_qinq_set_port = {
	.mode_str = "lbqinq",
	.sub_mode_str = "lut_qinq_set_port",
	.init = init_task_lb_qinq,
	.handle = handle_lb_qinq_bulk_set_port,
	.size = sizeof(struct task_lb_qinq)
};

/*
	Load Balance on Hash of combination of cvlan and svlan
*/
static struct task_init task_init_lb_qinq_hash_friend = {
	.mode_str = "lbqinq",
	.sub_mode_str ="lut_qinq_hash_friend",
	.init = init_task_lb_qinq,
	.handle = handle_lb_qinq_bulk,
	.flag_features = TASK_FEATURE_LUT_QINQ_HASH,
	.size = sizeof(struct task_lb_qinq)
};

/*
	Load Balance on rss of combination of cvlan and svlan.
	This could be used to compare with HW implementations.
*/
static struct task_init task_init_lb_qinq_rss_friend = {
	.mode_str = "lbqinq",
	.sub_mode_str ="lut_qinq_rss_friend",
	.init = init_task_lb_qinq,
	.handle = handle_lb_qinq_bulk,
	.flag_features = TASK_FEATURE_LUT_QINQ_RSS,
	.size = sizeof(struct task_lb_qinq)
};

__attribute__((constructor)) static void reg_task_lb_qinq(void)
{
	reg_task(&task_init_lb_qinq);
	reg_task(&task_init_lb_qinq_hash_friend);
	reg_task(&task_init_lb_qinq_rss_friend);
	reg_task(&task_init_lb_qinq_set_port);
}

static inline uint8_t handle_lb_qinq(struct task_lb_qinq *task, struct rte_mbuf *mbuf);

int handle_lb_qinq_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
{
	struct task_lb_qinq *task = (struct task_lb_qinq *)tbase;
	uint8_t out[MAX_PKT_BURST];
	uint16_t j;

	prefetch_first(mbufs, n_pkts);

	for (j = 0; j + PREFETCH_OFFSET < n_pkts; ++j) {
#ifdef PROX_PREFETCH_OFFSET
		PREFETCH0(mbufs[j + PREFETCH_OFFSET]);
		PREFETCH0(rte_pktmbuf_mtod(mbufs[j + PREFETCH_OFFSET - 1], void *));
#endif
		out[j] = handle_lb_qinq(task, mbufs[j]);
	}
#ifdef PROX_PREFETCH_OFFSET
	PREFETCH0(rte_pktmbuf_mtod(mbufs[n_pkts - 1], void *));
	for (; j < n_pkts; ++j) {
		out[j] = handle_lb_qinq(task, mbufs[j]);
	}
#endif

	return task->base.tx_pkt(&task->base, mbufs, n_pkts, out);
}

int handle_lb_qinq_bulk_set_port(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
{
	struct task_lb_qinq *task = (struct task_lb_qinq *)tbase;
	uint8_t out[MAX_PKT_BURST];
	uint16_t j;
#if RTE_VERSION < RTE_VERSION_NUM(1,8,0,0)
	uint32_t port_id = mbufs[0]->pkt.in_port;
#else
	uint32_t port_id = mbufs[0]->port;
#endif

	if (tbase->rx_pkt == rx_pkt_hw) {
		port_id = tbase->rx_params_hw.last_read_portid + tbase->rx_params_hw.nb_rxports;
		port_id = ( port_id - 1 ) % tbase->rx_params_hw.nb_rxports;
		port_id = tbase->rx_params_hw.rx_pq[port_id].port;
	} else if (tbase->rx_pkt == rx_pkt_hw1) {
		port_id = tbase->rx_params_hw1.rx_pq.port;
	}

	prefetch_first(mbufs, n_pkts);

	for (j = 0; j + PREFETCH_OFFSET < n_pkts; ++j) {
#ifdef PROX_PREFETCH_OFFSET
		PREFETCH0(mbufs[j + PREFETCH_OFFSET]);
		PREFETCH0(rte_pktmbuf_mtod(mbufs[j + PREFETCH_OFFSET - 1], void *));
#endif
#if RTE_VERSION < RTE_VERSION_NUM(1,8,0,0)
		mbufs[j]->pkt.in_port = port_id;
#else
		mbufs[j]->port = port_id;
#endif
		out[j] = handle_lb_qinq(task, mbufs[j]);
	}
#ifdef PROX_PREFETCH_OFFSET
	PREFETCH0(rte_pktmbuf_mtod(mbufs[n_pkts - 1], void *));
	for (; j < n_pkts; ++j) {
#if RTE_VERSION < RTE_VERSION_NUM(1,8,0,0)
		mbufs[j]->pkt.in_port = port_id;
#else
		mbufs[j]->port = port_id;
#endif
		out[j] = handle_lb_qinq(task, mbufs[j]);
	}
#endif

	return task->base.tx_pkt(&task->base, mbufs, n_pkts, out);
}

struct qinq_packet {
	struct qinq_hdr qinq_hdr;
	union {
		struct ipv4_hdr ipv4_hdr;
		struct ipv6_hdr ipv6_hdr;
	};
} __attribute__((packed));

struct qinq_packet_data {
	struct ether_addr  d_addr;
	struct ether_addr  s_addr;
	uint64_t qinq;
} __attribute__((packed));

struct ether_packet {
	struct ether_hdr ether_hdr;
	union {
		struct ipv4_hdr ipv4_hdr;
		struct ipv6_hdr ipv6_hdr;
	};
} __attribute__((packed));

struct cpe_packet {
	union {
		struct qinq_packet  qp;
		struct ether_packet ep;
		struct qinq_packet_data qd;
	};
};

static inline uint8_t get_worker(struct task_lb_qinq *task, struct cpe_packet *packet)
{
	uint8_t worker = 0;
	if (((struct task_base *)task)->flags & BASE_FLAG_LUT_QINQ_HASH) {
		// Load Balance on Hash of combination of cvlan and svlan
		uint64_t qinq_net = packet->qd.qinq;
		qinq_net = qinq_net & 0xFF0F0000FF0F0000;	// Mask Proto and QoS bits
		if (task->bit_mask != 0xff) {
			worker = rte_hash_crc(&qinq_net,8,0) & task->bit_mask;
		}
		else {
			worker = rte_hash_crc(&qinq_net,8,0) % task->nb_worker_threads;
		}
		plogx_dbg("Sending packet svlan=%x, cvlan=%x, pseudo_qinq=%lx to worker %d\n", rte_bswap16(0xFF0F & packet->qp.qinq_hdr.svlan.vlan_tci), rte_bswap16(0xFF0F & packet->qp.qinq_hdr.cvlan.vlan_tci), qinq_net, worker);
	} else if (((struct task_base *)task)->flags & BASE_FLAG_LUT_QINQ_RSS){
		// Load Balance on rss of combination of cvlan and svlan
		uint32_t qinq = (packet->qp.qinq_hdr.cvlan.vlan_tci & 0xFF0F) << 16;
		uint32_t rss = toeplitz_hash((uint8_t *)&qinq, 4);
		if (task->bit_mask != 0xff) {
			worker = rss & task->bit_mask;
		} else {
			worker = (0x1ff & rss) % task->nb_worker_threads;
		}
		plogx_dbg("Sending packet svlan=%x, cvlan=%x, rss_input=%x, rss=%x to worker %d\n", rte_bswap16(0xFF0F & packet->qp.qinq_hdr.svlan.vlan_tci), rte_bswap16(0xFF0F & packet->qp.qinq_hdr.cvlan.vlan_tci), qinq, rss, worker);
	} else {
		uint16_t svlan = packet->qp.qinq_hdr.svlan.vlan_tci;
		uint16_t cvlan = packet->qp.qinq_hdr.cvlan.vlan_tci;
		prefetch_nta(&task->worker_table[PKT_TO_LUTQINQ(svlan, cvlan)]);
		worker = task->worker_table[PKT_TO_LUTQINQ(svlan, cvlan)];

		const size_t pos = offsetof(struct cpe_packet, qp.qinq_hdr.cvlan.vlan_tci);
		plogx_dbg("qinq = %u, worker = %u, pos = %lu\n", rte_be_to_cpu_16(cvlan), worker, pos);
	}
	return worker;
}

static inline uint8_t handle_lb_qinq(struct task_lb_qinq *task, struct rte_mbuf *mbuf)
{
	struct cpe_packet *packet = rte_pktmbuf_mtod(mbuf, struct cpe_packet*);
	if (packet->ep.ether_hdr.ether_type == ETYPE_IPv4) {
		if (unlikely((packet->ep.ipv4_hdr.version_ihl >> 4) != 4)) {
			plogx_err("Invalid Version %u for ETYPE_IPv4\n", packet->ep.ipv4_hdr.version_ihl);
			return OUT_DISCARD;
		}
		/* use 24 bits from the IP, clients are from the 10.0.0.0/8 network */
		const uint32_t tmp = rte_bswap32(packet->ep.ipv4_hdr.src_addr) & 0x00FFFFFF;
		const uint32_t svlan = rte_bswap16(tmp >> 12);
		const uint32_t cvlan = rte_bswap16(tmp & 0x0FFF);
		prefetch_nta(&task->worker_table[PKT_TO_LUTQINQ(svlan, cvlan)]);
		uint8_t worker = task->worker_table[PKT_TO_LUTQINQ(svlan, cvlan)];
		return worker + IPV4 * task->nb_worker_threads;
	}
	else if (unlikely(packet->qp.qinq_hdr.svlan.eth_proto != task->qinq_tag)) {
		/* might receive LLDP from the L2 switch... */
		if (packet->qp.qinq_hdr.svlan.eth_proto != ETYPE_LLDP) {
			plogdx_err(mbuf, "Invalid packet for LB in QinQ mode\n");
		}
		return OUT_DISCARD;
	}

	uint8_t worker = 0;
	uint8_t proto = 0xFF;
	switch (packet->qp.qinq_hdr.ether_type) {
	case ETYPE_IPv4: {
		if (unlikely((packet->qp.ipv4_hdr.version_ihl >> 4) != 4)) {
			plogx_err("Invalid Version %u for ETYPE_IPv4\n", packet->qp.ipv4_hdr.version_ihl);
			return OUT_DISCARD;
		}
		worker = get_worker(task, packet);
		proto = IPV4;
		break;
	}
	case ETYPE_IPv6: {
		if (unlikely((packet->qp.ipv4_hdr.version_ihl >> 4) != 6)) {
			plogx_err("Invalid Version %u for ETYPE_IPv6\n", packet->qp.ipv4_hdr.version_ihl);
			return OUT_DISCARD;
		}
		/* Use IP Destination when IPV6 QinQ */
		if (task->bit_mask != 0xff) {
			worker = ((uint8_t *)packet)[61] & task->bit_mask;
		}
		else {
			worker = ((uint8_t *)packet)[61] % task->nb_worker_threads;
		}
		proto = IPV6;
		break;
	}
	case ETYPE_ARP: {
		// We can only send to ARP ring if it exists
		if (0 != (task->protocols_mask & (1 << ARP))) {
			proto = ARP;
		} else {
			proto = IPV4;
		}
		worker = get_worker(task, packet);
		break;
	}
	default:
		plogx_warn("Error in ETYPE_8021ad: ether_type = %#06x\n", packet->qp.qinq_hdr.ether_type);
		return OUT_DISCARD;
	}

	return worker + proto * task->nb_worker_threads;
}