2 *------------------------------------------------------------------
3 * af_packet.c - linux kernel packet interface
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
20 #include <linux/if_packet.h>
22 #include <vlib/vlib.h>
23 #include <vlib/unix/unix.h>
24 #include <vnet/ip/ip.h>
25 #include <vnet/ethernet/ethernet.h>
27 #include <vnet/devices/af_packet/af_packet.h>
29 #define foreach_af_packet_tx_func_error \
30 _(FRAME_NOT_READY, "tx frame not ready")
33 #define _(f,s) AF_PACKET_TX_ERROR_##f,
34 foreach_af_packet_tx_func_error
37 } af_packet_tx_func_error_t;
39 static char * af_packet_tx_func_error_strings[] = {
41 foreach_af_packet_tx_func_error
46 static u8 * format_af_packet_device_name (u8 * s, va_list * args)
48 u32 i = va_arg (*args, u32);
49 af_packet_main_t * apm = &af_packet_main;
50 af_packet_if_t * apif = pool_elt_at_index (apm->interfaces, i);
52 s = format (s, "host-%s", apif->host_if_name);
56 static u8 * format_af_packet_device (u8 * s, va_list * args)
58 s = format (s, "Linux PACKET socket interface");
62 static u8 * format_af_packet_tx_trace (u8 * s, va_list * args)
64 s = format (s, "Unimplemented...");
69 af_packet_interface_tx (vlib_main_t * vm,
70 vlib_node_runtime_t * node,
73 af_packet_main_t * apm = &af_packet_main;
74 u32 * buffers = vlib_frame_args (frame);
75 u32 n_left = frame->n_vectors;
77 vnet_interface_output_runtime_t * rd = (void *) node->runtime_data;
78 af_packet_if_t * apif = pool_elt_at_index (apm->interfaces, rd->dev_instance);
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;
97 tph = (struct tpacket2_hdr *) (block_start + tx_frame * frame_size);
99 if(tph->tp_status & (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING))
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);
113 while ((bi = b0->next_buffer));
115 tph->tp_len = tph->tp_snaplen = offset;
116 tph->tp_status = TP_STATUS_SEND_REQUEST;
119 tx_frame = (tx_frame + 1) % frame_num;
122 CLIB_MEMORY_BARRIER();
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");
132 vlib_error_count (vm, node->node_index, AF_PACKET_TX_ERROR_FRAME_NOT_READY,
135 vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
136 return frame->n_vectors;
140 af_packet_set_interface_next_node (vnet_main_t *vnm, u32 hw_if_index,
143 af_packet_main_t * apm = &af_packet_main;
144 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
145 af_packet_if_t * apif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
147 /* Shut off redirection */
148 if (node_index == ~0)
150 apif->per_interface_next_index = node_index;
154 apif->per_interface_next_index =
155 vlib_node_add_next (vlib_get_main(), af_packet_input_node.index, node_index);
158 static void af_packet_clear_hw_interface_counters (u32 instance)
160 /* Nothing for now */
163 static clib_error_t *
164 af_packet_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
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);
171 apif->is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
173 if (apif->is_admin_up)
174 hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP;
178 vnet_hw_interface_set_flags(vnm, hw_if_index, hw_flags);
183 static clib_error_t *
184 af_packet_subif_add_del_function (vnet_main_t * vnm,
186 struct vnet_sw_interface_t * st,
189 /* Nothing for now */
193 VNET_DEVICE_CLASS (af_packet_device_class) = {
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,
208 VLIB_DEVICE_TX_FUNCTION_MULTIARCH (af_packet_device_class,
209 af_packet_interface_tx)