FD.io VPP  v20.01-48-g3e0dafb74
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  s = format (s, "Linux PACKET socket interface");
71  return s;
72 }
73 
74 static u8 *
75 format_af_packet_tx_trace (u8 * s, va_list * args)
76 {
77  s = format (s, "Unimplemented...");
78  return s;
79 }
80 
84 {
86  u32 *buffers = vlib_frame_vector_args (frame);
87  u32 n_left = frame->n_vectors;
88  u32 n_sent = 0;
89  vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
90  af_packet_if_t *apif =
92  clib_spinlock_lock_if_init (&apif->lockp);
93  int block = 0;
94  u32 block_size = apif->tx_req->tp_block_size;
95  u32 frame_size = apif->tx_req->tp_frame_size;
96  u32 frame_num = apif->tx_req->tp_frame_nr;
97  u8 *block_start = apif->tx_ring + block * block_size;
98  u32 tx_frame = apif->next_tx_frame;
99  struct tpacket2_hdr *tph;
100  u32 frame_not_ready = 0;
101 
102  while (n_left > 0)
103  {
104  u32 len;
105  u32 offset = 0;
106  vlib_buffer_t *b0;
107  n_left--;
108  u32 bi = buffers[0];
109  buffers++;
110 
111  tph = (struct tpacket2_hdr *) (block_start + tx_frame * frame_size);
112 
113  if (PREDICT_FALSE
114  (tph->tp_status & (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING)))
115  {
116  frame_not_ready++;
117  goto next;
118  }
119 
120  do
121  {
122  b0 = vlib_get_buffer (vm, bi);
123  len = b0->current_length;
124  clib_memcpy_fast ((u8 *) tph +
125  TPACKET_ALIGN (sizeof (struct tpacket2_hdr)) +
126  offset, vlib_buffer_get_current (b0), len);
127  offset += len;
128  }
129  while ((bi =
130  (b0->flags & VLIB_BUFFER_NEXT_PRESENT) ? b0->next_buffer : 0));
131 
132  tph->tp_len = tph->tp_snaplen = offset;
133  tph->tp_status = TP_STATUS_SEND_REQUEST;
134  n_sent++;
135  next:
136  tx_frame = (tx_frame + 1) % frame_num;
137 
138  /* check if we've exhausted the ring */
139  if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num))
140  break;
141  }
142 
144 
145  if (PREDICT_TRUE (n_sent))
146  {
147  apif->next_tx_frame = tx_frame;
148 
149  if (PREDICT_FALSE (sendto (apif->fd, NULL, 0,
150  MSG_DONTWAIT, NULL, 0) == -1))
151  {
152  /* Uh-oh, drop & move on, but count whether it was fatal or not.
153  * Note that we have no reliable way to properly determine the
154  * disposition of the packets we just enqueued for delivery.
155  */
156  vlib_error_count (vm, node->node_index,
157  unix_error_is_fatal (errno) ?
158  AF_PACKET_TX_ERROR_TXRING_FATAL :
159  AF_PACKET_TX_ERROR_TXRING_EAGAIN, n_sent);
160  }
161  }
162 
163  clib_spinlock_unlock_if_init (&apif->lockp);
164 
165  if (PREDICT_FALSE (frame_not_ready))
166  vlib_error_count (vm, node->node_index,
167  AF_PACKET_TX_ERROR_FRAME_NOT_READY, frame_not_ready);
168 
169  if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num))
170  vlib_error_count (vm, node->node_index, AF_PACKET_TX_ERROR_TXRING_OVERRUN,
171  n_left);
172 
174  return frame->n_vectors;
175 }
176 
177 static void
179  u32 node_index)
180 {
182  vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
183  af_packet_if_t *apif =
185 
186  /* Shut off redirection */
187  if (node_index == ~0)
188  {
189  apif->per_interface_next_index = node_index;
190  return;
191  }
192 
195  node_index);
196 }
197 
198 static void
200 {
201  /* Nothing for now */
202 }
203 
204 static clib_error_t *
206  u32 flags)
207 {
209  vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
210  af_packet_if_t *apif =
212  u32 hw_flags;
213  int rv, fd = socket (AF_UNIX, SOCK_DGRAM, 0);
214  struct ifreq ifr;
215 
216  if (0 > fd)
217  {
218  vlib_log_warn (apm->log_class, "af_packet_%s could not open socket",
219  apif->host_if_name);
220  return 0;
221  }
222 
223  /* if interface is a bridge ignore */
224  if (apif->host_if_index < 0)
225  goto error; /* no error */
226 
227  /* use host_if_index in case host name has changed */
228  ifr.ifr_ifindex = apif->host_if_index;
229  if ((rv = ioctl (fd, SIOCGIFNAME, &ifr)) < 0)
230  {
231  vlib_log_warn (apm->log_class,
232  "af_packet_%s ioctl could not retrieve eth name",
233  apif->host_if_name);
234  goto error;
235  }
236 
237  apif->is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
238 
239  if ((rv = ioctl (fd, SIOCGIFFLAGS, &ifr)) < 0)
240  {
241  vlib_log_warn (apm->log_class, "af_packet_%s error: %d",
242  apif->is_admin_up ? "up" : "down", rv);
243  goto error;
244  }
245 
246  if (apif->is_admin_up)
247  {
249  ifr.ifr_flags |= IFF_UP;
250  }
251  else
252  {
253  hw_flags = 0;
254  ifr.ifr_flags &= ~IFF_UP;
255  }
256 
257  if ((rv = ioctl (fd, SIOCSIFFLAGS, &ifr)) < 0)
258  {
259  vlib_log_warn (apm->log_class, "af_packet_%s error: %d",
260  apif->is_admin_up ? "up" : "down", rv);
261  goto error;
262  }
263 
264  vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
265 
266 error:
267  if (0 <= fd)
268  close (fd);
269 
270  return 0; /* no error */
271 }
272 
273 static clib_error_t *
275  u32 hw_if_index,
276  struct vnet_sw_interface_t *st, int is_add)
277 {
278  /* Nothing for now */
279  return 0;
280 }
281 
283  (struct vnet_hw_interface_t *hi, const u8 * old_address, const u8 * address)
284 {
286  af_packet_if_t *apif =
288  int rv, fd = socket (AF_UNIX, SOCK_DGRAM, 0);
289  struct ifreq ifr;
290 
291  if (0 > fd)
292  {
293  vlib_log_warn (apm->log_class, "af_packet_%s could not open socket",
294  apif->host_if_name);
295  return 0;
296  }
297 
298  /* if interface is a bridge ignore */
299  if (apif->host_if_index < 0)
300  goto error; /* no error */
301 
302  /* use host_if_index in case host name has changed */
303  ifr.ifr_ifindex = apif->host_if_index;
304  if ((rv = ioctl (fd, SIOCGIFNAME, &ifr)) < 0)
305  {
307  (apm->log_class,
308  "af_packet_%s ioctl could not retrieve eth name, error: %d",
309  apif->host_if_name, rv);
310  goto error;
311  }
312 
313  clib_memcpy (ifr.ifr_hwaddr.sa_data, address, 6);
314  ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
315 
316  if ((rv = ioctl (fd, SIOCSIFHWADDR, &ifr)) < 0)
317  {
318  vlib_log_warn (apm->log_class,
319  "af_packet_%s ioctl could not set mac, error: %d",
320  apif->host_if_name, rv);
321  goto error;
322  }
323 
324 error:
325 
326  if (0 <= fd)
327  close (fd);
328 
329  return 0; /* no error */
330 }
331 
332 /* *INDENT-OFF* */
334  .name = "af-packet",
335  .format_device_name = format_af_packet_device_name,
336  .format_device = format_af_packet_device,
337  .format_tx_trace = format_af_packet_tx_trace,
338  .tx_function_n_errors = AF_PACKET_TX_N_ERROR,
339  .tx_function_error_strings = af_packet_tx_func_error_strings,
340  .rx_redirect_to_node = af_packet_set_interface_next_node,
341  .clear_counters = af_packet_clear_hw_interface_counters,
342  .admin_up_down_function = af_packet_interface_admin_up_down,
343  .subif_add_del_function = af_packet_subif_add_del_function,
344  .mac_addr_change_function = af_packet_set_mac_address_function,
345 };
346 /* *INDENT-ON* */
347 
348 /*
349  * fd.io coding-style-patch-verification: ON
350  *
351  * Local Variables:
352  * eval: (c-set-style "gnu")
353  * End:
354  */
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:124
#define vlib_log_warn(...)
Definition: log.h:103
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:890
#define PREDICT_TRUE(x)
Definition: clib.h:112
static void vlib_error_count(vlib_main_t *vm, uword node_index, uword counter, uword increment)
Definition: error_funcs.h:57
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
#define NULL
Definition: clib.h:58
static u8 * format_af_packet_tx_trace(u8 *s, va_list *args)
Definition: device.c:75
static_always_inline void clib_spinlock_unlock_if_init(clib_spinlock_t *p)
Definition: lock.h:110
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:113
int i
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
static void af_packet_set_interface_next_node(vnet_main_t *vnm, u32 hw_if_index, u32 node_index)
Definition: device.c:178
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:81
static uword vlib_node_add_next(vlib_main_t *vm, uword node, uword next_node)
Definition: node_funcs.h:1092
unsigned char u8
Definition: types.h:56
#define clib_memcpy(d, s, n)
Definition: string.h:180
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:283
u8 * host_if_name
Definition: af_packet.h:34
af_packet_if_t * interfaces
Definition: af_packet.h:55
#define VNET_DEVICE_CLASS_TX_FN(devclass)
Definition: interface.h:305
unsigned int u32
Definition: types.h:88
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:519
static void af_packet_clear_hw_interface_counters(u32 instance)
Definition: device.c:199
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:229
#define PREDICT_FALSE(x)
Definition: clib.h:111
static char * af_packet_tx_func_error_strings[]
Definition: device.c:47
vlib_log_class_t log_class
log class
Definition: af_packet.h:67
vlib_main_t * vm
Definition: in2out_ed.c:1810
u8 len
Definition: ip_types.api:91
u32 flags
Definition: vhost_user.h:141
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:274
u32 per_interface_next_index
Definition: af_packet.h:48
vlib_node_registration_t af_packet_input_node
(constructor) VLIB_REGISTER_NODE (af_packet_input_node)
Definition: node.c:370
vlib_main_t vlib_node_runtime_t * node
Definition: in2out_ed.c:1810
#define foreach_af_packet_tx_func_error
Definition: device.c:33
int host_if_index
Definition: af_packet.h:35
af_packet_main_t af_packet_main
Definition: af_packet.c:37
static word unix_error_is_fatal(word error)
Definition: error.h:118
af_packet_tx_func_error_t
Definition: device.c:39
u8 * format_af_packet_device_name(u8 *s, va_list *args)
Definition: device.c:56
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
template key/value backing page structure
Definition: bihash_doc.h:44
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:140
u32 instance
Definition: gre.api:57
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:492
VLIB buffer representation.
Definition: buffer.h:102
static clib_error_t * af_packet_interface_admin_up_down(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: device.c:205
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:244
struct clib_bihash_value offset
template key/value backing page structure
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: in2out_ed.c:1811
#define CLIB_MEMORY_BARRIER()
Definition: clib.h:115
static_always_inline void clib_spinlock_lock_if_init(clib_spinlock_t *p)
Definition: lock.h:95
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:85
static u8 * format_af_packet_device(u8 *s, va_list *args)
Definition: device.c:68
VNET_DEVICE_CLASS(avf_device_class,)