c941d3763cf9db771a0185b18a681152214da0a9
[vpp.git] / src / vnet / devices / af_packet / af_packet.c
1 /*
2  *------------------------------------------------------------------
3  * af_packet.c - linux kernel packet interface
4  *
5  * Copyright (c) 2016 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19
20 #include <linux/if_ether.h>
21 #include <linux/if_packet.h>
22 #include <sys/ioctl.h>
23 #include <net/if.h>
24 #include <dirent.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <fcntl.h>
28
29 #include <vppinfra/linux/sysfs.h>
30 #include <vlib/vlib.h>
31 #include <vlib/unix/unix.h>
32 #include <vnet/ip/ip.h>
33 #include <vnet/devices/netlink.h>
34 #include <vnet/ethernet/ethernet.h>
35 #include <vnet/interface/rx_queue_funcs.h>
36 #include <vnet/interface/tx_queue_funcs.h>
37
38 #include <vnet/devices/af_packet/af_packet.h>
39
40 af_packet_main_t af_packet_main;
41
42 VNET_HW_INTERFACE_CLASS (af_packet_ip_device_hw_interface_class, static) = {
43   .name = "af-packet-ip-device",
44   .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
45 };
46
47 #define AF_PACKET_DEFAULT_TX_FRAMES_PER_BLOCK 1024
48 #define AF_PACKET_DEFAULT_TX_FRAME_SIZE       (2048 * 33) // GSO packet of 64KB
49 #define AF_PACKET_TX_BLOCK_NR           1
50
51 #define AF_PACKET_DEFAULT_RX_FRAMES_PER_BLOCK 32
52 #define AF_PACKET_DEFAULT_RX_FRAME_SIZE       2048
53 #define AF_PACKET_RX_BLOCK_NR                 160
54
55 /*defined in net/if.h but clashes with dpdk headers */
56 unsigned int if_nametoindex (const char *ifname);
57
58 static clib_error_t *
59 af_packet_eth_set_max_frame_size (vnet_main_t *vnm, vnet_hw_interface_t *hi,
60                                   u32 frame_size)
61 {
62   clib_error_t *error, *rv;
63   af_packet_main_t *apm = &af_packet_main;
64   af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, hi->dev_instance);
65
66   error = vnet_netlink_set_link_mtu (apif->host_if_index,
67                                      frame_size + hi->frame_overhead);
68
69   if (error)
70     {
71       vlib_log_err (apm->log_class, "netlink failed to change MTU: %U",
72                     format_clib_error, error);
73       rv = vnet_error (VNET_ERR_SYSCALL_ERROR_1, "netlink error: %U",
74                        format_clib_error, error);
75       clib_error_free (error);
76       return rv;
77     }
78   else
79     apif->host_mtu = frame_size + hi->frame_overhead;
80   return 0;
81 }
82
83 static int
84 af_packet_read_mtu (af_packet_if_t *apif)
85 {
86   af_packet_main_t *apm = &af_packet_main;
87   clib_error_t *error;
88   error = vnet_netlink_get_link_mtu (apif->host_if_index, &apif->host_mtu);
89   if (error)
90     {
91       vlib_log_err (apm->log_class, "netlink failed to get MTU: %U",
92                     format_clib_error, error);
93       clib_error_free (error);
94       return VNET_API_ERROR_SYSCALL_ERROR_1;
95     }
96   return 0;
97 }
98
99 static clib_error_t *
100 af_packet_fd_read_ready (clib_file_t * uf)
101 {
102   vnet_main_t *vnm = vnet_get_main ();
103
104   /* Schedule the rx node */
105   vnet_hw_if_rx_queue_set_int_pending (vnm, uf->private_data);
106   return 0;
107 }
108
109 static int
110 is_bridge (const u8 * host_if_name)
111 {
112   u8 *s;
113   DIR *dir = NULL;
114
115   s = format (0, "/sys/class/net/%s/bridge%c", host_if_name, 0);
116   dir = opendir ((char *) s);
117   vec_free (s);
118
119   if (dir)
120     {
121       closedir (dir);
122       return 0;
123     }
124
125   return -1;
126 }
127
128 static void
129 af_packet_set_rx_queues (vlib_main_t *vm, af_packet_if_t *apif)
130 {
131   vnet_main_t *vnm = vnet_get_main ();
132   af_packet_queue_t *rx_queue;
133
134   vnet_hw_if_set_input_node (vnm, apif->hw_if_index,
135                              af_packet_input_node.index);
136
137   vec_foreach (rx_queue, apif->rx_queues)
138     {
139       rx_queue->queue_index = vnet_hw_if_register_rx_queue (
140         vnm, apif->hw_if_index, rx_queue->queue_id, VNET_HW_IF_RXQ_THREAD_ANY);
141
142       {
143         clib_file_t template = { 0 };
144         template.read_function = af_packet_fd_read_ready;
145         template.file_descriptor = rx_queue->fd;
146         template.private_data = rx_queue->queue_index;
147         template.flags = UNIX_FILE_EVENT_EDGE_TRIGGERED;
148         template.description =
149           format (0, "%U queue %u", format_af_packet_device_name,
150                   apif->dev_instance, rx_queue->queue_id);
151         rx_queue->clib_file_index = clib_file_add (&file_main, &template);
152       }
153       vnet_hw_if_set_rx_queue_file_index (vnm, rx_queue->queue_index,
154                                           rx_queue->clib_file_index);
155       vnet_hw_if_set_rx_queue_mode (vnm, rx_queue->queue_index,
156                                     VNET_HW_IF_RX_MODE_INTERRUPT);
157       rx_queue->mode = VNET_HW_IF_RX_MODE_INTERRUPT;
158     }
159   vnet_hw_if_update_runtime_data (vnm, apif->hw_if_index);
160 }
161
162 static void
163 af_packet_set_tx_queues (vlib_main_t *vm, af_packet_if_t *apif)
164 {
165   vnet_main_t *vnm = vnet_get_main ();
166   af_packet_main_t *apm = &af_packet_main;
167   af_packet_queue_t *tx_queue;
168
169   vec_foreach (tx_queue, apif->tx_queues)
170     {
171       tx_queue->queue_index = vnet_hw_if_register_tx_queue (
172         vnm, apif->hw_if_index, tx_queue->queue_id);
173     }
174
175   if (apif->num_txqs == 0)
176     {
177       vlib_log_err (apm->log_class, "Interface %U has 0 txq",
178                     format_vnet_hw_if_index_name, vnm, apif->hw_if_index);
179       return;
180     }
181
182   for (u32 j = 0; j < vlib_get_n_threads (); j++)
183     {
184       u32 qi = apif->tx_queues[j % apif->num_txqs].queue_index;
185       vnet_hw_if_tx_queue_assign_thread (vnm, qi, j);
186     }
187
188   vnet_hw_if_update_runtime_data (vnm, apif->hw_if_index);
189 }
190
191 static int
192 create_packet_v3_sock (int host_if_index, tpacket_req3_t *rx_req,
193                        tpacket_req3_t *tx_req, int *fd, af_packet_ring_t *ring,
194                        u8 *is_cksum_gso_enabled, u32 fanout_id, u8 is_fanout)
195 {
196   af_packet_main_t *apm = &af_packet_main;
197   struct sockaddr_ll sll;
198   socklen_t req_sz = sizeof (tpacket_req3_t);
199   int ret;
200   int ver = TPACKET_V3;
201   u32 ring_sz = 0;
202
203   if (rx_req)
204     ring_sz += rx_req->tp_block_size * rx_req->tp_block_nr;
205
206   if (tx_req)
207     ring_sz += tx_req->tp_block_size * tx_req->tp_block_nr;
208
209   if ((*fd = socket (AF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0)
210     {
211       vlib_log_err (apm->log_class,
212                     "Failed to create AF_PACKET socket: %s (errno %d)",
213                     strerror (errno), errno);
214       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
215       goto error;
216     }
217
218   /* bind before rx ring is cfged so we don't receive packets from other interfaces */
219   clib_memset (&sll, 0, sizeof (sll));
220   sll.sll_family = PF_PACKET;
221   sll.sll_protocol = htons (ETH_P_ALL);
222   sll.sll_ifindex = host_if_index;
223   if (bind (*fd, (struct sockaddr *) &sll, sizeof (sll)) < 0)
224     {
225       vlib_log_err (apm->log_class,
226                     "Failed to bind rx packet socket: %s (errno %d)",
227                     strerror (errno), errno);
228       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
229       goto error;
230     }
231
232   if (setsockopt (*fd, SOL_PACKET, PACKET_VERSION, &ver, sizeof (ver)) < 0)
233     {
234       vlib_log_err (apm->log_class,
235                     "Failed to set rx packet interface version: %s (errno %d)",
236                     strerror (errno), errno);
237       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
238       goto error;
239     }
240
241   int opt = 1;
242   if (setsockopt (*fd, SOL_PACKET, PACKET_LOSS, &opt, sizeof (opt)) < 0)
243     {
244       vlib_log_err (
245         apm->log_class,
246         "Failed to set packet tx ring error handling option: %s (errno %d)",
247         strerror (errno), errno);
248       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
249       goto error;
250     }
251
252   int opt2 = 1;
253   if (setsockopt (*fd, SOL_PACKET, PACKET_VNET_HDR, &opt2, sizeof (opt2)) < 0)
254     {
255       vlib_log_debug (
256         apm->log_class,
257         "Failed to set packet vnet hdr error handling option: %s (errno %d)",
258         strerror (errno), errno);
259     }
260   else
261     *is_cksum_gso_enabled = 1;
262
263 #if defined(PACKET_QDISC_BYPASS)
264   /* Introduced with Linux 3.14 so the ifdef should eventually be removed  */
265   if (setsockopt (*fd, SOL_PACKET, PACKET_QDISC_BYPASS, &opt, sizeof (opt)) <
266       0)
267     {
268       vlib_log_debug (apm->log_class,
269                       "Failed to set qdisc bypass error "
270                       "handling option: %s (errno %d)",
271                       strerror (errno), errno);
272     }
273 #endif
274
275   if (is_fanout)
276     {
277       int fanout = ((fanout_id & 0xffff) | ((PACKET_FANOUT_HASH) << 16));
278       if (setsockopt (*fd, SOL_PACKET, PACKET_FANOUT, &fanout,
279                       sizeof (fanout)) < 0)
280         {
281           vlib_log_err (apm->log_class,
282                         "Failed to set fanout options: %s (errno %d)",
283                         strerror (errno), errno);
284           ret = VNET_API_ERROR_SYSCALL_ERROR_1;
285           goto error;
286         }
287     }
288
289   if (rx_req)
290     if (setsockopt (*fd, SOL_PACKET, PACKET_RX_RING, rx_req, req_sz) < 0)
291       {
292         vlib_log_err (apm->log_class,
293                       "Failed to set packet rx ring options: %s (errno %d)",
294                       strerror (errno), errno);
295         ret = VNET_API_ERROR_SYSCALL_ERROR_1;
296         goto error;
297       }
298
299   if (tx_req)
300     if (setsockopt (*fd, SOL_PACKET, PACKET_TX_RING, tx_req, req_sz) < 0)
301       {
302         vlib_log_err (apm->log_class,
303                       "Failed to set packet tx ring options: %s (errno %d)",
304                       strerror (errno), errno);
305         ret = VNET_API_ERROR_SYSCALL_ERROR_1;
306         goto error;
307       }
308
309   ring->ring_start_addr = mmap (NULL, ring_sz, PROT_READ | PROT_WRITE,
310                                 MAP_SHARED | MAP_LOCKED, *fd, 0);
311   if (ring->ring_start_addr == MAP_FAILED)
312     {
313       vlib_log_err (apm->log_class, "mmap failure: %s (errno %d)",
314                     strerror (errno), errno);
315       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
316       goto error;
317     }
318
319   ring->ring_size = ring_sz;
320
321   return 0;
322 error:
323   if (*fd >= 0)
324     {
325       close (*fd);
326       *fd = -1;
327     }
328   return ret;
329 }
330
331 int
332 af_packet_queue_init (vlib_main_t *vm, af_packet_if_t *apif,
333                       af_packet_create_if_arg_t *arg,
334                       af_packet_queue_t *rx_queue, af_packet_queue_t *tx_queue,
335                       u8 queue_id, u8 is_fanout)
336 {
337   af_packet_main_t *apm = &af_packet_main;
338   tpacket_req3_t *rx_req = 0;
339   tpacket_req3_t *tx_req = 0;
340   int ret, fd = -1;
341   af_packet_ring_t ring = { 0 };
342   u8 *ring_addr = 0;
343   u32 rx_frames_per_block, tx_frames_per_block;
344   u32 rx_frame_size, tx_frame_size;
345   u32 i = 0;
346   u8 is_cksum_gso_enabled = 0;
347
348   if (rx_queue)
349     {
350       rx_frames_per_block = arg->rx_frames_per_block ?
351                                     arg->rx_frames_per_block :
352                                     AF_PACKET_DEFAULT_RX_FRAMES_PER_BLOCK;
353
354       rx_frame_size = arg->rx_frame_size ? arg->rx_frame_size :
355                                                  AF_PACKET_DEFAULT_RX_FRAME_SIZE;
356       vec_validate (rx_queue->rx_req, 0);
357       rx_queue->rx_req->tp_block_size = rx_frame_size * rx_frames_per_block;
358       rx_queue->rx_req->tp_frame_size = rx_frame_size;
359       rx_queue->rx_req->tp_block_nr = AF_PACKET_RX_BLOCK_NR;
360       rx_queue->rx_req->tp_frame_nr =
361         AF_PACKET_RX_BLOCK_NR * rx_frames_per_block;
362       rx_queue->rx_req->tp_retire_blk_tov = 1; // 1 ms block timout
363       rx_queue->rx_req->tp_feature_req_word = 0;
364       rx_queue->rx_req->tp_sizeof_priv = 0;
365       rx_req = rx_queue->rx_req;
366     }
367
368   if (tx_queue)
369     {
370       tx_frames_per_block = arg->tx_frames_per_block ?
371                                     arg->tx_frames_per_block :
372                                     AF_PACKET_DEFAULT_TX_FRAMES_PER_BLOCK;
373       tx_frame_size = arg->tx_frame_size ? arg->tx_frame_size :
374                                                  AF_PACKET_DEFAULT_TX_FRAME_SIZE;
375
376       vec_validate (tx_queue->tx_req, 0);
377       tx_queue->tx_req->tp_block_size = tx_frame_size * tx_frames_per_block;
378       tx_queue->tx_req->tp_frame_size = tx_frame_size;
379       tx_queue->tx_req->tp_block_nr = AF_PACKET_TX_BLOCK_NR;
380       tx_queue->tx_req->tp_frame_nr =
381         AF_PACKET_TX_BLOCK_NR * tx_frames_per_block;
382       tx_queue->tx_req->tp_retire_blk_tov = 0;
383       tx_queue->tx_req->tp_sizeof_priv = 0;
384       tx_queue->tx_req->tp_feature_req_word = 0;
385       tx_req = tx_queue->tx_req;
386     }
387
388   if (rx_queue || tx_queue)
389     {
390       ret = create_packet_v3_sock (apif->host_if_index, rx_req, tx_req, &fd,
391                                    &ring, &is_cksum_gso_enabled,
392                                    apif->dev_instance, is_fanout);
393
394       if (ret != 0)
395         goto error;
396
397       vec_add1 (apif->rings, ring);
398       ring_addr = ring.ring_start_addr;
399     }
400
401   if (rx_queue)
402     {
403       rx_queue->fd = fd;
404       vec_validate (rx_queue->rx_ring, rx_queue->rx_req->tp_block_nr - 1);
405       vec_foreach_index (i, rx_queue->rx_ring)
406         {
407           rx_queue->rx_ring[i] =
408             ring_addr + i * rx_queue->rx_req->tp_block_size;
409         }
410
411       rx_queue->next_rx_block = 0;
412       rx_queue->queue_id = queue_id;
413       rx_queue->is_rx_pending = 0;
414       ring_addr = ring_addr + rx_queue->rx_req->tp_block_size *
415                                 rx_queue->rx_req->tp_block_nr;
416     }
417
418   if (tx_queue)
419     {
420       tx_queue->fd = fd;
421       vec_validate (tx_queue->tx_ring, tx_queue->tx_req->tp_block_nr - 1);
422       vec_foreach_index (i, tx_queue->tx_ring)
423         {
424           tx_queue->tx_ring[i] =
425             ring_addr + i * tx_queue->tx_req->tp_block_size;
426         }
427
428       tx_queue->next_tx_frame = 0;
429       tx_queue->queue_id = queue_id;
430       clib_spinlock_init (&tx_queue->lockp);
431     }
432
433   if (queue_id == 0)
434     apif->is_cksum_gso_enabled = is_cksum_gso_enabled;
435
436   return 0;
437 error:
438   vlib_log_err (apm->log_class, "Failed to set queue %u error", queue_id);
439   if (rx_queue)
440     vec_free (rx_queue->rx_req);
441   if (tx_queue)
442     vec_free (tx_queue->tx_req);
443   return ret;
444 }
445
446 int
447 af_packet_device_init (vlib_main_t *vm, af_packet_if_t *apif,
448                        af_packet_create_if_arg_t *args)
449 {
450   af_packet_main_t *apm = &af_packet_main;
451   af_packet_queue_t *rx_queue = 0;
452   af_packet_queue_t *tx_queue = 0;
453   u16 nq = clib_min (args->num_rxqs, args->num_txqs);
454   u16 i = 0;
455   int ret = 0;
456   u8 is_fanout = (args->num_rxqs > 1) ? 1 : 0;
457
458   vec_validate (apif->rx_queues, args->num_rxqs - 1);
459   vec_validate (apif->tx_queues, args->num_txqs - 1);
460
461   for (; i < nq; i++)
462     {
463       rx_queue = vec_elt_at_index (apif->rx_queues, i);
464       tx_queue = vec_elt_at_index (apif->tx_queues, i);
465       ret = af_packet_queue_init (vm, apif, args, rx_queue, tx_queue, i,
466                                   is_fanout);
467       if (ret != 0)
468         goto error;
469     }
470
471   if (args->num_rxqs > args->num_txqs)
472     {
473       for (; i < args->num_rxqs; i++)
474         {
475           rx_queue = vec_elt_at_index (apif->rx_queues, i);
476           ret =
477             af_packet_queue_init (vm, apif, args, rx_queue, 0, i, is_fanout);
478           if (ret != 0)
479             goto error;
480         }
481     }
482   else if (args->num_txqs > args->num_rxqs)
483     {
484       for (; i < args->num_txqs; i++)
485         {
486           tx_queue = vec_elt_at_index (apif->tx_queues, i);
487           ret = af_packet_queue_init (vm, apif, args, 0, tx_queue, i, 0);
488           if (ret != 0)
489             goto error;
490         }
491     }
492
493   apif->num_rxqs = args->num_rxqs;
494   apif->num_txqs = args->num_txqs;
495
496   return 0;
497 error:
498   vlib_log_err (apm->log_class, "Failed to init device error");
499   return ret;
500 }
501
502 int
503 af_packet_create_if (af_packet_create_if_arg_t *arg)
504 {
505   af_packet_main_t *apm = &af_packet_main;
506   vlib_main_t *vm = vlib_get_main ();
507   int fd2 = -1;
508   struct ifreq ifr;
509   af_packet_if_t *apif = 0;
510   u8 hw_addr[6];
511   vnet_sw_interface_t *sw;
512   vnet_main_t *vnm = vnet_get_main ();
513   vnet_hw_if_caps_t caps = VNET_HW_IF_CAP_INT_MODE;
514   uword *p;
515   uword if_index;
516   u8 *host_if_name_dup = 0;
517   int host_if_index = -1;
518   int ret = 0;
519
520   p = mhash_get (&apm->if_index_by_host_if_name, arg->host_if_name);
521   if (p)
522     {
523       apif = vec_elt_at_index (apm->interfaces, p[0]);
524       arg->sw_if_index = apif->sw_if_index;
525       return VNET_API_ERROR_IF_ALREADY_EXISTS;
526     }
527
528   host_if_name_dup = vec_dup (arg->host_if_name);
529
530   /*
531    * make sure host side of interface is 'UP' before binding AF_PACKET
532    * socket on it.
533    */
534   if ((fd2 = socket (AF_UNIX, SOCK_DGRAM, 0)) < 0)
535     {
536       vlib_log_debug (apm->log_class,
537                       "Failed to create AF_UNIX socket: %s (errno %d)",
538                       strerror (errno), errno);
539       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
540       goto error;
541     }
542
543   clib_memcpy (ifr.ifr_name, (const char *) arg->host_if_name,
544                vec_len (arg->host_if_name));
545   if (ioctl (fd2, SIOCGIFINDEX, &ifr) < 0)
546     {
547       vlib_log_debug (
548         apm->log_class,
549         "Failed to retrieve the interface (%s) index: %s (errno %d)",
550         arg->host_if_name, strerror (errno), errno);
551       ret = VNET_API_ERROR_INVALID_INTERFACE;
552       goto error;
553     }
554
555   host_if_index = ifr.ifr_ifindex;
556   if (ioctl (fd2, SIOCGIFFLAGS, &ifr) < 0)
557     {
558       vlib_log_debug (apm->log_class,
559                       "Failed to get the active flag: %s (errno %d)",
560                       strerror (errno), errno);
561       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
562       goto error;
563     }
564
565   if (!(ifr.ifr_flags & IFF_UP))
566     {
567       ifr.ifr_flags |= IFF_UP;
568       if (ioctl (fd2, SIOCSIFFLAGS, &ifr) < 0)
569         {
570           vlib_log_debug (apm->log_class,
571                           "Failed to set the active flag: %s (errno %d)",
572                           strerror (errno), errno);
573           ret = VNET_API_ERROR_SYSCALL_ERROR_1;
574           goto error;
575         }
576     }
577
578   if (fd2 > -1)
579     {
580       close (fd2);
581       fd2 = -1;
582     }
583
584   ret = is_bridge (arg->host_if_name);
585   if (ret == 0)                 /* is a bridge, ignore state */
586     host_if_index = -1;
587
588   /* So far everything looks good, let's create interface */
589   pool_get (apm->interfaces, apif);
590   if_index = apif - apm->interfaces;
591
592   apif->dev_instance = if_index;
593   apif->host_if_index = host_if_index;
594   apif->host_if_name = host_if_name_dup;
595   apif->per_interface_next_index = ~0;
596   apif->mode = arg->mode;
597
598   ret = af_packet_device_init (vm, apif, arg);
599   if (ret != 0)
600     goto error;
601
602   ret = af_packet_read_mtu (apif);
603   if (ret != 0)
604     goto error;
605
606
607   if (apif->mode != AF_PACKET_IF_MODE_IP)
608     {
609       vnet_eth_interface_registration_t eir = {};
610       /*use configured or generate random MAC address */
611       if (arg->hw_addr)
612         clib_memcpy (hw_addr, arg->hw_addr, 6);
613       else
614         {
615           f64 now = vlib_time_now (vm);
616           u32 rnd;
617           rnd = (u32) (now * 1e6);
618           rnd = random_u32 (&rnd);
619
620           clib_memcpy (hw_addr + 2, &rnd, sizeof (rnd));
621           hw_addr[0] = 2;
622           hw_addr[1] = 0xfe;
623         }
624
625       eir.dev_class_index = af_packet_device_class.index;
626       eir.dev_instance = apif->dev_instance;
627       eir.address = hw_addr;
628       eir.cb.set_max_frame_size = af_packet_eth_set_max_frame_size;
629       apif->hw_if_index = vnet_eth_register_interface (vnm, &eir);
630     }
631   else
632     {
633       apif->hw_if_index = vnet_register_interface (
634         vnm, af_packet_device_class.index, apif->dev_instance,
635         af_packet_ip_device_hw_interface_class.index, apif->dev_instance);
636     }
637
638   sw = vnet_get_hw_sw_interface (vnm, apif->hw_if_index);
639   apif->sw_if_index = sw->sw_if_index;
640
641   af_packet_set_rx_queues (vm, apif);
642   af_packet_set_tx_queues (vm, apif);
643
644   if (apif->is_cksum_gso_enabled)
645     caps |= VNET_HW_IF_CAP_TCP_GSO | VNET_HW_IF_CAP_TX_IP4_CKSUM |
646             VNET_HW_IF_CAP_TX_TCP_CKSUM | VNET_HW_IF_CAP_TX_UDP_CKSUM;
647
648   vnet_hw_if_set_caps (vnm, apif->hw_if_index, caps);
649   vnet_hw_interface_set_flags (vnm, apif->hw_if_index,
650                                VNET_HW_INTERFACE_FLAG_LINK_UP);
651
652   mhash_set_mem (&apm->if_index_by_host_if_name, host_if_name_dup, &if_index,
653                  0);
654   arg->sw_if_index = apif->sw_if_index;
655
656   return 0;
657
658 error:
659   if (fd2 > -1)
660     {
661       close (fd2);
662       fd2 = -1;
663     }
664   vec_free (host_if_name_dup);
665   memset (apif, 0, sizeof (*apif));
666   pool_put (apm->interfaces, apif);
667   return ret;
668 }
669
670 static int
671 af_packet_rx_queue_free (af_packet_if_t *apif, af_packet_queue_t *rx_queue)
672 {
673   clib_file_del_by_index (&file_main, rx_queue->clib_file_index);
674   close (rx_queue->fd);
675   rx_queue->fd = -1;
676   rx_queue->rx_ring = NULL;
677   vec_free (rx_queue->rx_req);
678   rx_queue->rx_req = NULL;
679   return 0;
680 }
681
682 static int
683 af_packet_tx_queue_free (af_packet_if_t *apif, af_packet_queue_t *tx_queue)
684 {
685   close (tx_queue->fd);
686   tx_queue->fd = -1;
687   clib_spinlock_free (&tx_queue->lockp);
688   tx_queue->tx_ring = NULL;
689   vec_free (tx_queue->tx_req);
690   tx_queue->tx_req = NULL;
691   return 0;
692 }
693
694 static int
695 af_packet_ring_free (af_packet_if_t *apif, af_packet_ring_t *ring)
696 {
697   af_packet_main_t *apm = &af_packet_main;
698
699   if (ring)
700     {
701       // FIXME: unmap the memory
702       if (munmap (ring->ring_start_addr, ring->ring_size))
703         vlib_log_warn (apm->log_class,
704                        "Host interface %s could not free ring %p of size %u",
705                        apif->host_if_name, ring->ring_start_addr,
706                        ring->ring_size);
707       else
708         ring->ring_start_addr = 0;
709     }
710
711   return 0;
712 }
713
714 int
715 af_packet_delete_if (u8 *host_if_name)
716 {
717   vnet_main_t *vnm = vnet_get_main ();
718   af_packet_main_t *apm = &af_packet_main;
719   af_packet_if_t *apif;
720   af_packet_queue_t *rx_queue;
721   af_packet_queue_t *tx_queue;
722   af_packet_ring_t *ring;
723   uword *p;
724
725   p = mhash_get (&apm->if_index_by_host_if_name, host_if_name);
726   if (p == NULL)
727     {
728       vlib_log_warn (apm->log_class, "Host interface %s does not exist",
729                      host_if_name);
730       return VNET_API_ERROR_SYSCALL_ERROR_1;
731     }
732   apif = pool_elt_at_index (apm->interfaces, p[0]);
733
734   /* bring down the interface */
735   vnet_hw_interface_set_flags (vnm, apif->hw_if_index, 0);
736
737   /* clean up */
738   vec_foreach (rx_queue, apif->rx_queues)
739     af_packet_rx_queue_free (apif, rx_queue);
740   vec_foreach (tx_queue, apif->tx_queues)
741     af_packet_tx_queue_free (apif, tx_queue);
742   vec_foreach (ring, apif->rings)
743     af_packet_ring_free (apif, ring);
744
745   vec_free (apif->rx_queues);
746   apif->rx_queues = NULL;
747   vec_free (apif->tx_queues);
748   apif->tx_queues = NULL;
749   vec_free (apif->rings);
750   apif->rings = NULL;
751
752   vec_free (apif->host_if_name);
753   apif->host_if_name = NULL;
754   apif->host_if_index = -1;
755
756   mhash_unset (&apm->if_index_by_host_if_name, host_if_name, p);
757
758   if (apif->mode != AF_PACKET_IF_MODE_IP)
759     ethernet_delete_interface (vnm, apif->hw_if_index);
760   else
761     vnet_delete_hw_interface (vnm, apif->hw_if_index);
762
763   memset (apif, 0, sizeof (*apif));
764   pool_put (apm->interfaces, apif);
765
766   return 0;
767 }
768
769 int
770 af_packet_set_l4_cksum_offload (u32 sw_if_index, u8 set)
771 {
772   // deprecated ...
773   return 0;
774 }
775
776 int
777 af_packet_dump_ifs (af_packet_if_detail_t ** out_af_packet_ifs)
778 {
779   af_packet_main_t *apm = &af_packet_main;
780   af_packet_if_t *apif;
781   af_packet_if_detail_t *r_af_packet_ifs = NULL;
782   af_packet_if_detail_t *af_packet_if = NULL;
783
784   pool_foreach (apif, apm->interfaces)
785      {
786       vec_add2 (r_af_packet_ifs, af_packet_if, 1);
787       af_packet_if->sw_if_index = apif->sw_if_index;
788       if (apif->host_if_name)
789         {
790           clib_memcpy (af_packet_if->host_if_name, apif->host_if_name,
791                        MIN (ARRAY_LEN (af_packet_if->host_if_name) - 1,
792                        strlen ((const char *) apif->host_if_name)));
793         }
794     }
795
796   *out_af_packet_ifs = r_af_packet_ifs;
797
798   return 0;
799 }
800
801 static clib_error_t *
802 af_packet_init (vlib_main_t * vm)
803 {
804   af_packet_main_t *apm = &af_packet_main;
805   vlib_thread_main_t *tm = vlib_get_thread_main ();
806
807   clib_memset (apm, 0, sizeof (af_packet_main_t));
808
809   mhash_init_vec_string (&apm->if_index_by_host_if_name, sizeof (uword));
810
811   vec_validate_aligned (apm->rx_buffers, tm->n_vlib_mains - 1,
812                         CLIB_CACHE_LINE_BYTES);
813
814   apm->log_class = vlib_log_register_class ("af_packet", 0);
815   vlib_log_debug (apm->log_class, "initialized");
816
817   return 0;
818 }
819
820 VLIB_INIT_FUNCTION (af_packet_init);
821
822 /*
823  * fd.io coding-style-patch-verification: ON
824  *
825  * Local Variables:
826  * eval: (c-set-style "gnu")
827  * End:
828  */