aboutsummaryrefslogtreecommitdiffstats
path: root/src/vnet/devices/af_packet/af_packet.c
blob: 5526d72e7a5bdacff4a4d5ba48fd8a2f97051937 (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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
/*
 *------------------------------------------------------------------
 * af_packet.c - linux kernel packet interface
 *
 * Copyright (c) 2016 Cisco and/or its affiliates.
 * 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 <linux/if_ether.h>
#include <linux/if_packet.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>

#include <vppinfra/linux/sysfs.h>
#include <vlib/vlib.h>
#include <vlib/unix/unix.h>
#include <vnet/ip/ip.h>
#include <vnet/devices/netlink.h>
#include <vnet/ethernet/ethernet.h>
#include <vnet/interface/rx_queue_funcs.h>

#include <vnet/devices/af_packet/af_packet.h>

af_packet_main_t af_packet_main;

VNET_HW_INTERFACE_CLASS (af_packet_ip_device_hw_interface_class, static) = {
  .name = "af-packet-ip-device",
  .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
};

#define AF_PACKET_DEFAULT_TX_FRAMES_PER_BLOCK 1024
#define AF_PACKET_DEFAULT_TX_FRAME_SIZE	      (2048 * 33) // GSO packet of 64KB
#define AF_PACKET_TX_BLOCK_NR		1

#define AF_PACKET_DEFAULT_RX_FRAMES_PER_BLOCK 256
#define AF_PACKET_DEFAULT_RX_FRAME_SIZE	      2048
#define AF_PACKET_RX_BLOCK_NR		      20

/*defined in net/if.h but clashes with dpdk headers */
unsigned int if_nametoindex (const char *ifname);

static clib_error_t *
af_packet_eth_set_max_frame_size (vnet_main_t *vnm, vnet_hw_interface_t *hi,
				  u32 frame_size)
{
  clib_error_t *error, *rv;
  af_packet_main_t *apm = &af_packet_main;
  af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, hi->dev_instance);

  error = vnet_netlink_set_link_mtu (apif->host_if_index,
				     frame_size + hi->frame_overhead);

  if (error)
    {
      vlib_log_err (apm->log_class, "netlink failed to change MTU: %U",
		    format_clib_error, error);
      rv = vnet_error (VNET_ERR_SYSCALL_ERROR_1, "netlink error: %U",
		       format_clib_error, error);
      clib_error_free (error);
      return rv;
    }
  else
    apif->host_mtu = frame_size + hi->frame_overhead;
  return 0;
}

static int
af_packet_read_mtu (af_packet_if_t *apif)
{
  af_packet_main_t *apm = &af_packet_main;
  clib_error_t *error;
  error = vnet_netlink_get_link_mtu (apif->host_if_index, &apif->host_mtu);
  if (error)
    {
      vlib_log_err (apm->log_class, "netlink failed to get MTU: %U",
		    format_clib_error, error);
      clib_error_free (error);
      return VNET_API_ERROR_SYSCALL_ERROR_1;
    }
  return 0;
}

static clib_error_t *
af_packet_fd_read_ready (clib_file_t * uf)
{
  af_packet_main_t *apm = &af_packet_main;
  vnet_main_t *vnm = vnet_get_main ();
  u32 idx = uf->private_data;
  af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, idx);

  /* Schedule the rx node */
  vnet_hw_if_rx_queue_set_int_pending (vnm, apif->queue_index);
  return 0;
}

static int
is_bridge (const u8 * host_if_name)
{
  u8 *s;
  DIR *dir = NULL;

  s = format (0, "/sys/class/net/%s/bridge%c", host_if_name, 0);
  dir = opendir ((char *) s);
  vec_free (s);

  if (dir)
    {
      closedir (dir);
      return 0;
    }

  return -1;
}

