FD.io VPP  v16.06
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 
22 #include <vlib/vlib.h>
23 #include <vlib/unix/unix.h>
24 #include <vnet/ip/ip.h>
25 #include <vnet/ethernet/ethernet.h>
26 
28 
29 #define foreach_af_packet_tx_func_error \
30 _(FRAME_NOT_READY, "tx frame not ready")
31 
32 typedef enum {
33 #define _(f,s) AF_PACKET_TX_ERROR_##f,
35 #undef _
38 
40 #define _(n,s) s,
42 #undef _
43 };
44 
45 
46 static u8 * format_af_packet_device_name (u8 * s, va_list * args)
47 {
48  u32 i = va_arg (*args, u32);
50  af_packet_if_t * apif = pool_elt_at_index (apm->interfaces, i);
51 
52  s = format (s, "host-%s", apif->host_if_name);
53  return s;
54 }
55 
56 static u8 * format_af_packet_device (u8 * s, va_list * args)
57 {
58  s = format (s, "Linux PACKET socket interface");
59  return s;
60 }
61 
62 static u8 * format_af_packet_tx_trace (u8 * s, va_list * args)
63 {
64  s = format (s, "Unimplemented...");
65  return s;
66 }
67 
68 static uword
70  vlib_node_runtime_t * node,
71  vlib_frame_t * frame)
72 {
74  u32 * buffers = vlib_frame_args (frame);
75  u32 n_left = frame->n_vectors;
76  u32 n_sent = 0;
77  vnet_interface_output_runtime_t * rd = (void *) node->runtime_data;
79  int block = 0;
80  u32 block_size = apif->tx_req->tp_block_size;
81  u32 frame_size = apif->tx_req->tp_frame_size;
82  u32 frame_num = apif->tx_req->tp_frame_nr;
83  u8 * block_start = apif->tx_ring + block * block_size;
84  u32 tx_frame = apif->next_tx_frame;
85  struct tpacket2_hdr * tph;
86  u32 frame_not_ready = 0;
87 
88  while(n_left > 0)
89  {
90  u32 len;
91  u32 offset = 0;
92  vlib_buffer_t * b0;
93  n_left--;
94  u32 bi = buffers[0];
95  buffers++;
96 
97  tph = (struct tpacket2_hdr *) (block_start + tx_frame * frame_size);
98 
99  if(tph->tp_status & (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING))
100  {
101  frame_not_ready++;
102  goto next;
103  }
104 
105  do
106  {
107  b0 = vlib_get_buffer (vm, bi);
108  len = b0->current_length;
109  clib_memcpy((u8 *) tph + TPACKET_ALIGN(sizeof(struct tpacket2_hdr)) + offset,
110  vlib_buffer_get_current(b0), len);
111  offset += len;
112  }
113  while ((bi = b0->next_buffer));
114 
115  tph->tp_len = tph->tp_snaplen = offset;
116  tph->tp_status = TP_STATUS_SEND_REQUEST;
117  n_sent++;
118 next:
119  tx_frame = (tx_frame + 1) % frame_num;
120  }
121 
123 
124  if (n_sent)
125  {
126  apif->next_tx_frame = tx_frame;
127  if (sendto(apif->fd, NULL, 0, MSG_DONTWAIT, NULL, 0) == -1)
128  clib_unix_error("tx sendto failure");
129  }
130 
131  if (frame_not_ready)
132  vlib_error_count (vm, node->node_index, AF_PACKET_TX_ERROR_FRAME_NOT_READY,
133  frame_not_ready);
134 
135  vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
136  return frame->n_vectors;
137 }
138 
139 static void
141  u32 node_index)
142 {
144  vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
146 
147  /* Shut off redirection */
148  if (node_index == ~0)
149  {
150  apif->per_interface_next_index = node_index;
151  return;
152  }
153 
156 }
157 
159 {
160  /* Nothing for now */
161 }
162 
163 static clib_error_t *
165 {
167  vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
169  u32 hw_flags;
170 
171  apif->is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
172 
173  if (apif->is_admin_up)
175  else
176  hw_flags = 0;
177 
178  vnet_hw_interface_set_flags(vnm, hw_if_index, hw_flags);
179 
180  return 0;
181 }
182 
183 static clib_error_t *
185  u32 hw_if_index,
186  struct vnet_sw_interface_t * st,
187  int is_add)
188 {
189  /* Nothing for now */
190  return 0;
191 }
192 
194  .name = "af-packet",
195  .tx_function = af_packet_interface_tx,
196  .format_device_name = format_af_packet_device_name,
197  .format_device = format_af_packet_device,
198  .format_tx_trace = format_af_packet_tx_trace,
199  .tx_function_n_errors = AF_PACKET_TX_N_ERROR,
200  .tx_function_error_strings = af_packet_tx_func_error_strings,
201  .rx_redirect_to_node = af_packet_set_interface_next_node,
202  .clear_counters = af_packet_clear_hw_interface_counters,
203  .admin_up_down_function = af_packet_interface_admin_up_down,
204  .subif_add_del_function = af_packet_subif_add_del_function,
205  .no_flatten_output_chains = 1,
206 };
static void af_packet_clear_hw_interface_counters(u32 instance)
Definition: device.c:158
always_inline void vlib_error_count(vlib_main_t *vm, uword node_index, uword counter, uword increment)
Definition: error_funcs.h:54
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:267
clib_error_t * vnet_hw_interface_set_flags(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: interface.c:454
static u8 * format_af_packet_tx_trace(u8 *s, va_list *args)
Definition: device.c:62
#define NULL
Definition: clib.h:55
#define VNET_HW_INTERFACE_FLAG_LINK_UP
Definition: interface.h:241
always_inline void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:184
always_inline vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
#define foreach_af_packet_tx_func_error
Definition: device.c:29
static u8 * format_af_packet_device(u8 *s, va_list *args)
Definition: device.c:56
u8 * host_if_name
Definition: af_packet.h:22
VNET_DEVICE_CLASS(af_packet_device_class)
af_packet_if_t * interfaces
Definition: af_packet.h:41
static uword af_packet_interface_tx(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: device.c:69
static void af_packet_set_interface_next_node(vnet_main_t *vnm, u32 hw_if_index, u32 node_index)
Definition: device.c:140
#define pool_elt_at_index(p, i)
Definition: pool.h:346
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.c:1060
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:81
static clib_error_t * af_packet_interface_admin_up_down(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: device.c:164
static u8 * format_af_packet_device_name(u8 *s, va_list *args)
Definition: device.c:46
af_packet_tx_func_error_t
Definition: device.c:32
always_inline void * vlib_frame_args(vlib_frame_t *f)
Definition: node_funcs.h:209
u16 n_vectors
Definition: node.h:307
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:184
#define clib_memcpy(a, b, c)
Definition: string.h:63
u32 per_interface_next_index
Definition: af_packet.h:35
vlib_node_registration_t af_packet_input_node
(constructor) VLIB_REGISTER_NODE (af_packet_input_node)
Definition: node.c:271
always_inline vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
#define VNET_SW_INTERFACE_FLAG_ADMIN_UP
Definition: interface.h:373
unsigned int u32
Definition: types.h:88
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:405
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:112
af_packet_main_t af_packet_main
Definition: af_packet.h:53
#define clib_unix_error(format, args...)
Definition: error.h:65
always_inline uword vlib_node_add_next(vlib_main_t *vm, uword node, uword next_node)
Definition: node_funcs.h:919
vnet_device_class_t af_packet_device_class
u64 uword
Definition: types.h:112
unsigned char u8
Definition: types.h:56
#define CLIB_MEMORY_BARRIER()
Definition: clib.h:101
u32 flags
Definition: vhost-user.h:73
uword runtime_data[(128-1 *sizeof(vlib_node_function_t *)-1 *sizeof(vlib_error_t *)-11 *sizeof(u32)-5 *sizeof(u16))/sizeof(uword)]
Definition: node.h:432
always_inline vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:69
static char * af_packet_tx_func_error_strings[]
Definition: device.c:39