VPP-84 af_packet retry on EAGAIN, count on errors
[vpp.git] / vnet / vnet / devices / af_packet / device.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_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
27 #include <vnet/devices/af_packet/af_packet.h>
28
29 #define foreach_af_packet_tx_func_error               \
30 _(FRAME_NOT_READY, "tx frame not ready")              \
31 _(TXRING_EAGAIN,   "tx sendto temporary failure")     \
32 _(TXRING_FATAL,    "tx sendto fatal failure")         \
33 _(TXRING_OVERRUN,  "tx ring overrun")
34
35 typedef enum {
36 #define _(f,s) AF_PACKET_TX_ERROR_##f,
37   foreach_af_packet_tx_func_error
38 #undef _
39   AF_PACKET_TX_N_ERROR,
40 } af_packet_tx_func_error_t;
41
42 static char * af_packet_tx_func_error_strings[] = {
43 #define _(n,s) s,
44     foreach_af_packet_tx_func_error
45 #undef _
46 };
47
48
49 static u8 * format_af_packet_device_name (u8 * s, va_list * args)
50 {
51   u32 i = va_arg (*args, u32);
52   af_packet_main_t * apm = &af_packet_main;
53   af_packet_if_t * apif = pool_elt_at_index (apm->interfaces, i);
54
55   s = format (s, "host-%s", apif->host_if_name);
56   return s;
57 }
58
59 static u8 * format_af_packet_device (u8 * s, va_list * args)
60 {
61   s = format (s, "Linux PACKET socket interface");
62   return s;
63 }
64
65 static u8 * format_af_packet_tx_trace (u8 * s, va_list * args)
66 {
67   s = format (s, "Unimplemented...");
68   return s;
69 }
70
71 static uword
72 af_packet_interface_tx (vlib_main_t * vm,
73                        vlib_node_runtime_t * node,
74                        vlib_frame_t * frame)
75 {
76   af_packet_main_t * apm = &af_packet_main;
77   u32 * buffers = vlib_frame_args (frame);
78   u32 n_left = frame->n_vectors;
79   u32 n_sent = 0;
80   vnet_interface_output_runtime_t * rd = (void *) node->runtime_data;
81   af_packet_if_t * apif = pool_elt_at_index (apm->interfaces, rd->dev_instance);
82   int block = 0;
83   u32 block_size = apif->tx_req->tp_block_size;
84   u32 frame_size = apif->tx_req->tp_frame_size;
85   u32 frame_num = apif->tx_req->tp_frame_nr;
86   u8 * block_start = apif->tx_ring + block * block_size;
87   u32 tx_frame = apif->next_tx_frame;
88   struct tpacket2_hdr * tph;
89   u32 frame_not_ready = 0;
90
91   while(n_left > 0)
92     {
93       u32 len;
94       u32 offset = 0;
95       vlib_buffer_t * b0;
96       n_left--;
97       u32 bi = buffers[0];
98       buffers++;
99
100       tph = (struct tpacket2_hdr *) (block_start + tx_frame * frame_size);
101
102       if (PREDICT_FALSE(tph->tp_status & (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING)))
103         {
104           frame_not_ready++;
105           goto next;
106         }
107
108       do
109         {
110           b0 = vlib_get_buffer (vm, bi);
111           len = b0->current_length;
112           clib_memcpy((u8 *) tph + TPACKET_ALIGN(sizeof(struct tpacket2_hdr)) + offset,
113                  vlib_buffer_get_current(b0), len);
114           offset += len;
115         }
116       while ((bi = b0->next_buffer));
117
118       tph->tp_len = tph->tp_snaplen = offset;
119       tph->tp_status = TP_STATUS_SEND_REQUEST;
120       n_sent++;
121 next:
122       /* check if we've exhausted the ring */
123       if (PREDICT_FALSE(frame_not_ready + n_sent == frame_num))
124         break;
125
126       tx_frame = (tx_frame + 1) % frame_num;
127     }
128
129   CLIB_MEMORY_BARRIER();
130
131   if (PREDICT_TRUE(n_sent))
132     {
133       apif->next_tx_frame = tx_frame;
134
135       if (PREDICT_FALSE(sendto(apif->fd, NULL, 0,
136                                MSG_DONTWAIT, NULL, 0) == -1))
137         {
138           /* Uh-oh, drop & move on, but count whether it was fatal or not.
139            * Note that we have no reliable way to properly determine the
140            * disposition of the packets we just enqueued for delivery.
141            */
142           vlib_error_count (vm, node->node_index,
143                             unix_error_is_fatal(errno) ?
144                             AF_PACKET_TX_ERROR_TXRING_FATAL :
145                             AF_PACKET_TX_ERROR_TXRING_EAGAIN,
146                             n_sent);
147         }
148     }
149
150   if (PREDICT_FALSE(frame_not_ready))
151     vlib_error_count (vm, node->node_index, AF_PACKET_TX_ERROR_FRAME_NOT_READY,
152                       frame_not_ready);
153
154   if (PREDICT_FALSE(frame_not_ready + n_sent == frame_num))
155     vlib_error_count (vm, node->node_index, AF_PACKET_TX_ERROR_TXRING_OVERRUN,
156                       n_left);
157
158   vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
159   return frame->n_vectors;
160 }
161
162 static void
163 af_packet_set_interface_next_node (vnet_main_t *vnm, u32 hw_if_index,
164                                   u32 node_index)
165 {
166   af_packet_main_t * apm = &af_packet_main;
167   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
168   af_packet_if_t * apif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
169
170   /* Shut off redirection */
171   if (node_index == ~0)
172     {
173       apif->per_interface_next_index = node_index;
174       return;
175     }
176
177   apif->per_interface_next_index =
178     vlib_node_add_next (vlib_get_main(), af_packet_input_node.index, node_index);
179 }
180
181 static void af_packet_clear_hw_interface_counters (u32 instance)
182 {
183   /* Nothing for now */
184 }
185
186 static clib_error_t *
187 af_packet_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
188 {
189   af_packet_main_t * apm = &af_packet_main;
190   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
191   af_packet_if_t * apif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
192   u32 hw_flags;
193
194   apif->is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
195
196   if (apif->is_admin_up)
197     hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP;
198   else
199     hw_flags = 0;
200
201   vnet_hw_interface_set_flags(vnm, hw_if_index, hw_flags);
202
203   return 0;
204 }
205
206 static clib_error_t *
207 af_packet_subif_add_del_function (vnet_main_t * vnm,
208                                  u32 hw_if_index,
209                                  struct vnet_sw_interface_t * st,
210                                  int is_add)
211 {
212   /* Nothing for now */
213   return 0;
214 }
215
216 VNET_DEVICE_CLASS (af_packet_device_class) = {
217   .name = "af-packet",
218   .tx_function = af_packet_interface_tx,
219   .format_device_name = format_af_packet_device_name,
220   .format_device = format_af_packet_device,
221   .format_tx_trace = format_af_packet_tx_trace,
222   .tx_function_n_errors = AF_PACKET_TX_N_ERROR,
223   .tx_function_error_strings = af_packet_tx_func_error_strings,
224   .rx_redirect_to_node = af_packet_set_interface_next_node,
225   .clear_counters = af_packet_clear_hw_interface_counters,
226   .admin_up_down_function = af_packet_interface_admin_up_down,
227   .subif_add_del_function = af_packet_subif_add_del_function,
228   .no_flatten_output_chains = 1,
229 };
230
231 VLIB_DEVICE_TX_FUNCTION_MULTIARCH (af_packet_device_class,
232                                    af_packet_interface_tx)