static int
create_packet_v3_sock (int host_if_index, tpacket_req3_t *rx_req,
		       tpacket_req3_t *tx_req, int *fd, u8 **ring,
		       u32 *hdrlen_ptr, u8 *is_cksum_gso_enabled)
{
  af_packet_main_t *apm = &af_packet_main;
  struct sockaddr_ll sll;
  socklen_t req_sz = sizeof (tpacket_req3_t);
  int ret;
  int ver = TPACKET_V3;
  u32 hdrlen = 0;
  u32 len = sizeof (hdrlen);
  u32 ring_sz = rx_req->tp_block_size * rx_req->tp_block_nr +
    tx_req->tp_block_size * tx_req->tp_block_nr;

  if ((*fd = socket (AF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0)
    {
      vlib_log_debug (apm->log_class,
		      "Failed to create AF_PACKET socket: %s (errno %d)",
		      strerror (errno), errno);
      ret = VNET_API_ERROR_SYSCALL_ERROR_1;
      goto error;
    }

  /* bind before rx ring is cfged so we don't receive packets from other interfaces */
  clib_memset (&sll, 0, sizeof (sll));
  sll.sll_family = PF_PACKET;
  sll.sll_protocol = htons (ETH_P_ALL);
  sll.sll_ifindex = host_if_index;
  if (bind (*fd, (struct sockaddr *) &sll, sizeof (sll)) < 0)
    {
      vlib_log_debug (apm->log_class,
		      "Failed to bind rx packet socket: %s (errno %d)",
		      strerror (errno), errno);
      ret = VNET_API_ERROR_SYSCALL_ERROR_1;
      goto error;
    }

  if (setsockopt (*fd, SOL_PACKET, PACKET_VERSION, &ver, sizeof (ver)) < 0)
    {
      vlib_log_debug (
	apm->log_class,
	"Failed to set rx packet interface version: %s (errno %d)",
	strerror (errno), errno);
      ret = VNET_API_ERROR_SYSCALL_ERROR_1;
      goto error;
    }

  if (getsockopt (*fd, SOL_PACKET, PACKET_HDRLEN, &hdrlen, &len) < 0)
    {
      vlib_log_debug (
	apm->log_class,
	"Failed to get packet hdr len error handling option: %s (errno %d)",
	strerror (errno), errno);
      ret = VNET_API_ERROR_SYSCALL_ERROR_1;
      goto error;
    }
  else
    *hdrlen_ptr = hdrlen;

  int opt = 1;
  if (setsockopt (*fd, SOL_PACKET, PACKET_LOSS, &opt, sizeof (opt)) < 0)
    {
      vlib_log_debug (
	apm->log_class,
	"Failed to set packet tx ring error handling option: %s (errno %d)",
	strerror (errno), errno);
      ret = VNET_API_ERROR_SYSCALL_ERROR_1;
      goto error;
    }

  int opt2 = 1;
  if (setsockopt (*fd, SOL_PACKET, PACKET_VNET_HDR, &opt2, sizeof (opt2)) < 0)
    {
      vlib_log_debug (
	apm->log_class,
	"Failed to set packet vnet hdr error handling option: %s (errno %d)",
	strerror (errno), errno);
    }
  else
    *is_cksum_gso_enabled = 1;

#if defined(PACKET_QDISC_BYPASS)
  /* Introduced with Linux 3.14 so the ifdef should eventually be removed  */
  if (setsockopt (*fd, SOL_PACKET, PACKET_QDISC_BYPASS, &opt, sizeof (opt)) <
      0)
    {
      vlib_log_debug (apm->log_class,
		      "Failed to set qdisc bypass error "
		      "handling option: %s (errno %d)",
		      strerror (errno), errno);
    }
#endif

  if (setsockopt (*fd, SOL_PACKET, PACKET_RX_RING, rx_req, req_sz) < 0)
    {
      vlib_log_debug (apm->log_class,
		      "Failed to set packet rx ring options: %s (errno %d)",
		      strerror (errno), errno);
      ret = VNET_API_ERROR_SYSCALL_ERROR_1;
      goto error;
    }

  if (setsockopt (*fd, SOL_PACKET, PACKET_TX_RING, tx_req, req_sz) < 0)
    {
      vlib_log_debug (apm->log_class,
		      "Failed to set packet tx ring options: %s (errno %d)",
		      strerror (errno), errno);
      ret = VNET_API_ERROR_SYSCALL_ERROR_1;
      goto error;
    }

  *ring = mmap (NULL, ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED,
		*fd, 0);
  if (*ring == MAP_FAILED)
    {
      vlib_log_debug (apm->log_class, "mmap failure: %s (errno %d)",
		      strerror (errno), errno);
      ret = VNET_API_ERROR_SYSCALL_ERROR_1;
      goto error;
    }

  return 0;
error:
  if (*fd >= 0)
    {
      close (*fd);
      *fd = -1;
    }
  return ret;
}

int
af_packet_create_if (af_packet_create_if_arg_t *arg)
{
  af_packet_main_t *apm = &af_packet_main;
  vlib_main_t *vm = vlib_get_main ();
  int ret, fd = -1, fd2 = -1;
  tpacket_req3_t *rx_req = 0;
  tpacket_req3_t *tx_req = 0;
  struct ifreq ifr;
  u8 *ring = 0;
  af_packet_if_t *apif = 0;
  u8 hw_addr[6];
  vnet_sw_interface_t *sw;
  vlib_thread_main_t *tm = vlib_get_thread_main ();
  vnet_main_t *vnm = vnet_get_main ();
  vnet_hw_if_caps_t caps = VNET_HW_IF_CAP_INT_MODE;
  uword *p;
  uword if_index;
  u8 *host_if_name_dup = 0;
  int host_if_index = -1;
  u32 rx_frames_per_block, tx_frames_per_block;
  u32 rx_frame_size, tx_frame_size;
  u32 hdrlen = 0;
  u32 i = 0;
  u8 is_cksum_gso_enabled = 0;

  p = mhash_get (&apm->if_index_by_host_if_name, arg->host_if_name);
  if (p)
    {
      apif = vec_elt_at_index (apm->interfaces, p[0]);
      arg->sw_if_index = apif->sw_if_index;
      return VNET_API_ERROR_IF_ALREADY_EXISTS;
    }

  host_if_name_dup = vec_dup (arg->host_if_name);

  rx_frames_per_block = arg->rx_frames_per_block ?
			  arg->rx_frames_per_block :
			  AF_PACKET_DEFAULT_RX_FRAMES_PER_BLOCK;
  tx_frames_per_block = arg->tx_frames_per_block ?
			  arg->tx_frames_per_block :
			  AF_PACKET_DEFAULT_TX_FRAMES_PER_BLOCK;
  rx_frame_size =
    arg->rx_frame_size ? arg->rx_frame_size : AF_PACKET_DEFAULT_RX_FRAME_SIZE;
  tx_frame_size =
    arg->tx_frame_size ? arg->tx_frame_size : AF_PACKET_DEFAULT_TX_FRAME_SIZE;

  vec_validate (rx_req, 0);
  rx_req->tp_block_size = rx_frame_size * rx_frames_per_block;
  rx_req->tp_frame_size = rx_frame_size;
  rx_req->tp_block_nr = AF_PACKET_RX_BLOCK_NR;
  rx_req->tp_frame_nr = AF_PACKET_RX_BLOCK_NR * rx_frames_per_block;
  rx_req->tp_retire_blk_tov = 0;
  rx_req->tp_feature_req_word = 0;
  rx_req->tp_sizeof_priv = 0;

  vec_validate (tx_req, 0);
  tx_req->tp_block_size = tx_frame_size * tx_frames_per_block;
  tx_req->tp_frame_size = tx_frame_size;
  tx_req->tp_block_nr = AF_PACKET_TX_BLOCK_NR;
  tx_req->tp_frame_nr = AF_PACKET_TX_BLOCK_NR * tx_frames_per_block;
  tx_req->tp_retire_blk_tov = 0;
  tx_req->tp_sizeof_priv = 0;
  tx_req->tp_feature_req_word = 0;

  /*
   * make sure host side of interface is 'UP' before binding AF_PACKET
   * socket on it.
   */
  if ((fd2 = socket (AF_UNIX, SOCK_DGRAM, 0)) < 0)
    {
      vlib_log_debug (apm->log_class,
		      "Failed to create AF_UNIX socket: %s (errno %d)",
		      strerror (errno), errno);
      ret = VNET_API_ERROR_SYSCALL_ERROR_1;
      goto error;
    }

  clib_memcpy (ifr.ifr_name, (const char *) arg->host_if_name,
	       vec_len (arg->host_if_name));
  if (ioctl (fd2, SIOCGIFINDEX, &ifr) < 0)
    {
      vlib_log_debug (
	apm->log_class,
	"Failed to retrieve the interface (%s) index: %s (errno %d)",
	arg->host_if_name, strerror (errno), errno);
      ret = VNET_API_ERROR_INVALID_INTERFACE;
      goto error;
    }

  host_if_index = ifr.ifr_ifindex;
  if (ioctl (fd2, SIOCGIFFLAGS, &ifr) < 0)
    {
      vlib_log_debug (apm->log_class,
		      "Failed to get the active flag: %s (errno %d)",
		      strerror (errno), errno);
      ret = VNET_API_ERROR_SYSCALL_ERROR_1;
      goto error;
    }

  if (!(ifr.ifr_flags & IFF_UP))
    {
      ifr.ifr_flags |= IFF_UP;
      if (ioctl (fd2, SIOCSIFFLAGS, &ifr) < 0)
	{
	  vlib_log_debug (apm->log_class,
			  "Failed to set the active flag: %s (errno %d)",
			  strerror (errno), errno);
	  ret = VNET_API_ERROR_SYSCALL_ERROR_1;
	  goto error;
	}
    }

  if (fd2 > -1)
    {
      close (fd2);
      fd2 = -1;
    }

  ret = create_packet_v3_sock (host_if_index, rx_req, tx_req, &fd, &ring,
			       &hdrlen, &is_cksum_gso_enabled);

  if (ret != 0)
    goto error;

  ret = is_bridge (arg->host_if_name);

  if (ret == 0)			/* is a bridge, ignore state */
    host_if_index = -1;

  /* So far everything looks good, let's create interface */
  pool_get (apm->interfaces, apif);
  if_index = apif - apm->interfaces;

  apif->host_if_index = host_if_index;
  apif->fd = fd;

  vec_validate (apif->rx_ring, rx_req->tp_block_nr - 1);
  vec_foreach_index (i, apif->rx_ring)
    {
      apif->rx_ring[i] = ring + i * rx_req->tp_block_size;
    }

  ring = ring + rx_req->tp_block_size * rx_req->tp_block_nr;

  vec_validate (apif->tx_ring, tx_req->tp_block_nr - 1);
  vec_foreach_index (i, apif->tx_ring)
    {
      apif->tx_ring[i] = ring + i * tx_req->tp_block_size;
    }

  apif->rx_req = rx_req;
  apif->tx_req = tx_req;
  apif->host_if_name = host_if_name_dup;
  apif->per_interface_next_index = ~0;
  apif->next_tx_frame = 0;
  apif->next_rx_block = 0;
  apif->mode = arg->mode;
  apif->hdrlen = hdrlen;
  apif->is_cksum_gso_enabled = is_cksum_gso_enabled;
  apif->ss.is_save = 0;

  ret = af_packet_read_mtu (apif);
  if (ret != 0)
    goto error;

  if (tm->n_vlib_mains > 1)
    clib_spinlock_init (&apif->lockp);

  if (apif->mode != AF_PACKET_IF_MODE_IP)
    {
      vnet_eth_interface_registration_t eir = {};
      /*use configured or generate random MAC address */
      if (arg->hw_addr)
	clib_memcpy (hw_addr, arg->hw_addr, 6);
      else
	{
	  f64 now = vlib_time_now (vm);
	  u32 rnd;
	  rnd = (u32) (now * 1e6);
	  rnd = random_u32 (&rnd);

	  clib_memcpy (hw_addr + 2, &rnd, sizeof (rnd));
	  hw_addr[0] = 2;
	  hw_addr[1] = 0xfe;
	}

      eir.dev_class_index = af_packet_device_class.index;
      eir.dev_instance = if_index;
      eir.address = hw_addr;
      eir.cb.set_max_frame_size = af_packet_eth_set_max_frame_size;
      apif->hw_if_index = vnet_eth_register_interface (vnm, &eir);
    }
  else
    {
      apif->hw_if_index = vnet_register_interface (
	vnm, af_packet_device_class.index, if_index,
	af_packet_ip_device_hw_interface_class.index, if_index);
    }
  sw = vnet_get_hw_sw_interface (vnm, apif->hw_if_index);
  apif->sw_if_index = sw->sw_if_index;
  vnet_hw_if_set_input_node (vnm, apif->hw_if_index,
			     af_packet_input_node.index);
  apif->queue_index = vnet_hw_if_register_rx_queue (vnm, apif->hw_if_index, 0,
						    VNET_HW_IF_RXQ_THREAD_ANY);

  if (apif->is_cksum_gso_enabled)
    caps |= VNET_HW_IF_CAP_TCP_GSO | VNET_HW_IF_CAP_TX_IP4_CKSUM |
	    VNET_HW_IF_CAP_TX_TCP_CKSUM | VNET_HW_IF_CAP_TX_UDP_CKSUM;

  vnet_hw_if_set_caps (vnm, apif->hw_if_index, caps);
  vnet_hw_interface_set_flags (vnm, apif->hw_if_index,
			       VNET_HW_INTERFACE_FLAG_LINK_UP);

  vnet_hw_if_set_rx_queue_mode (vnm, apif->queue_index,
				VNET_HW_IF_RX_MODE_INTERRUPT);
  vnet_hw_if_update_runtime_data (vnm, apif->hw_if_index);
  {
    clib_file_t template = { 0 };
    template.read_function = af_packet_fd_read_ready;
    template.file_descriptor = fd;
    template.private_data = if_index;
    template.flags = UNIX_FILE_EVENT_EDGE_TRIGGERED;
    template.description =
      format (0, "%U", format_af_packet_device_name, if_index);
    apif->clib_file_index = clib_file_add (&file_main, &template);
  }
  vnet_hw_if_set_rx_queue_file_index (vnm, apif->queue_index,
				      apif->clib_file_index);

  mhash_set_mem (&apm->if_index_by_host_if_name, host_if_name_dup, &if_index,
		 0);
  arg->sw_if_index = apif->sw_if_index;

  return 0;

error:
  if (fd2 > -1)
    {
      close (fd2);
      fd2 = -1;
    }
  vec_free (host_if_name_dup);
  vec_free (rx_req);
  vec_free (tx_req);
  return ret;
}

int
af_packet_delete_if (u8 *host_if_name)
{
  vnet_main_t *vnm = vnet_get_main ();
  af_packet_main_t *apm = &af_packet_main;
  af_packet_if_t *apif;
  uword *p;
  uword if_index;
  u32 ring_sz;

  p = mhash_get (&apm->if_index_by_host_if_name, host_if_name);
  if (p == NULL)
    {
      vlib_log_warn (apm->log_class, "Host interface %s does not exist",
		     host_if_name);
      return VNET_API_ERROR_SYSCALL_ERROR_1;
    }
  apif = pool_elt_at_index (apm->interfaces, p[0]);
  if_index = apif - apm->interfaces;

  /* bring down the interface */
  vnet_hw_interface_set_flags (vnm, apif->hw_if_index, 0);

  /* clean up */
  if (apif->clib_file_index != ~0)
    {
      clib_file_del (&file_main, file_main.file_pool + apif->clib_file_index);
      apif->clib_file_index = ~0;
    }
  else
    close (apif->fd);

  ring_sz = apif->rx_req->tp_block_size * apif->rx_req->tp_block_nr +
    apif->tx_req->tp_block_size * apif->tx_req->tp_block_nr;
  if (munmap (apif->rx_ring, ring_sz))
    vlib_log_warn (apm->log_class,
		   "Host interface %s could not free rx/tx ring",
		   host_if_name);
  apif->rx_ring = NULL;
  apif->tx_ring = NULL;
  apif->fd = -1;

  vec_free (apif->rx_req);
  apif->rx_req = NULL;
  vec_free (apif->tx_req);
  apif->tx_req = NULL;

  vec_free (apif->host_if_name);
  apif->host_if_name = NULL;
  apif->host_if_index = -1;

  mhash_unset (&apm->if_index_by_host_if_name, host_if_name, &if_index);

  if (apif->mode != AF_PACKET_IF_MODE_IP)
    ethernet_delete_interface (vnm, apif->hw_if_index);
  else
    vnet_delete_hw_interface (vnm, apif->hw_if_index);

  pool_put (apm->interfaces, apif);

  return 0;
}

int
af_packet_set_l4_cksum_offload (u32 sw_if_index, u8 set)
{
  // deprecated ...
  return 0;
}

int
af_packet_dump_ifs (af_packet_if_detail_t ** out_af_packet_ifs)
{
  af_packet_main_t *apm = &af_packet_main;
  af_packet_if_t *apif;
  af_packet_if_detail_t *r_af_packet_ifs = NULL;
  af_packet_if_detail_t *af_packet_if = NULL;

  pool_foreach (apif, apm->interfaces)
     {
      vec_add2 (r_af_packet_ifs, af_packet_if, 1);
      af_packet_if->sw_if_index = apif->sw_if_index;
      if (apif->host_if_name)
	{
	  clib_memcpy (af_packet_if->host_if_name, apif->host_if_name,
		       MIN (ARRAY_LEN (af_packet_if->host_if_name) - 1,
		       strlen ((const char *) apif->host_if_name)));
	}
    }

  *out_af_packet_ifs = r_af_packet_ifs;

  return 0;
}

static clib_error_t *
af_packet_init (vlib_main_t * vm)
{
  af_packet_main_t *apm = &af_packet_main;
  vlib_thread_main_t *tm = vlib_get_thread_main ();

  clib_memset (apm, 0, sizeof (af_packet_main_t));

  mhash_init_vec_string (&apm->if_index_by_host_if_name, sizeof (uword));

  vec_validate_aligned (apm->rx_buffers, tm->n_vlib_mains - 1,
			CLIB_CACHE_LINE_BYTES);

  apm->log_class = vlib_log_register_class ("af_packet", 0);
  vlib_log_debug (apm->log_class, "initialized");

  return 0;
}

VLIB_INIT_FUNCTION (af_packet_init);

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */