af_xdp: fix NUMA node parsing
[vpp.git] / src / plugins / af_xdp / device.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2018 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17
18 #include <stdio.h>
19 #include <net/if.h>
20 #include <linux/if_link.h>
21 #include <bpf/libbpf.h>
22 #include <vlib/vlib.h>
23 #include <vlib/unix/unix.h>
24 #include <vlib/pci/pci.h>
25 #include <vppinfra/linux/sysfs.h>
26 #include <vppinfra/unix.h>
27 #include <vnet/ethernet/ethernet.h>
28 #include "af_xdp.h"
29
30 af_xdp_main_t af_xdp_main;
31
32 typedef struct
33 {
34   u32 prod;
35   u32 cons;
36 } gdb_af_xdp_pair_t;
37
38 gdb_af_xdp_pair_t
39 gdb_af_xdp_get_prod (const struct xsk_ring_prod *prod)
40 {
41   gdb_af_xdp_pair_t pair = { *prod->producer, *prod->consumer };
42   return pair;
43 }
44
45 gdb_af_xdp_pair_t
46 gdb_af_xdp_get_cons (const struct xsk_ring_cons * cons)
47 {
48   gdb_af_xdp_pair_t pair = { *cons->producer, *cons->consumer };
49   return pair;
50 }
51
52 static clib_error_t *
53 af_xdp_mac_change (vnet_hw_interface_t * hw, const u8 * old, const u8 * new)
54 {
55   af_xdp_main_t *am = &af_xdp_main;
56   af_xdp_device_t *ad = vec_elt_at_index (am->devices, hw->dev_instance);
57   errno_t err = memcpy_s (ad->hwaddr, sizeof (ad->hwaddr), new, 6);
58   if (err)
59     return clib_error_return_code (0, -err, CLIB_ERROR_ERRNO_VALID,
60                                    "mac change failed");
61   return 0;
62 }
63
64 static u32
65 af_xdp_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hw, u32 flags)
66 {
67   af_xdp_main_t *am = &af_xdp_main;
68   af_xdp_device_t *ad = vec_elt_at_index (am->devices, hw->dev_instance);
69
70   switch (flags)
71     {
72     case 0:
73       af_xdp_log (VLIB_LOG_LEVEL_ERR, ad, "set unicast not supported yet");
74       return ~0;
75     case ETHERNET_INTERFACE_FLAG_ACCEPT_ALL:
76       af_xdp_log (VLIB_LOG_LEVEL_ERR, ad,
77                   "set promiscuous not supported yet");
78       return ~0;
79     case ETHERNET_INTERFACE_FLAG_MTU:
80       af_xdp_log (VLIB_LOG_LEVEL_ERR, ad, "set mtu not supported yet");
81       return ~0;
82     }
83
84   af_xdp_log (VLIB_LOG_LEVEL_ERR, ad, "unknown flag %x requested", flags);
85   return ~0;
86 }
87
88 void
89 af_xdp_delete_if (vlib_main_t * vm, af_xdp_device_t * ad)
90 {
91   vnet_main_t *vnm = vnet_get_main ();
92   af_xdp_main_t *axm = &af_xdp_main;
93   struct xsk_socket **xsk;
94   struct xsk_umem **umem;
95   af_xdp_rxq_t *rxq;
96   af_xdp_txq_t *txq;
97
98   if (ad->hw_if_index)
99     {
100       vnet_hw_interface_set_flags (vnm, ad->hw_if_index, 0);
101       vnet_hw_interface_unassign_rx_thread (vnm, ad->hw_if_index, 0);
102       ethernet_delete_interface (vnm, ad->hw_if_index);
103     }
104
105   vec_foreach (rxq, ad->rxqs) clib_file_del_by_index (&file_main,
106                                                       rxq->file_index);
107   vec_foreach (txq, ad->txqs) clib_spinlock_free (&txq->lock);
108   vec_foreach (xsk, ad->xsk) xsk_socket__delete (*xsk);
109   vec_foreach (umem, ad->umem) xsk_umem__delete (*umem);
110
111   if (ad->bpf_obj)
112     {
113       bpf_set_link_xdp_fd (ad->linux_ifindex, -1, 0);
114       bpf_object__unload (ad->bpf_obj);
115     }
116
117   vec_free (ad->xsk);
118   vec_free (ad->umem);
119   vec_free (ad->buffer_template);
120   vec_free (ad->rxqs);
121   vec_free (ad->txqs);
122   clib_error_free (ad->error);
123   pool_put (axm->devices, ad);
124 }
125
126 static int
127 af_xdp_load_program (af_xdp_create_if_args_t * args, af_xdp_device_t * ad)
128 {
129   int fd;
130
131   ad->linux_ifindex = if_nametoindex (ad->linux_ifname);
132   if (!ad->linux_ifindex)
133     {
134       args->rv = VNET_API_ERROR_INVALID_VALUE;
135       args->error =
136         clib_error_return_unix (0, "if_nametoindex(%s) failed",
137                                 ad->linux_ifname);
138       goto err0;
139     }
140
141   if (bpf_prog_load (args->prog, BPF_PROG_TYPE_XDP, &ad->bpf_obj, &fd))
142     {
143       args->rv = VNET_API_ERROR_SYSCALL_ERROR_5;
144       args->error =
145         clib_error_return_unix (0, "bpf_prog_load(%s) failed", args->prog);
146       goto err0;
147     }
148
149 #ifndef XDP_FLAGS_REPLACE
150 #define XDP_FLAGS_REPLACE 0
151 #endif
152   if (bpf_set_link_xdp_fd (ad->linux_ifindex, fd, XDP_FLAGS_REPLACE))
153     {
154       args->rv = VNET_API_ERROR_SYSCALL_ERROR_6;
155       args->error =
156         clib_error_return_unix (0, "bpf_set_link_xdp_fd(%s) failed",
157                                 ad->linux_ifname);
158       goto err1;
159     }
160
161   return 0;
162
163 err1:
164   bpf_object__unload (ad->bpf_obj);
165   ad->bpf_obj = 0;
166 err0:
167   ad->linux_ifindex = ~0;
168   return -1;
169 }
170
171 static int
172 af_xdp_create_queue (vlib_main_t * vm, af_xdp_create_if_args_t * args,
173                      af_xdp_device_t * ad, int qid, int rxq_num, int txq_num)
174 {
175   struct xsk_umem **umem = vec_elt_at_index (ad->umem, qid);
176   struct xsk_socket **xsk = vec_elt_at_index (ad->xsk, qid);
177   af_xdp_rxq_t *rxq = vec_elt_at_index (ad->rxqs, qid);
178   af_xdp_txq_t *txq = vec_elt_at_index (ad->txqs, qid);
179   struct xsk_umem_config umem_config;
180   struct xsk_socket_config sock_config;
181   struct xdp_options opt;
182   socklen_t optlen;
183   /*
184    * fq and cq must always be allocated even if unused
185    * whereas rx and tx indicates whether we want rxq, txq, or both
186    */
187   struct xsk_ring_cons *rx = qid < rxq_num ? &rxq->rx : 0;
188   struct xsk_ring_prod *fq = &rxq->fq;
189   struct xsk_ring_prod *tx = qid < txq_num ? &txq->tx : 0;
190   struct xsk_ring_cons *cq = &txq->cq;
191   int fd;
192
193   memset (&umem_config, 0, sizeof (umem_config));
194   umem_config.fill_size = args->rxq_size;
195   umem_config.comp_size = args->txq_size;
196   umem_config.frame_size =
197     sizeof (vlib_buffer_t) + vlib_buffer_get_default_data_size (vm);
198   umem_config.flags = XDP_UMEM_UNALIGNED_CHUNK_FLAG;
199   if (xsk_umem__create
200       (umem, uword_to_pointer (vm->buffer_main->buffer_mem_start, void *),
201        vm->buffer_main->buffer_mem_size, fq, cq, &umem_config))
202     {
203       args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
204       args->error = clib_error_return_unix (0, "xsk_umem__create() failed");
205       goto err0;
206     }
207
208   memset (&sock_config, 0, sizeof (sock_config));
209   sock_config.rx_size = args->rxq_size;
210   sock_config.tx_size = args->txq_size;
211   sock_config.bind_flags = XDP_USE_NEED_WAKEUP;
212   switch (args->mode)
213     {
214     case AF_XDP_MODE_AUTO:
215       break;
216     case AF_XDP_MODE_COPY:
217       sock_config.bind_flags |= XDP_COPY;
218       break;
219     case AF_XDP_MODE_ZERO_COPY:
220       sock_config.bind_flags |= XDP_ZEROCOPY;
221       break;
222     }
223   if (xsk_socket__create
224       (xsk, ad->linux_ifname, qid, *umem, rx, tx, &sock_config))
225     {
226       args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
227       args->error =
228         clib_error_return_unix (0,
229                                 "xsk_socket__create() failed (is linux netdev %s up?)",
230                                 ad->linux_ifname);
231       goto err1;
232     }
233
234   fd = xsk_socket__fd (*xsk);
235   optlen = sizeof (opt);
236   if (getsockopt (fd, SOL_XDP, XDP_OPTIONS, &opt, &optlen))
237     {
238       args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
239       args->error =
240         clib_error_return_unix (0, "getsockopt(XDP_OPTIONS) failed");
241       goto err2;
242     }
243   if (opt.flags & XDP_OPTIONS_ZEROCOPY)
244     ad->flags |= AF_XDP_DEVICE_F_ZEROCOPY;
245
246   rxq->xsk_fd = qid < rxq_num ? fd : -1;
247   txq->xsk_fd = qid < txq_num ? fd : -1;
248
249   return 0;
250
251 err2:
252   xsk_socket__delete (*xsk);
253 err1:
254   xsk_umem__delete (*umem);
255 err0:
256   *umem = 0;
257   *xsk = 0;
258   return -1;
259 }
260
261 static int
262 af_xdp_get_numa (const char *ifname)
263 {
264   char *path;
265   clib_error_t *err;
266   int numa;
267
268   path =
269     (char *) format (0, "/sys/class/net/%s/device/numa_node%c", ifname, 0);
270   err = clib_sysfs_read (path, "%d", &numa);
271   if (err || numa < 0)
272     numa = 0;
273
274   clib_error_free (err);
275   vec_free (path);
276   return numa;
277 }
278
279 static clib_error_t *
280 af_xdp_device_rxq_read_ready (clib_file_t * f)
281 {
282   vnet_main_t *vnm = vnet_get_main ();
283   const af_xdp_main_t *am = &af_xdp_main;
284   const u32 dev_instance = f->private_data >> 16;
285   const u16 qid = f->private_data & 0xffff;
286   const af_xdp_device_t *ad = vec_elt_at_index (am->devices, dev_instance);
287   vnet_device_input_set_interrupt_pending (vnm, ad->hw_if_index, qid);
288   return 0;
289 }
290
291 void
292 af_xdp_create_if (vlib_main_t * vm, af_xdp_create_if_args_t * args)
293 {
294   vnet_main_t *vnm = vnet_get_main ();
295   vlib_thread_main_t *tm = vlib_get_thread_main ();
296   af_xdp_main_t *am = &af_xdp_main;
297   af_xdp_device_t *ad;
298   vnet_sw_interface_t *sw;
299   vnet_hw_interface_t *hw;
300   int rxq_num, txq_num, q_num;
301   int i;
302
303   args->rxq_size = args->rxq_size ? args->rxq_size : 2 * VLIB_FRAME_SIZE;
304   args->txq_size = args->txq_size ? args->txq_size : 2 * VLIB_FRAME_SIZE;
305   rxq_num = args->rxq_num ? args->rxq_num : 1;
306   txq_num = tm->n_vlib_mains;
307
308   if (!args->linux_ifname)
309     {
310       args->rv = VNET_API_ERROR_INVALID_VALUE;
311       args->error = clib_error_return (0, "missing host interface");
312       goto err0;
313     }
314
315   if (args->rxq_size < VLIB_FRAME_SIZE || args->txq_size < VLIB_FRAME_SIZE ||
316       args->rxq_size > 65535 || args->txq_size > 65535 ||
317       !is_pow2 (args->rxq_size) || !is_pow2 (args->txq_size))
318     {
319       args->rv = VNET_API_ERROR_INVALID_VALUE;
320       args->error =
321         clib_error_return (0,
322                            "queue size must be a power of two between %i and 65535",
323                            VLIB_FRAME_SIZE);
324       goto err0;
325     }
326
327   pool_get_zero (am->devices, ad);
328
329   ad->linux_ifname = (char *) format (0, "%s", args->linux_ifname);
330   vec_validate (ad->linux_ifname, IFNAMSIZ - 1);        /* libbpf expects ifname to be at least IFNAMSIZ */
331
332   if (args->prog && af_xdp_load_program (args, ad))
333     goto err1;
334
335   q_num = clib_max (rxq_num, txq_num);
336   vec_validate_aligned (ad->rxqs, q_num - 1, CLIB_CACHE_LINE_BYTES);
337   vec_validate_aligned (ad->txqs, q_num - 1, CLIB_CACHE_LINE_BYTES);
338   vec_validate_aligned (ad->umem, q_num - 1, CLIB_CACHE_LINE_BYTES);
339   vec_validate_aligned (ad->xsk, q_num - 1, CLIB_CACHE_LINE_BYTES);
340   ad->txq_num = txq_num;
341   for (i = 0; i < q_num; i++)
342     {
343       if (af_xdp_create_queue (vm, args, ad, i, rxq_num, txq_num))
344         {
345           /*
346            * queue creation failed
347            * it is only a fatal error if we could not create the number of rx
348            * queues requested explicitely by the user
349            * we might create less tx queues than workers but this is ok
350            */
351           af_xdp_txq_t *txq;
352
353           /* fixup vectors length */
354           vec_set_len (ad->umem, i);
355           vec_set_len (ad->xsk, i);
356           vec_set_len (ad->rxqs, i);
357           vec_set_len (ad->txqs, i);
358
359           if (i < rxq_num)
360             goto err1;          /* failed creating requested rxq: fatal error, bailing out */
361
362           /*
363            * we created all rxq but failed some txq: not an error but
364            * initialize lock for shared txq
365            */
366           ad->txq_num = i;
367           vec_foreach (txq, ad->txqs) clib_spinlock_init (&txq->lock);
368           args->rv = 0;
369           clib_error_free (args->error);
370           break;
371         }
372     }
373
374   ad->dev_instance = ad - am->devices;
375   ad->per_interface_next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
376   ad->pool =
377     vlib_buffer_pool_get_default_for_numa (vm,
378                                            af_xdp_get_numa
379                                            (ad->linux_ifname));
380   if (!args->name)
381     ad->name =
382       (char *) format (0, "%s/%d", ad->linux_ifname, ad->dev_instance);
383   else
384     ad->name = (char *) format (0, "%s", args->name);
385
386   ethernet_mac_address_generate (ad->hwaddr);
387
388   /* create interface */
389   if (ethernet_register_interface (vnm, af_xdp_device_class.index,
390                                    ad->dev_instance, ad->hwaddr,
391                                    &ad->hw_if_index, af_xdp_flag_change))
392     {
393       args->rv = VNET_API_ERROR_INVALID_INTERFACE;
394       args->error =
395         clib_error_return (0, "ethernet_register_interface() failed");
396       goto err1;
397     }
398
399   sw = vnet_get_hw_sw_interface (vnm, ad->hw_if_index);
400   hw = vnet_get_hw_interface (vnm, ad->hw_if_index);
401   args->sw_if_index = ad->sw_if_index = sw->sw_if_index;
402   hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
403
404   vnet_hw_interface_set_input_node (vnm, ad->hw_if_index,
405                                     af_xdp_input_node.index);
406
407   for (i = 0; i < rxq_num; i++)
408     {
409       af_xdp_rxq_t *rxq = vec_elt_at_index (ad->rxqs, i);
410       clib_file_t f = {
411         .file_descriptor = rxq->xsk_fd,
412         .flags = UNIX_FILE_EVENT_EDGE_TRIGGERED,
413         .private_data = (uword) ad->dev_instance << 16 | (uword) i,
414         .read_function = af_xdp_device_rxq_read_ready,
415         .description =
416           format (0, "%U rxq %d", format_af_xdp_device_name, ad->dev_instance,
417                   i),
418       };
419       rxq->file_index = clib_file_add (&file_main, &f);
420       vnet_hw_interface_assign_rx_thread (vnm, ad->hw_if_index, i, ~0);
421     }
422
423   /* buffer template */
424   vec_validate_aligned (ad->buffer_template, 1, CLIB_CACHE_LINE_BYTES);
425   ad->buffer_template->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
426   ad->buffer_template->ref_count = 1;
427   vnet_buffer (ad->buffer_template)->sw_if_index[VLIB_RX] = ad->sw_if_index;
428   vnet_buffer (ad->buffer_template)->sw_if_index[VLIB_TX] = (u32) ~ 0;
429   ad->buffer_template->buffer_pool_index = ad->pool;
430
431   return;
432
433 err1:
434   af_xdp_delete_if (vm, ad);
435 err0:
436   vlib_log_err (am->log_class, "%U", format_clib_error, args->error);
437 }
438
439 static clib_error_t *
440 af_xdp_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
441 {
442   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
443   af_xdp_main_t *am = &af_xdp_main;
444   af_xdp_device_t *ad = vec_elt_at_index (am->devices, hi->dev_instance);
445   uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
446
447   if (ad->flags & AF_XDP_DEVICE_F_ERROR)
448     return clib_error_return (0, "device is in error state");
449
450   if (is_up)
451     {
452       vnet_hw_interface_set_flags (vnm, ad->hw_if_index,
453                                    VNET_HW_INTERFACE_FLAG_LINK_UP);
454       ad->flags |= AF_XDP_DEVICE_F_ADMIN_UP;
455     }
456   else
457     {
458       vnet_hw_interface_set_flags (vnm, ad->hw_if_index, 0);
459       ad->flags &= ~AF_XDP_DEVICE_F_ADMIN_UP;
460     }
461   return 0;
462 }
463
464 static void
465 af_xdp_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
466                                 u32 node_index)
467 {
468   af_xdp_main_t *am = &af_xdp_main;
469   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
470   af_xdp_device_t *ad = pool_elt_at_index (am->devices, hw->dev_instance);
471
472   /* Shut off redirection */
473   if (node_index == ~0)
474     {
475       ad->per_interface_next_index = node_index;
476       return;
477     }
478
479   ad->per_interface_next_index =
480     vlib_node_add_next (vlib_get_main (), af_xdp_input_node.index,
481                         node_index);
482 }
483
484 static char *af_xdp_tx_func_error_strings[] = {
485 #define _(n,s) s,
486   foreach_af_xdp_tx_func_error
487 #undef _
488 };
489
490 static void
491 af_xdp_clear (u32 dev_instance)
492 {
493   af_xdp_main_t *am = &af_xdp_main;
494   af_xdp_device_t *ad = pool_elt_at_index (am->devices, dev_instance);
495   clib_error_free (ad->error);
496 }
497
498 /* *INDENT-OFF* */
499 VNET_DEVICE_CLASS (af_xdp_device_class) =
500 {
501   .name = "AF_XDP interface",
502   .format_device = format_af_xdp_device,
503   .format_device_name = format_af_xdp_device_name,
504   .admin_up_down_function = af_xdp_interface_admin_up_down,
505   .rx_redirect_to_node = af_xdp_set_interface_next_node,
506   .tx_function_n_errors = AF_XDP_TX_N_ERROR,
507   .tx_function_error_strings = af_xdp_tx_func_error_strings,
508   .mac_addr_change_function = af_xdp_mac_change,
509   .clear_counters = af_xdp_clear,
510 };
511 /* *INDENT-ON* */
512
513 clib_error_t *
514 af_xdp_init (vlib_main_t * vm)
515 {
516   af_xdp_main_t *am = &af_xdp_main;
517
518   am->log_class = vlib_log_register_class ("af_xdp", 0);
519
520   return 0;
521 }
522
523 VLIB_INIT_FUNCTION (af_xdp_init);
524
525 /*
526  * fd.io coding-style-patch-verification: ON
527  *
528  * Local Variables:
529  * eval: (c-set-style "gnu")
530  * End:
531  */