Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 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> |
Ray Kinsella | 2038ad0 | 2017-05-18 11:56:28 +0100 | [diff] [blame] | 21 | #include <sys/socket.h> |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 22 | #include <sys/ioctl.h> |
| 23 | #include <net/if.h> |
Ray Kinsella | 2038ad0 | 2017-05-18 11:56:28 +0100 | [diff] [blame] | 24 | #include <net/if_arp.h> |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 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 | |
| 31 | #include <vnet/devices/af_packet/af_packet.h> |
| 32 | |
Chris Luke | 4b46c84 | 2016-05-23 21:30:26 -0400 | [diff] [blame] | 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") |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 38 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 39 | typedef enum |
| 40 | { |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 41 | #define _(f,s) AF_PACKET_TX_ERROR_##f, |
| 42 | foreach_af_packet_tx_func_error |
| 43 | #undef _ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 44 | AF_PACKET_TX_N_ERROR, |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 45 | } af_packet_tx_func_error_t; |
| 46 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 47 | static char *af_packet_tx_func_error_strings[] = { |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 48 | #define _(n,s) s, |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 49 | foreach_af_packet_tx_func_error |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 50 | #undef _ |
| 51 | }; |
| 52 | |
Damjan Marion | c517020 | 2017-10-11 14:15:47 +0200 | [diff] [blame] | 53 | |
Filip Tehlar | aee7364 | 2019-03-13 05:50:44 -0700 | [diff] [blame] | 54 | #ifndef CLIB_MARCH_VARIANT |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 55 | u8 * |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 56 | format_af_packet_device_name (u8 * s, va_list * args) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 57 | { |
| 58 | u32 i = va_arg (*args, u32); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 59 | af_packet_main_t *apm = &af_packet_main; |
| 60 | af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, i); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 61 | |
| 62 | s = format (s, "host-%s", apif->host_if_name); |
| 63 | return s; |
| 64 | } |
Filip Tehlar | aee7364 | 2019-03-13 05:50:44 -0700 | [diff] [blame] | 65 | #endif /* CLIB_MARCH_VARIANT */ |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 66 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 67 | static u8 * |
| 68 | format_af_packet_device (u8 * s, va_list * args) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 69 | { |
Nathan Skrzypczak | ffc6bdc | 2021-02-01 17:13:59 +0100 | [diff] [blame] | 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 | |
| 74 | af_packet_main_t *apm = &af_packet_main; |
| 75 | af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, dev_instance); |
| 76 | clib_spinlock_lock_if_init (&apif->lockp); |
| 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 | |
| 111 | clib_spinlock_unlock_if_init (&apif->lockp); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 112 | return s; |
| 113 | } |
| 114 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 115 | static u8 * |
| 116 | format_af_packet_tx_trace (u8 * s, va_list * args) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 117 | { |
| 118 | s = format (s, "Unimplemented..."); |
| 119 | return s; |
| 120 | } |
| 121 | |
Filip Tehlar | aee7364 | 2019-03-13 05:50:44 -0700 | [diff] [blame] | 122 | VNET_DEVICE_CLASS_TX_FN (af_packet_device_class) (vlib_main_t * vm, |
| 123 | vlib_node_runtime_t * node, |
| 124 | vlib_frame_t * frame) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 125 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 126 | af_packet_main_t *apm = &af_packet_main; |
Damjan Marion | a3d5986 | 2018-11-10 10:23:00 +0100 | [diff] [blame] | 127 | u32 *buffers = vlib_frame_vector_args (frame); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 128 | u32 n_left = frame->n_vectors; |
| 129 | u32 n_sent = 0; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 130 | vnet_interface_output_runtime_t *rd = (void *) node->runtime_data; |
| 131 | af_packet_if_t *apif = |
| 132 | pool_elt_at_index (apm->interfaces, rd->dev_instance); |
Pierre Pfister | 8cedff2 | 2018-02-07 15:48:21 +0100 | [diff] [blame] | 133 | clib_spinlock_lock_if_init (&apif->lockp); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 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; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 138 | u8 *block_start = apif->tx_ring + block * block_size; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 139 | u32 tx_frame = apif->next_tx_frame; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 140 | struct tpacket2_hdr *tph; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 141 | u32 frame_not_ready = 0; |
| 142 | |
Nathan Skrzypczak | ffc6bdc | 2021-02-01 17:13:59 +0100 | [diff] [blame] | 143 | while (n_left) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 144 | { |
| 145 | u32 len; |
| 146 | u32 offset = 0; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 147 | vlib_buffer_t *b0; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 148 | n_left--; |
| 149 | u32 bi = buffers[0]; |
| 150 | buffers++; |
| 151 | |
| 152 | tph = (struct tpacket2_hdr *) (block_start + tx_frame * frame_size); |
Nathan Skrzypczak | ffc6bdc | 2021-02-01 17:13:59 +0100 | [diff] [blame] | 153 | if (PREDICT_FALSE (tph->tp_status & |
| 154 | (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING))) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 155 | { |
| 156 | frame_not_ready++; |
Mohammed Hawari | eed6fc9 | 2021-09-06 11:48:17 +0200 | [diff] [blame] | 157 | goto next; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | do |
| 161 | { |
| 162 | b0 = vlib_get_buffer (vm, bi); |
| 163 | len = b0->current_length; |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 164 | clib_memcpy_fast ((u8 *) tph + |
| 165 | TPACKET_ALIGN (sizeof (struct tpacket2_hdr)) + |
| 166 | offset, vlib_buffer_get_current (b0), len); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 167 | offset += len; |
| 168 | } |
Jim Gibson | 22db11b | 2017-03-27 19:46:12 +0000 | [diff] [blame] | 169 | while ((bi = |
| 170 | (b0->flags & VLIB_BUFFER_NEXT_PRESENT) ? b0->next_buffer : 0)); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 171 | |
| 172 | tph->tp_len = tph->tp_snaplen = offset; |
| 173 | tph->tp_status = TP_STATUS_SEND_REQUEST; |
| 174 | n_sent++; |
Nathan Skrzypczak | ffc6bdc | 2021-02-01 17:13:59 +0100 | [diff] [blame] | 175 | |
Florin Coras | 837503c | 2017-11-28 11:59:20 -0800 | [diff] [blame] | 176 | tx_frame = (tx_frame + 1) % frame_num; |
| 177 | |
Mohammed Hawari | eed6fc9 | 2021-09-06 11:48:17 +0200 | [diff] [blame] | 178 | next: |
Chris Luke | 4b46c84 | 2016-05-23 21:30:26 -0400 | [diff] [blame] | 179 | /* check if we've exhausted the ring */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 180 | if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num)) |
| 181 | break; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 182 | } |
| 183 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 184 | CLIB_MEMORY_BARRIER (); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 185 | |
Nathan Skrzypczak | ffc6bdc | 2021-02-01 17:13:59 +0100 | [diff] [blame] | 186 | if (PREDICT_TRUE (n_sent)) |
Mohammed Hawari | eed6fc9 | 2021-09-06 11:48:17 +0200 | [diff] [blame] | 187 | { |
| 188 | apif->next_tx_frame = tx_frame; |
| 189 | |
| 190 | if (PREDICT_FALSE (sendto (apif->fd, NULL, 0, MSG_DONTWAIT, NULL, 0) == |
| 191 | -1)) |
| 192 | { |
| 193 | /* Uh-oh, drop & move on, but count whether it was fatal or not. |
| 194 | * Note that we have no reliable way to properly determine the |
| 195 | * disposition of the packets we just enqueued for delivery. |
| 196 | */ |
| 197 | vlib_error_count (vm, node->node_index, |
| 198 | unix_error_is_fatal (errno) ? |
| 199 | AF_PACKET_TX_ERROR_TXRING_FATAL : |
| 200 | AF_PACKET_TX_ERROR_TXRING_EAGAIN, |
| 201 | n_sent); |
| 202 | } |
| 203 | } |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 204 | |
Damjan Marion | 1927da2 | 2017-03-27 17:08:20 +0200 | [diff] [blame] | 205 | clib_spinlock_unlock_if_init (&apif->lockp); |
Mohsin KAZMI | cf751ec | 2017-01-18 11:59:45 +0100 | [diff] [blame] | 206 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 207 | if (PREDICT_FALSE (frame_not_ready)) |
| 208 | vlib_error_count (vm, node->node_index, |
| 209 | AF_PACKET_TX_ERROR_FRAME_NOT_READY, frame_not_ready); |
Chris Luke | 4b46c84 | 2016-05-23 21:30:26 -0400 | [diff] [blame] | 210 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 211 | if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num)) |
Chris Luke | 4b46c84 | 2016-05-23 21:30:26 -0400 | [diff] [blame] | 212 | vlib_error_count (vm, node->node_index, AF_PACKET_TX_ERROR_TXRING_OVERRUN, |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 213 | n_left); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 214 | |
Damjan Marion | a3d5986 | 2018-11-10 10:23:00 +0100 | [diff] [blame] | 215 | vlib_buffer_free (vm, vlib_frame_vector_args (frame), frame->n_vectors); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 216 | return frame->n_vectors; |
| 217 | } |
| 218 | |
| 219 | static void |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 220 | af_packet_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index, |
| 221 | u32 node_index) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 222 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 223 | af_packet_main_t *apm = &af_packet_main; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 224 | vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 225 | af_packet_if_t *apif = |
| 226 | pool_elt_at_index (apm->interfaces, hw->dev_instance); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 227 | |
| 228 | /* Shut off redirection */ |
| 229 | if (node_index == ~0) |
| 230 | { |
| 231 | apif->per_interface_next_index = node_index; |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | apif->per_interface_next_index = |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 236 | vlib_node_add_next (vlib_get_main (), af_packet_input_node.index, |
| 237 | node_index); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 238 | } |
| 239 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 240 | static void |
| 241 | af_packet_clear_hw_interface_counters (u32 instance) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 242 | { |
| 243 | /* Nothing for now */ |
| 244 | } |
| 245 | |
| 246 | static clib_error_t * |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 247 | af_packet_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, |
| 248 | u32 flags) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 249 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 250 | af_packet_main_t *apm = &af_packet_main; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 251 | vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 252 | af_packet_if_t *apif = |
| 253 | pool_elt_at_index (apm->interfaces, hw->dev_instance); |
Alpesh Patel | 83cc4e1 | 2016-04-05 12:49:30 -0700 | [diff] [blame] | 254 | u32 hw_flags; |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 255 | int rv, fd = socket (AF_UNIX, SOCK_DGRAM, 0); |
| 256 | struct ifreq ifr; |
| 257 | |
Ray Kinsella | e6cc9cc | 2017-05-20 13:42:30 +0100 | [diff] [blame] | 258 | if (0 > fd) |
| 259 | { |
Mohsin Kazmi | acba9f7 | 2018-05-17 15:42:27 +0200 | [diff] [blame] | 260 | vlib_log_warn (apm->log_class, "af_packet_%s could not open socket", |
| 261 | apif->host_if_name); |
Ray Kinsella | e6cc9cc | 2017-05-20 13:42:30 +0100 | [diff] [blame] | 262 | return 0; |
| 263 | } |
| 264 | |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 265 | /* if interface is a bridge ignore */ |
| 266 | if (apif->host_if_index < 0) |
Ray Kinsella | 2038ad0 | 2017-05-18 11:56:28 +0100 | [diff] [blame] | 267 | goto error; /* no error */ |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 268 | |
| 269 | /* use host_if_index in case host name has changed */ |
| 270 | ifr.ifr_ifindex = apif->host_if_index; |
| 271 | if ((rv = ioctl (fd, SIOCGIFNAME, &ifr)) < 0) |
| 272 | { |
Mohsin Kazmi | acba9f7 | 2018-05-17 15:42:27 +0200 | [diff] [blame] | 273 | vlib_log_warn (apm->log_class, |
| 274 | "af_packet_%s ioctl could not retrieve eth name", |
| 275 | apif->host_if_name); |
Ray Kinsella | 2038ad0 | 2017-05-18 11:56:28 +0100 | [diff] [blame] | 276 | goto error; |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 277 | } |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 278 | |
| 279 | apif->is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0; |
| 280 | |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 281 | if ((rv = ioctl (fd, SIOCGIFFLAGS, &ifr)) < 0) |
| 282 | { |
Mohsin Kazmi | acba9f7 | 2018-05-17 15:42:27 +0200 | [diff] [blame] | 283 | vlib_log_warn (apm->log_class, "af_packet_%s error: %d", |
| 284 | apif->is_admin_up ? "up" : "down", rv); |
Ray Kinsella | 2038ad0 | 2017-05-18 11:56:28 +0100 | [diff] [blame] | 285 | goto error; |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 286 | } |
| 287 | |
Alpesh Patel | 83cc4e1 | 2016-04-05 12:49:30 -0700 | [diff] [blame] | 288 | if (apif->is_admin_up) |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 289 | { |
| 290 | hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP; |
| 291 | ifr.ifr_flags |= IFF_UP; |
| 292 | } |
Alpesh Patel | 83cc4e1 | 2016-04-05 12:49:30 -0700 | [diff] [blame] | 293 | else |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 294 | { |
| 295 | hw_flags = 0; |
| 296 | ifr.ifr_flags &= ~IFF_UP; |
| 297 | } |
| 298 | |
| 299 | if ((rv = ioctl (fd, SIOCSIFFLAGS, &ifr)) < 0) |
| 300 | { |
Mohsin Kazmi | acba9f7 | 2018-05-17 15:42:27 +0200 | [diff] [blame] | 301 | vlib_log_warn (apm->log_class, "af_packet_%s error: %d", |
| 302 | apif->is_admin_up ? "up" : "down", rv); |
Ray Kinsella | 2038ad0 | 2017-05-18 11:56:28 +0100 | [diff] [blame] | 303 | goto error; |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 304 | } |
Alpesh Patel | 83cc4e1 | 2016-04-05 12:49:30 -0700 | [diff] [blame] | 305 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 306 | vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags); |
Alpesh Patel | 83cc4e1 | 2016-04-05 12:49:30 -0700 | [diff] [blame] | 307 | |
Ray Kinsella | 2038ad0 | 2017-05-18 11:56:28 +0100 | [diff] [blame] | 308 | error: |
Ray Kinsella | e6cc9cc | 2017-05-20 13:42:30 +0100 | [diff] [blame] | 309 | if (0 <= fd) |
| 310 | close (fd); |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 311 | |
| 312 | return 0; /* no error */ |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | static clib_error_t * |
| 316 | af_packet_subif_add_del_function (vnet_main_t * vnm, |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 317 | u32 hw_if_index, |
| 318 | struct vnet_sw_interface_t *st, int is_add) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 319 | { |
| 320 | /* Nothing for now */ |
| 321 | return 0; |
| 322 | } |
| 323 | |
Ray Kinsella | 2038ad0 | 2017-05-18 11:56:28 +0100 | [diff] [blame] | 324 | static clib_error_t *af_packet_set_mac_address_function |
Neale Ranns | 3b81a1e | 2018-09-06 09:50:26 -0700 | [diff] [blame] | 325 | (struct vnet_hw_interface_t *hi, const u8 * old_address, const u8 * address) |
Ray Kinsella | 2038ad0 | 2017-05-18 11:56:28 +0100 | [diff] [blame] | 326 | { |
| 327 | af_packet_main_t *apm = &af_packet_main; |
| 328 | af_packet_if_t *apif = |
| 329 | pool_elt_at_index (apm->interfaces, hi->dev_instance); |
| 330 | int rv, fd = socket (AF_UNIX, SOCK_DGRAM, 0); |
| 331 | struct ifreq ifr; |
| 332 | |
Ray Kinsella | e6cc9cc | 2017-05-20 13:42:30 +0100 | [diff] [blame] | 333 | if (0 > fd) |
| 334 | { |
Mohsin Kazmi | acba9f7 | 2018-05-17 15:42:27 +0200 | [diff] [blame] | 335 | vlib_log_warn (apm->log_class, "af_packet_%s could not open socket", |
| 336 | apif->host_if_name); |
Ray Kinsella | e6cc9cc | 2017-05-20 13:42:30 +0100 | [diff] [blame] | 337 | return 0; |
| 338 | } |
| 339 | |
Ray Kinsella | 2038ad0 | 2017-05-18 11:56:28 +0100 | [diff] [blame] | 340 | /* if interface is a bridge ignore */ |
| 341 | if (apif->host_if_index < 0) |
| 342 | goto error; /* no error */ |
| 343 | |
| 344 | /* use host_if_index in case host name has changed */ |
| 345 | ifr.ifr_ifindex = apif->host_if_index; |
| 346 | if ((rv = ioctl (fd, SIOCGIFNAME, &ifr)) < 0) |
| 347 | { |
Mohsin Kazmi | acba9f7 | 2018-05-17 15:42:27 +0200 | [diff] [blame] | 348 | vlib_log_warn |
| 349 | (apm->log_class, |
| 350 | "af_packet_%s ioctl could not retrieve eth name, error: %d", |
Ray Kinsella | 2038ad0 | 2017-05-18 11:56:28 +0100 | [diff] [blame] | 351 | apif->host_if_name, rv); |
| 352 | goto error; |
| 353 | } |
| 354 | |
| 355 | clib_memcpy (ifr.ifr_hwaddr.sa_data, address, 6); |
| 356 | ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER; |
| 357 | |
| 358 | if ((rv = ioctl (fd, SIOCSIFHWADDR, &ifr)) < 0) |
| 359 | { |
Mohsin Kazmi | acba9f7 | 2018-05-17 15:42:27 +0200 | [diff] [blame] | 360 | vlib_log_warn (apm->log_class, |
| 361 | "af_packet_%s ioctl could not set mac, error: %d", |
| 362 | apif->host_if_name, rv); |
Ray Kinsella | 2038ad0 | 2017-05-18 11:56:28 +0100 | [diff] [blame] | 363 | goto error; |
| 364 | } |
| 365 | |
| 366 | error: |
Ray Kinsella | e6cc9cc | 2017-05-20 13:42:30 +0100 | [diff] [blame] | 367 | |
| 368 | if (0 <= fd) |
| 369 | close (fd); |
Ray Kinsella | 2038ad0 | 2017-05-18 11:56:28 +0100 | [diff] [blame] | 370 | |
| 371 | return 0; /* no error */ |
| 372 | } |
| 373 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 374 | /* *INDENT-OFF* */ |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 375 | VNET_DEVICE_CLASS (af_packet_device_class) = { |
| 376 | .name = "af-packet", |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 377 | .format_device_name = format_af_packet_device_name, |
| 378 | .format_device = format_af_packet_device, |
| 379 | .format_tx_trace = format_af_packet_tx_trace, |
| 380 | .tx_function_n_errors = AF_PACKET_TX_N_ERROR, |
| 381 | .tx_function_error_strings = af_packet_tx_func_error_strings, |
| 382 | .rx_redirect_to_node = af_packet_set_interface_next_node, |
| 383 | .clear_counters = af_packet_clear_hw_interface_counters, |
| 384 | .admin_up_down_function = af_packet_interface_admin_up_down, |
| 385 | .subif_add_del_function = af_packet_subif_add_del_function, |
Ray Kinsella | 2038ad0 | 2017-05-18 11:56:28 +0100 | [diff] [blame] | 386 | .mac_addr_change_function = af_packet_set_mac_address_function, |
Alpesh Patel | 83cc4e1 | 2016-04-05 12:49:30 -0700 | [diff] [blame] | 387 | }; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 388 | /* *INDENT-ON* */ |
| 389 | |
| 390 | /* |
| 391 | * fd.io coding-style-patch-verification: ON |
| 392 | * |
| 393 | * Local Variables: |
| 394 | * eval: (c-set-style "gnu") |
| 395 | * End: |
| 396 | */ |