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
379
380
381
382
383
384
385
386
387
388
|
/*
* Zebra dataplane plugin for DPDK based hw offload
*
* Copyright (C) 2021 Nvidia
* Anuradha Karuppiah
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; see the file COPYING; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef HAVE_CONFIG_H
#include "config.h" /* Include this explicitly */
#endif
#include "lib/libfrr.h"
#include "zebra/debug.h"
#include "zebra/interface.h"
#include "zebra/zebra_dplane.h"
#include "zebra/debug.h"
#include "zebra/zebra_pbr.h"
#include "zebra/dpdk/zebra_dplane_dpdk_private.h"
static const char *plugin_name = "zebra_dplane_dpdk";
extern struct zebra_privs_t zserv_privs;
static struct zd_dpdk_ctx dpdk_ctx_buf, *dpdk_ctx = &dpdk_ctx_buf;
#define dpdk_stat (&dpdk_ctx->stats)
static struct zd_dpdk_port *zd_dpdk_port_find_by_index(int ifindex);
DEFINE_MTYPE_STATIC(ZEBRA, DPDK_PORTS, "ZD DPDK port database");
void zd_dpdk_stat_show(struct vty *vty)
{
uint32_t tmp_cnt;
vty_out(vty, "%30s\n%30s\n", "Dataplane DPDK counters",
"=======================");
#define ZD_DPDK_SHOW_COUNTER(label, counter) \
do { \
tmp_cnt = \
atomic_load_explicit(&counter, memory_order_relaxed); \
vty_out(vty, "%28s: %u\n", (label), (tmp_cnt)); \
} while (0);
ZD_DPDK_SHOW_COUNTER("PBR rule adds", dpdk_stat->rule_adds);
ZD_DPDK_SHOW_COUNTER("PBR rule dels", dpdk_stat->rule_dels);
ZD_DPDK_SHOW_COUNTER("Ignored updates", dpdk_stat->ignored_updates);
}
static void zd_dpdk_rule_add(struct zebra_dplane_ctx *ctx)
{
/* XXX - place holder */
}
static void zd_dpdk_rule_del(const char *ifname, int in_ifindex,
intptr_t dp_flow_ptr)
{
/* XXX - place holder */
}
static void zd_dpdk_rule_update(struct zebra_dplane_ctx *ctx)
{
enum dplane_op_e op;
int in_ifindex;
intptr_t dp_flow_ptr;
if (IS_ZEBRA_DEBUG_DPLANE_DPDK_DETAIL) {
zlog_debug("Dplane %s", dplane_op2str(dplane_ctx_get_op(ctx)));
}
op = dplane_ctx_get_op(ctx);
switch (op) {
case DPLANE_OP_RULE_ADD:
atomic_fetch_add_explicit(&dpdk_stat->rule_adds, 1,
memory_order_relaxed);
zd_dpdk_rule_add(ctx);
break;
case DPLANE_OP_RULE_UPDATE:
/* delete old rule and install new one */
atomic_fetch_add_explicit(&dpdk_stat->rule_adds, 1,
memory_order_relaxed);
in_ifindex = dplane_ctx_get_ifindex(ctx);
dp_flow_ptr = dplane_ctx_rule_get_old_dp_flow_ptr(ctx);
zd_dpdk_rule_del(dplane_ctx_rule_get_ifname(ctx), in_ifindex,
dp_flow_ptr);
zd_dpdk_rule_add(ctx);
break;
case DPLANE_OP_RULE_DELETE:
atomic_fetch_add_explicit(&dpdk_stat->rule_dels, 1,
memory_order_relaxed);
in_ifindex = dplane_ctx_get_ifindex(ctx);
dp_flow_ptr = dplane_ctx_rule_get_dp_flow_ptr(ctx);
zd_dpdk_rule_del(dplane_ctx_rule_get_ifname(ctx), in_ifindex,
dp_flow_ptr);
break;
default:;
}
}
/* DPDK provider callback.
*/
static void zd_dpdk_process_update(struct zebra_dplane_ctx *ctx)
{
switch (dplane_ctx_get_op(ctx)) {
case DPLANE_OP_RULE_ADD:
case DPLANE_OP_RULE_UPDATE:
case DPLANE_OP_RULE_DELETE:
zd_dpdk_rule_update(ctx);
break;
default:
atomic_fetch_add_explicit(&dpdk_stat->ignored_updates, 1,
memory_order_relaxed);
break;
}
}
static int zd_dpdk_process(struct zebra_dplane_provider *prov)
{
struct zebra_dplane_ctx *ctx;
int counter, limit;
if (IS_ZEBRA_DEBUG_DPLANE_DPDK_DETAIL)
zlog_debug("processing %s", dplane_provider_get_name(prov));
limit = dplane_provider_get_work_limit(prov);
for (counter = 0; counter < limit; counter++) {
ctx = dplane_provider_dequeue_in_ctx(prov);
if (!ctx)
break;
zd_dpdk_process_update(ctx);
dplane_ctx_set_status(ctx, ZEBRA_DPLANE_REQUEST_SUCCESS);
dplane_provider_enqueue_out_ctx(prov, ctx);
}
return 0;
}
static void zd_dpdk_port_show_entry(struct zd_dpdk_port *dport, struct vty *vty,
int detail)
{
struct rte_eth_dev_info *dev_info;
dev_info = &dport->dev_info;
if (detail) {
vty_out(vty, "DPDK port: %u\n", dport->port_id);
vty_out(vty, " Device: %s\n",
dev_info->device ? dev_info->device->name : "-");
vty_out(vty, " Driver: %s\n",
dev_info->driver_name ? dev_info->driver_name : "-");
vty_out(vty, " Interface: %s (%d)\n",
ifindex2ifname(dev_info->if_index, VRF_DEFAULT),
dev_info->if_index);
vty_out(vty, " Switch: %s Domain: %u Port: %u\n",
dev_info->switch_info.name,
dev_info->switch_info.domain_id,
dev_info->switch_info.port_id);
vty_out(vty, "\n");
} else {
vty_out(vty, "%-4u %-16s %-16s %-16d %s,%u,%u\n",
dport->port_id,
dev_info->device ? dev_info->device->name : "-",
ifindex2ifname(dev_info->if_index, VRF_DEFAULT),
dev_info->if_index, dev_info->switch_info.name,
dev_info->switch_info.domain_id,
dev_info->switch_info.port_id);
}
}
static struct zd_dpdk_port *zd_dpdk_port_find_by_index(int ifindex)
{
int count;
struct zd_dpdk_port *dport;
struct rte_eth_dev_info *dev_info;
for (count = 0; count < RTE_MAX_ETHPORTS; ++count) {
dport = &dpdk_ctx->dpdk_ports[count];
if (!(dport->flags & ZD_DPDK_PORT_FLAG_INITED))
continue;
dev_info = &dport->dev_info;
if (dev_info->if_index == (uint32_t)ifindex)
return dport;
}
return NULL;
}
void zd_dpdk_port_show(struct vty *vty, uint16_t port_id, bool uj, int detail)
{
int count;
struct zd_dpdk_port *dport;
/* XXX - support for json is yet to be added */
if (uj)
return;
if (!detail) {
vty_out(vty, "%-4s %-16s %-16s %-16s %s\n", "Port", "Device",
"IfName", "IfIndex", "sw,domain,port");
}
for (count = 0; count < RTE_MAX_ETHPORTS; ++count) {
dport = &dpdk_ctx->dpdk_ports[count];
if (dport->flags & ZD_DPDK_PORT_FLAG_INITED)
zd_dpdk_port_show_entry(dport, vty, detail);
}
}
static void zd_dpdk_port_init(void)
{
struct zd_dpdk_port *dport;
uint16_t port_id;
struct rte_eth_dev_info *dev_info;
int count;
int rc;
struct rte_flow_error error;
/* allocate a list of ports */
dpdk_ctx->dpdk_ports =
XCALLOC(MTYPE_DPDK_PORTS,
sizeof(struct zd_dpdk_port) * RTE_MAX_ETHPORTS);
if (IS_ZEBRA_DEBUG_DPLANE_DPDK)
zlog_debug("dpdk port init");
count = 0;
RTE_ETH_FOREACH_DEV(port_id)
{
if (IS_ZEBRA_DEBUG_DPLANE_DPDK)
zlog_debug("dpdk port init %d", port_id);
dport = &dpdk_ctx->dpdk_ports[count];
count++;
dport->port_id = port_id;
dport->flags |= ZD_DPDK_PORT_FLAG_PROBED;
dev_info = &dport->dev_info;
if (rte_eth_dev_info_get(port_id, dev_info) < 0) {
zlog_warn("failed to get dev info for %u, %s", port_id,
rte_strerror(rte_errno));
continue;
}
dport->flags |= ZD_DPDK_PORT_FLAG_INITED;
if (IS_ZEBRA_DEBUG_DPLANE_DPDK)
zlog_debug(
"port %u, dev %s, ifI %d, sw_name %s, sw_domain %u, sw_port %u",
port_id,
dev_info->device ? dev_info->device->name : "-",
dev_info->if_index, dev_info->switch_info.name,
dev_info->switch_info.domain_id,
dev_info->switch_info.port_id);
if (rte_flow_isolate(port_id, 1, &error)) {
if (IS_ZEBRA_DEBUG_DPLANE_DPDK)
zlog_debug(
"Flow isolate on port %u failed %d\n",
port_id, error.type);
} else {
if (IS_ZEBRA_DEBUG_DPLANE_DPDK)
zlog_debug("Flow isolate on port %u\n",
port_id);
}
rc = rte_eth_dev_start(port_id);
if (rc) {
zlog_warn("DPDK port %d start error: %s", port_id,
rte_strerror(-rc));
continue;
}
if (IS_ZEBRA_DEBUG_DPLANE_DPDK)
zlog_debug("DPDK port %d started in promiscuous mode ",
port_id);
}
if (!count) {
if (IS_ZEBRA_DEBUG_DPLANE_DPDK)
zlog_debug("no probed ethernet devices");
}
}
static int zd_dpdk_init(void)
{
int rc;
char *argv[] = {(char *)"/usr/lib/frr/zebra", (char *)"--"};
zd_dpdk_vty_init();
frr_with_privs (&zserv_privs) {
rc = rte_eal_init(sizeof(argv) / sizeof(argv[0]), argv);
}
if (rc < 0) {
zlog_warn("EAL init failed %s", rte_strerror(rte_errno));
return -1;
}
frr_with_privs (&zserv_privs) {
zd_dpdk_port_init();
}
return 0;
}
static int zd_dpdk_start(struct zebra_dplane_provider *prov)
{
if (IS_ZEBRA_DEBUG_DPLANE_DPDK)
zlog_debug("%s start", dplane_provider_get_name(prov));
return zd_dpdk_init();
}
static int zd_dpdk_finish(struct zebra_dplane_provider *prov, bool early)
{
int rc;
if (early) {
if (IS_ZEBRA_DEBUG_DPLANE_DPDK)
zlog_debug("%s early finish",
dplane_provider_get_name(prov));
return 0;
}
if (IS_ZEBRA_DEBUG_DPLANE_DPDK)
zlog_debug("%s finish", dplane_provider_get_name(prov));
frr_with_privs (&zserv_privs) {
rc = rte_eal_cleanup();
}
if (rc < 0)
zlog_warn("EAL cleanup failed %s", rte_strerror(rte_errno));
return 0;
}
static int zd_dpdk_plugin_init(struct thread_master *tm)
{
int ret;
ret = dplane_provider_register(
plugin_name, DPLANE_PRIO_KERNEL, DPLANE_PROV_FLAGS_DEFAULT,
zd_dpdk_start, zd_dpdk_process, zd_dpdk_finish, dpdk_ctx, NULL);
if (IS_ZEBRA_DEBUG_DPLANE_DPDK)
zlog_debug("%s register status %d", plugin_name, ret);
return 0;
}
static int zd_dpdk_module_init(void)
{
hook_register(frr_late_init, zd_dpdk_plugin_init);
return 0;
}
FRR_MODULE_SETUP(.name = "dplane_dpdk", .version = "0.0.1",
.description = "Data plane plugin using dpdk for hw offload",
.init = zd_dpdk_module_init, );
|