FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
device.c
Go to the documentation of this file.
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_packet.h>
21 #include <sys/socket.h>
22 #include <sys/ioctl.h>
23 #include <net/if.h>
24 #include <net/if_arp.h>
25 
26 #include <vlib/vlib.h>
27 #include <vlib/unix/unix.h>
28 #include <vnet/ip/ip.h>
29 #include <vnet/ethernet/ethernet.h>
30 
32 
33 #define foreach_af_packet_tx_func_error \
34 _(FRAME_NOT_READY, "tx frame not ready") \
35 _(TXRING_EAGAIN, "tx sendto temporary failure") \
36 _(TXRING_FATAL, "tx sendto fatal failure") \
37 _(TXRING_OVERRUN, "tx ring overrun")
38 
39 typedef enum
40 {
41 #define _(f,s) AF_PACKET_TX_ERROR_##f,
43 #undef _
46 
48 #define _(n,s) s,
50 #undef _
51 };
52 
53 
54 #ifndef CLIB_MARCH_VARIANT
55 u8 *
56 format_af_packet_device_name (u8 * s, va_list * args)
57 {
58  u32 i = va_arg (*args, u32);
61 
62  s = format (s, "host-%s", apif->host_if_name);
63  return s;
64 }
65 #endif /* CLIB_MARCH_VARIANT */
66 
67 static u8 *
68 format_af_packet_device (u8 * s, va_list * args)
69 {
70  u32 dev_instance = va_arg (*args, u32);
71  u32 indent = format_get_indent (s);
72  int __clib_unused verbose = va_arg (*args, int);
73 
75  af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, dev_instance);
77  u32 block_size = apif->tx_req->tp_block_size;
78  u32 frame_size = apif->tx_req->tp_frame_size;
79  u32 frame_num = apif->tx_req->tp_frame_nr;
80  int block = 0;
81  u8 *block_start = apif->tx_ring + block * block_size;
82  u32 tx_frame = apif->next_tx_frame;
83  struct tpacket2_hdr *tph;
84 
85  s = format (s, "Linux PACKET socket interface\n");
86  s = format (s, "%Ublock:%d frame:%d\n", format_white_space, indent,
87  block_size, frame_size);
88  s = format (s, "%Unext frame:%d\n", format_white_space, indent,
89  apif->next_tx_frame);
90 
91  int n_send_req = 0, n_avail = 0, n_sending = 0, n_tot = 0, n_wrong = 0;
92  do
93  {
94  tph = (struct tpacket2_hdr *) (block_start + tx_frame * frame_size);
95  tx_frame = (tx_frame + 1) % frame_num;
96  if (tph->tp_status == 0)
97  n_avail++;
98  else if (tph->tp_status & TP_STATUS_SEND_REQUEST)
99  n_send_req++;
100  else if (tph->tp_status & TP_STATUS_SENDING)
101  n_sending++;
102  else
103  n_wrong++;
104  n_tot++;
105  }
106  while (tx_frame != apif->next_tx_frame);
107  s = format (s, "%Uavailable:%d request:%d sending:%d wrong:%d total:%d\n",
108  format_white_space, indent, n_avail, n_send_req, n_sending,
109  n_wrong, n_tot);
110 
112  return s;
113 }
114 
115 static u8 *
116 format_af_packet_tx_trace (u8 * s, va_list * args)
117 {
118  s = format (s, "Unimplemented...");
119  return s;
120 }
121 
125 {
127  u32 *buffers = vlib_frame_vector_args (frame);
128  u32 n_left = frame->n_vectors;
129  u32 n_sent = 0;
130  vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
131  af_packet_if_t *apif =
133  clib_spinlock_lock_if_init (&apif->lockp);
134  int block = 0;
135  u32 block_size = apif->tx_req->tp_block_size;
136  u32 frame_size = apif->tx_req->tp_frame_size;
137  u32 frame_num = apif->tx_req->tp_frame_nr;
138  u8 *block_start = apif->tx_ring + block * block_size;
139  u32 tx_frame = apif->next_tx_frame;
140  struct tpacket2_hdr *tph;
141  u32 frame_not_ready = 0;
142 
143  while (n_left)
144  {
145  u32 len;
146  u32 offset = 0;
147  vlib_buffer_t *b0;
148  n_left--;
149  u32 bi = buffers[0];
150  buffers++;
151 
152  nextframe:
153  tph = (struct tpacket2_hdr *) (block_start + tx_frame * frame_size);
154  if (PREDICT_FALSE (tph->tp_status &
155  (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING)))
156  {
157  tx_frame = (tx_frame + 1) % frame_num;
158  frame_not_ready++;
159  /* check if we've exhausted the ring */
160  if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num))
161  break;
162  goto nextframe;
163  }
164 
165  do
166  {
167  b0 = vlib_get_buffer (vm, bi);
168  len = b0->current_length;
169  clib_memcpy_fast ((u8 *) tph +
170  TPACKET_ALIGN (sizeof (struct tpacket2_hdr)) +
172  offset += len;
173  }
174  while ((bi =
175  (b0->flags & VLIB_BUFFER_NEXT_PRESENT) ? b0->next_buffer : 0));
176 
177  tph->tp_len = tph->tp_snaplen = offset;
178  tph->tp_status = TP_STATUS_SEND_REQUEST;
179  n_sent++;
180 
181  tx_frame = (tx_frame + 1) % frame_num;
182 
183  /* check if we've exhausted the ring */
184  if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num))
185  break;
186  }
187 
189 
190  apif->next_tx_frame = tx_frame;
191 
192  if (PREDICT_TRUE (n_sent))
193  if (PREDICT_FALSE (sendto (apif->fd, NULL, 0, MSG_DONTWAIT, NULL, 0) ==
194  -1))
195  {
196  /* Uh-oh, drop & move on, but count whether it was fatal or not.
197  * Note that we have no reliable way to properly determine the
198  * disposition of the packets we just enqueued for delivery.
199  */
200  vlib_error_count (vm, node->node_index,
201  unix_error_is_fatal (errno) ?
202  AF_PACKET_TX_ERROR_TXRING_FATAL :
203  AF_PACKET_TX_ERROR_TXRING_EAGAIN,
204  n_sent);
205  }
206 
207  clib_spinlock_unlock_if_init (&apif->lockp);
208 
209  if (PREDICT_FALSE (frame_not_ready))
210  vlib_error_count (vm, node->node_index,
211  AF_PACKET_TX_ERROR_FRAME_NOT_READY, frame_not_ready);
212 
213  if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num))
214  vlib_error_count (vm, node->node_index, AF_PACKET_TX_ERROR_TXRING_OVERRUN,
215  n_left);
216 
218  return frame->n_vectors;
219 }
220 
221 static void
223  u32 node_index)
224 {
226  vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
227  af_packet_if_t *apif =
229 
230  /* Shut off redirection */
231  if (node_index == ~0)
232  {
234  return;
235  }
236 
239  node_index);
240 }
241 
242 static void
244 {
245  /* Nothing for now */
246 }
247 
248 static clib_error_t *
250  u32 flags)
251 {
253  vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
254  af_packet_if_t *apif =
256  u32 hw_flags;
257  int rv, fd = socket (AF_UNIX, SOCK_DGRAM, 0);
258  struct ifreq ifr;
259 
260  if (0 > fd)
261  {
262  vlib_log_warn (apm->log_class, "af_packet_%s could not open socket",
263  apif->host_if_name);
264  return 0;
265  }
266 
267  /* if interface is a bridge ignore */
268  if (apif->host_if_index < 0)
269  goto error; /* no error */
270 
271  /* use host_if_index in case host name has changed */
272  ifr.ifr_ifindex = apif->host_if_index;
273  if ((rv = ioctl (fd, SIOCGIFNAME, &ifr)) < 0)
274  {
275  vlib_log_warn (apm->log_class,
276  "af_packet_%s ioctl could not retrieve eth name",
277  apif->host_if_name);
278  goto error;
279  }
280 
282 
283  if ((rv = ioctl (fd, SIOCGIFFLAGS, &ifr)) < 0)
284  {
285  vlib_log_warn (apm->log_class, "af_packet_%s error: %d",
286  apif->is_admin_up ? "up" : "down", rv);
287  goto error;
288  }
289 
290  if (apif->is_admin_up)
291  {
293  ifr.ifr_flags |= IFF_UP;
294  }
295  else
296  {
297  hw_flags = 0;
298  ifr.ifr_flags &= ~IFF_UP;
299  }
300 
301  if ((rv = ioctl (fd, SIOCSIFFLAGS, &ifr)) < 0)
302  {
303  vlib_log_warn (apm->log_class, "af_packet_%s error: %d",
304  apif->is_admin_up ? "up" : "down", rv);
305  goto error;
306  }
307 
308  vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
309 
310 error:
311  if (0 <= fd)
312  close (fd);
313 
314  return 0; /* no error */
315 }
316 
317 static clib_error_t *
319  u32 hw_if_index,
320  struct vnet_sw_interface_t *st, int is_add)
321 {
322  /* Nothing for now */
323  return 0;
324 }
325 
327  (struct vnet_hw_interface_t *hi, const u8 * old_address, const u8 * address)
328 {
330  af_packet_if_t *apif =
331  pool_elt_at_index (apm->interfaces, hi->dev_instance);
332  int rv, fd = socket (AF_UNIX, SOCK_DGRAM, 0);
333  struct ifreq ifr;
334 
335  if (0 > fd)
336  {
337  vlib_log_warn (apm->log_class, "af_packet_%s could not open socket",
338  apif->host_if_name);
339  return 0;
340  }
341 
342  /* if interface is a bridge ignore */
343  if (apif->host_if_index < 0)
344  goto error; /* no error */
345 
346  /* use host_if_index in case host name has changed */
347  ifr.ifr_ifindex = apif->host_if_index;
348  if ((rv = ioctl (fd, SIOCGIFNAME, &ifr)) < 0)
349  {
351  (apm->log_class,
352  "af_packet_%s ioctl could not retrieve eth name, error: %d",
353  apif->host_if_name, rv);
354  goto error;
355  }
356 
357  clib_memcpy (ifr.ifr_hwaddr.sa_data, address, 6);
358  ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
359 
360  if ((rv = ioctl (fd, SIOCSIFHWADDR, &ifr)) < 0)
361  {
362  vlib_log_warn (apm->log_class,
363  "af_packet_%s ioctl could not set mac, error: %d",
364  apif->host_if_name, rv);
365  goto error;
366  }
367 
368 error:
369 
370  if (0 <= fd)
371  close (fd);
372 
373  return 0; /* no error */
374 }
375 
376 /* *INDENT-OFF* */
378  .name = "af-packet",
379  .format_device_name = format_af_packet_device_name,
380  .format_device = format_af_packet_device,
381  .format_tx_trace = format_af_packet_tx_trace,
382  .tx_function_n_errors = AF_PACKET_TX_N_ERROR,
383  .tx_function_error_strings = af_packet_tx_func_error_strings,
384  .rx_redirect_to_node = af_packet_set_interface_next_node,
385  .clear_counters = af_packet_clear_hw_interface_counters,
386  .admin_up_down_function = af_packet_interface_admin_up_down,
387  .subif_add_del_function = af_packet_subif_add_del_function,
388  .mac_addr_change_function = af_packet_set_mac_address_function,
389 };
390 /* *INDENT-ON* */
391 
392 /*
393  * fd.io coding-style-patch-verification: ON
394  *
395  * Local Variables:
396  * eval: (c-set-style "gnu")
397  * End:
398  */
vlib.h
vlib_buffer_t::next_buffer
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:149
vlib_buffer_free
static void vlib_buffer_free(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Free buffers Frees the entire buffer chain for each buffer.
Definition: buffer_funcs.h:982
af_packet_set_mac_address_function
static clib_error_t * af_packet_set_mac_address_function(struct vnet_hw_interface_t *hi, const u8 *old_address, const u8 *address)
Definition: device.c:327
af_packet_interface_admin_up_down
static clib_error_t * af_packet_interface_admin_up_down(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: device.c:249
af_packet_if_t::tx_req
struct tpacket_req * tx_req
Definition: af_packet.h:38
af_packet_input_node
vlib_node_registration_t af_packet_input_node
(constructor) VLIB_REGISTER_NODE (af_packet_input_node)
Definition: node.c:391
vnet_sw_interface_t
Definition: interface.h:868
af_packet_if_t::host_if_name
u8 * host_if_name
Definition: af_packet.h:34
frame
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: nat44_ei.c:3048
af_packet_main
af_packet_main_t af_packet_main
Definition: af_packet.c:39
vlib_node_add_next
static uword vlib_node_add_next(vlib_main_t *vm, uword node, uword next_node)
Definition: node_funcs.h:1177
clib_memcpy
#define clib_memcpy(d, s, n)
Definition: string.h:197
clib_spinlock_lock_if_init
static_always_inline void clib_spinlock_lock_if_init(clib_spinlock_t *p)
Definition: lock.h:106
af_packet_if_t
Definition: af_packet.h:30
vlib_get_buffer
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:111
pool_elt_at_index
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:553
AF_PACKET_TX_N_ERROR
@ AF_PACKET_TX_N_ERROR
Definition: device.c:44
unix_error_is_fatal
static word unix_error_is_fatal(word error)
Definition: error.h:118
node
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
VNET_DEVICE_CLASS
VNET_DEVICE_CLASS(af_xdp_device_class)
vlib_log_warn
#define vlib_log_warn(...)
Definition: log.h:134
block_size
u16 block_size
Definition: ikev2_types.api:97
VNET_SW_INTERFACE_FLAG_ADMIN_UP
@ VNET_SW_INTERFACE_FLAG_ADMIN_UP
Definition: interface.h:843
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
format_af_packet_device_name
u8 * format_af_packet_device_name(u8 *s, va_list *args)
Definition: device.c:56
node_index
node node_index
Definition: interface_output.c:420
VNET_HW_INTERFACE_FLAG_LINK_UP
@ VNET_HW_INTERFACE_FLAG_LINK_UP
Definition: interface.h:509
af_packet_if_t::host_if_index
int host_if_index
Definition: af_packet.h:35
hi
vl_api_ip4_address_t hi
Definition: arp.api:37
vnet_hw_interface_t::dev_instance
u32 dev_instance
Definition: interface.h:660
af_packet_if_t::tx_ring
u8 * tx_ring
Definition: af_packet.h:40
vlib_error_count
static void vlib_error_count(vlib_main_t *vm, uword node_index, uword counter, uword increment)
Definition: error_funcs.h:57
vlib_frame_t
Definition: node.h:372
af_packet_main_t
Definition: af_packet.h:54
clib_memcpy_fast
static_always_inline void * clib_memcpy_fast(void *restrict dst, const void *restrict src, size_t n)
Definition: string.h:92
ethernet.h
VNET_DEVICE_CLASS_TX_FN
#define VNET_DEVICE_CLASS_TX_FN(devclass)
Definition: interface.h:317
error
Definition: cJSON.c:88
af_packet_device_class
VNET_DEVICE_CLASS_TX_FN() af_packet_device_class(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: device.c:122
af_packet_subif_add_del_function
static clib_error_t * af_packet_subif_add_del_function(vnet_main_t *vnm, u32 hw_if_index, struct vnet_sw_interface_t *st, int is_add)
Definition: device.c:318
len
u8 len
Definition: ip_types.api:103
vnet_interface_output_runtime_t::dev_instance
u32 dev_instance
Definition: interface_funcs.h:476
vnet_get_hw_interface
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
Definition: interface_funcs.h:44
offset
struct clib_bihash_value offset
template key/value backing page structure
PREDICT_FALSE
#define PREDICT_FALSE(x)
Definition: clib.h:124
vlib_frame_vector_args
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:301
af_packet_main_t::log_class
vlib_log_class_t log_class
log class
Definition: af_packet.h:69
af_packet_tx_func_error_t
af_packet_tx_func_error_t
Definition: device.c:39
format_af_packet_device
static u8 * format_af_packet_device(u8 *s, va_list *args)
Definition: device.c:68
af_packet_main_t::interfaces
af_packet_if_t * interfaces
Definition: af_packet.h:57
af_packet_if_t::is_admin_up
u8 is_admin_up
Definition: af_packet.h:49
i
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:261
address
manual_print typedef address
Definition: ip_types.api:96
af_packet_clear_hw_interface_counters
static void af_packet_clear_hw_interface_counters(u32 instance)
Definition: device.c:243
CLIB_MEMORY_BARRIER
#define CLIB_MEMORY_BARRIER()
Definition: clib.h:137
vlib_buffer_t::current_length
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:122
vnet_hw_interface_t
Definition: interface.h:638
vnet_main_t
Definition: vnet.h:76
foreach_af_packet_tx_func_error
#define foreach_af_packet_tx_func_error
Definition: device.c:33
clib_bihash_value
template key/value backing page structure
Definition: bihash_doc.h:44
format
description fragment has unexpected format
Definition: map.api:433
format_get_indent
static u32 format_get_indent(u8 *s)
Definition: format.h:72
ip.h
u32
unsigned int u32
Definition: types.h:88
n_left
u32 n_left
Definition: interface_output.c:1078
instance
u32 instance
Definition: gre.api:51
af_packet.h
af_packet_tx_func_error_strings
static char * af_packet_tx_func_error_strings[]
Definition: device.c:47
vlib_main_t
Definition: main.h:102
af_packet_set_interface_next_node
static void af_packet_set_interface_next_node(vnet_main_t *vnm, u32 hw_if_index, u32 node_index)
Definition: device.c:222
vlib_get_main
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:38
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
vnet_hw_interface_set_flags
clib_error_t * vnet_hw_interface_set_flags(vnet_main_t *vnm, u32 hw_if_index, vnet_hw_interface_flags_t flags)
Definition: interface.c:513
unix.h
vlib_buffer_get_current
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:257
af_packet_if_t::next_tx_frame
u32 next_tx_frame
Definition: af_packet.h:46
rv
int __clib_unused rv
Definition: application.c:491
format_af_packet_tx_trace
static u8 * format_af_packet_tx_trace(u8 *s, va_list *args)
Definition: device.c:116
vlib_node_runtime_t
Definition: node.h:454
clib_spinlock_unlock_if_init
static_always_inline void clib_spinlock_unlock_if_init(clib_spinlock_t *p)
Definition: lock.h:129
PREDICT_TRUE
#define PREDICT_TRUE(x)
Definition: clib.h:125
af_packet_if_t::per_interface_next_index
u32 per_interface_next_index
Definition: af_packet.h:48
format_white_space
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:129
vlib_buffer_t::flags
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index,...
Definition: buffer.h:133
af_packet_if_t::lockp
clib_spinlock_t lockp
Definition: af_packet.h:33
vlib_buffer_t
VLIB buffer representation.
Definition: buffer.h:111
vnet_interface_output_runtime_t
Definition: interface_funcs.h:472
flags
vl_api_wireguard_peer_flags_t flags
Definition: wireguard.api:105