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 | |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 54 | u8 * |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 55 | format_af_packet_device_name (u8 * s, va_list * args) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 56 | { |
| 57 | u32 i = va_arg (*args, u32); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 58 | af_packet_main_t *apm = &af_packet_main; |
| 59 | af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, i); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 60 | |
| 61 | s = format (s, "host-%s", apif->host_if_name); |
| 62 | return s; |
| 63 | } |
| 64 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 65 | static u8 * |
| 66 | format_af_packet_device (u8 * s, va_list * args) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 67 | { |
| 68 | s = format (s, "Linux PACKET socket interface"); |
| 69 | return s; |
| 70 | } |
| 71 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 72 | static u8 * |
| 73 | format_af_packet_tx_trace (u8 * s, va_list * args) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 74 | { |
| 75 | s = format (s, "Unimplemented..."); |
| 76 | return s; |
| 77 | } |
| 78 | |
| 79 | static uword |
| 80 | af_packet_interface_tx (vlib_main_t * vm, |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 81 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 82 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 83 | af_packet_main_t *apm = &af_packet_main; |
| 84 | u32 *buffers = vlib_frame_args (frame); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 85 | u32 n_left = frame->n_vectors; |
| 86 | u32 n_sent = 0; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 87 | vnet_interface_output_runtime_t *rd = (void *) node->runtime_data; |
| 88 | af_packet_if_t *apif = |
| 89 | pool_elt_at_index (apm->interfaces, rd->dev_instance); |
Pierre Pfister | 8cedff2 | 2018-02-07 15:48:21 +0100 | [diff] [blame] | 90 | clib_spinlock_lock_if_init (&apif->lockp); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 91 | int block = 0; |
| 92 | u32 block_size = apif->tx_req->tp_block_size; |
| 93 | u32 frame_size = apif->tx_req->tp_frame_size; |
| 94 | u32 frame_num = apif->tx_req->tp_frame_nr; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 95 | u8 *block_start = apif->tx_ring + block * block_size; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 96 | u32 tx_frame = apif->next_tx_frame; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 97 | struct tpacket2_hdr *tph; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 98 | u32 frame_not_ready = 0; |
| 99 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 100 | while (n_left > 0) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 101 | { |
| 102 | u32 len; |
| 103 | u32 offset = 0; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 104 | vlib_buffer_t *b0; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 105 | n_left--; |
| 106 | u32 bi = buffers[0]; |
| 107 | buffers++; |
| 108 | |
| 109 | tph = (struct tpacket2_hdr *) (block_start + tx_frame * frame_size); |
| 110 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 111 | if (PREDICT_FALSE |
| 112 | (tph->tp_status & (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING))) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 113 | { |
| 114 | frame_not_ready++; |
| 115 | goto next; |
| 116 | } |
| 117 | |
| 118 | do |
| 119 | { |
| 120 | b0 = vlib_get_buffer (vm, bi); |
| 121 | len = b0->current_length; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 122 | clib_memcpy ((u8 *) tph + |
| 123 | TPACKET_ALIGN (sizeof (struct tpacket2_hdr)) + offset, |
| 124 | vlib_buffer_get_current (b0), len); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 125 | offset += len; |
| 126 | } |
Jim Gibson | 22db11b | 2017-03-27 19:46:12 +0000 | [diff] [blame] | 127 | while ((bi = |
| 128 | (b0->flags & VLIB_BUFFER_NEXT_PRESENT) ? b0->next_buffer : 0)); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 129 | |
| 130 | tph->tp_len = tph->tp_snaplen = offset; |
| 131 | tph->tp_status = TP_STATUS_SEND_REQUEST; |
| 132 | n_sent++; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 133 | next: |
Florin Coras | 837503c | 2017-11-28 11:59:20 -0800 | [diff] [blame] | 134 | tx_frame = (tx_frame + 1) % frame_num; |
| 135 | |
Chris Luke | 4b46c84 | 2016-05-23 21:30:26 -0400 | [diff] [blame] | 136 | /* check if we've exhausted the ring */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 137 | if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num)) |
| 138 | break; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 139 | } |
| 140 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 141 | CLIB_MEMORY_BARRIER (); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 142 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 143 | if (PREDICT_TRUE (n_sent)) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 144 | { |
| 145 | apif->next_tx_frame = tx_frame; |
Chris Luke | 4b46c84 | 2016-05-23 21:30:26 -0400 | [diff] [blame] | 146 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 147 | if (PREDICT_FALSE (sendto (apif->fd, NULL, 0, |
| 148 | MSG_DONTWAIT, NULL, 0) == -1)) |
| 149 | { |
| 150 | /* Uh-oh, drop & move on, but count whether it was fatal or not. |
| 151 | * Note that we have no reliable way to properly determine the |
| 152 | * disposition of the packets we just enqueued for delivery. |
| 153 | */ |
| 154 | vlib_error_count (vm, node->node_index, |
| 155 | unix_error_is_fatal (errno) ? |
| 156 | AF_PACKET_TX_ERROR_TXRING_FATAL : |
| 157 | AF_PACKET_TX_ERROR_TXRING_EAGAIN, n_sent); |
| 158 | } |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 159 | } |
| 160 | |
Damjan Marion | 1927da2 | 2017-03-27 17:08:20 +0200 | [diff] [blame] | 161 | clib_spinlock_unlock_if_init (&apif->lockp); |
Mohsin KAZMI | cf751ec | 2017-01-18 11:59:45 +0100 | [diff] [blame] | 162 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 163 | if (PREDICT_FALSE (frame_not_ready)) |
| 164 | vlib_error_count (vm, node->node_index, |
| 165 | AF_PACKET_TX_ERROR_FRAME_NOT_READY, frame_not_ready); |
Chris Luke | 4b46c84 | 2016-05-23 21:30:26 -0400 | [diff] [blame] | 166 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 167 | if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num)) |
Chris Luke | 4b46c84 | 2016-05-23 21:30:26 -0400 | [diff] [blame] | 168 | 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] | 169 | n_left); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 170 | |
| 171 | vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors); |
| 172 | return frame->n_vectors; |
| 173 | } |
| 174 | |
| 175 | static void |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 176 | af_packet_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index, |
| 177 | u32 node_index) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 178 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 179 | af_packet_main_t *apm = &af_packet_main; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 180 | 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] | 181 | af_packet_if_t *apif = |
| 182 | pool_elt_at_index (apm->interfaces, hw->dev_instance); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 183 | |
| 184 | /* Shut off redirection */ |
| 185 | if (node_index == ~0) |
| 186 | { |
| 187 | apif->per_interface_next_index = node_index; |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | apif->per_interface_next_index = |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 192 | vlib_node_add_next (vlib_get_main (), af_packet_input_node.index, |
| 193 | node_index); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 194 | } |
| 195 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 196 | static void |
| 197 | af_packet_clear_hw_interface_counters (u32 instance) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 198 | { |
| 199 | /* Nothing for now */ |
| 200 | } |
| 201 | |
| 202 | static clib_error_t * |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 203 | af_packet_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, |
| 204 | u32 flags) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 205 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 206 | af_packet_main_t *apm = &af_packet_main; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 207 | 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] | 208 | af_packet_if_t *apif = |
| 209 | pool_elt_at_index (apm->interfaces, hw->dev_instance); |
Alpesh Patel | 83cc4e1 | 2016-04-05 12:49:30 -0700 | [diff] [blame] | 210 | u32 hw_flags; |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 211 | int rv, fd = socket (AF_UNIX, SOCK_DGRAM, 0); |
| 212 | struct ifreq ifr; |
| 213 | |
Ray Kinsella | e6cc9cc | 2017-05-20 13:42:30 +0100 | [diff] [blame] | 214 | if (0 > fd) |
| 215 | { |
| 216 | clib_unix_warning ("af_packet_%s could not open socket", |
| 217 | apif->host_if_name); |
| 218 | return 0; |
| 219 | } |
| 220 | |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 221 | /* if interface is a bridge ignore */ |
| 222 | if (apif->host_if_index < 0) |
Ray Kinsella | 2038ad0 | 2017-05-18 11:56:28 +0100 | [diff] [blame] | 223 | goto error; /* no error */ |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 224 | |
| 225 | /* use host_if_index in case host name has changed */ |
| 226 | ifr.ifr_ifindex = apif->host_if_index; |
| 227 | if ((rv = ioctl (fd, SIOCGIFNAME, &ifr)) < 0) |
| 228 | { |
| 229 | clib_unix_warning ("af_packet_%s ioctl could not retrieve eth name", |
| 230 | apif->host_if_name); |
Ray Kinsella | 2038ad0 | 2017-05-18 11:56:28 +0100 | [diff] [blame] | 231 | goto error; |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 232 | } |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 233 | |
| 234 | apif->is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0; |
| 235 | |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 236 | if ((rv = ioctl (fd, SIOCGIFFLAGS, &ifr)) < 0) |
| 237 | { |
| 238 | clib_unix_warning ("af_packet_%s error: %d", |
| 239 | apif->is_admin_up ? "up" : "down", rv); |
Ray Kinsella | 2038ad0 | 2017-05-18 11:56:28 +0100 | [diff] [blame] | 240 | goto error; |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 241 | } |
| 242 | |
Alpesh Patel | 83cc4e1 | 2016-04-05 12:49:30 -0700 | [diff] [blame] | 243 | if (apif->is_admin_up) |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 244 | { |
| 245 | hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP; |
| 246 | ifr.ifr_flags |= IFF_UP; |
| 247 | } |
Alpesh Patel | 83cc4e1 | 2016-04-05 12:49:30 -0700 | [diff] [blame] | 248 | else |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 249 | { |
| 250 | hw_flags = 0; |
| 251 | ifr.ifr_flags &= ~IFF_UP; |
| 252 | } |
| 253 | |
| 254 | if ((rv = ioctl (fd, SIOCSIFFLAGS, &ifr)) < 0) |
| 255 | { |
| 256 | clib_unix_warning ("af_packet_%s error: %d", |
| 257 | apif->is_admin_up ? "up" : "down", rv); |
Ray Kinsella | 2038ad0 | 2017-05-18 11:56:28 +0100 | [diff] [blame] | 258 | goto error; |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 259 | } |
Alpesh Patel | 83cc4e1 | 2016-04-05 12:49:30 -0700 | [diff] [blame] | 260 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 261 | vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags); |
Alpesh Patel | 83cc4e1 | 2016-04-05 12:49:30 -0700 | [diff] [blame] | 262 | |
Ray Kinsella | 2038ad0 | 2017-05-18 11:56:28 +0100 | [diff] [blame] | 263 | error: |
Ray Kinsella | e6cc9cc | 2017-05-20 13:42:30 +0100 | [diff] [blame] | 264 | if (0 <= fd) |
| 265 | close (fd); |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 266 | |
| 267 | return 0; /* no error */ |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | static clib_error_t * |
| 271 | af_packet_subif_add_del_function (vnet_main_t * vnm, |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 272 | u32 hw_if_index, |
| 273 | struct vnet_sw_interface_t *st, int is_add) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 274 | { |
| 275 | /* Nothing for now */ |
| 276 | return 0; |
| 277 | } |
| 278 | |
Ray Kinsella | 2038ad0 | 2017-05-18 11:56:28 +0100 | [diff] [blame] | 279 | static clib_error_t *af_packet_set_mac_address_function |
| 280 | (struct vnet_hw_interface_t *hi, char *address) |
| 281 | { |
| 282 | af_packet_main_t *apm = &af_packet_main; |
| 283 | af_packet_if_t *apif = |
| 284 | pool_elt_at_index (apm->interfaces, hi->dev_instance); |
| 285 | int rv, fd = socket (AF_UNIX, SOCK_DGRAM, 0); |
| 286 | struct ifreq ifr; |
| 287 | |
Ray Kinsella | e6cc9cc | 2017-05-20 13:42:30 +0100 | [diff] [blame] | 288 | if (0 > fd) |
| 289 | { |
| 290 | clib_unix_warning ("af_packet_%s could not open socket", |
| 291 | apif->host_if_name); |
| 292 | return 0; |
| 293 | } |
| 294 | |
Ray Kinsella | 2038ad0 | 2017-05-18 11:56:28 +0100 | [diff] [blame] | 295 | /* if interface is a bridge ignore */ |
| 296 | if (apif->host_if_index < 0) |
| 297 | goto error; /* no error */ |
| 298 | |
| 299 | /* use host_if_index in case host name has changed */ |
| 300 | ifr.ifr_ifindex = apif->host_if_index; |
| 301 | if ((rv = ioctl (fd, SIOCGIFNAME, &ifr)) < 0) |
| 302 | { |
| 303 | clib_unix_warning |
| 304 | ("af_packet_%s ioctl could not retrieve eth name, error: %d", |
| 305 | apif->host_if_name, rv); |
| 306 | goto error; |
| 307 | } |
| 308 | |
| 309 | clib_memcpy (ifr.ifr_hwaddr.sa_data, address, 6); |
| 310 | ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER; |
| 311 | |
| 312 | if ((rv = ioctl (fd, SIOCSIFHWADDR, &ifr)) < 0) |
| 313 | { |
| 314 | clib_unix_warning ("af_packet_%s ioctl could not set mac, error: %d", |
| 315 | apif->host_if_name, rv); |
| 316 | goto error; |
| 317 | } |
| 318 | |
| 319 | error: |
Ray Kinsella | e6cc9cc | 2017-05-20 13:42:30 +0100 | [diff] [blame] | 320 | |
| 321 | if (0 <= fd) |
| 322 | close (fd); |
Ray Kinsella | 2038ad0 | 2017-05-18 11:56:28 +0100 | [diff] [blame] | 323 | |
| 324 | return 0; /* no error */ |
| 325 | } |
| 326 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 327 | /* *INDENT-OFF* */ |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 328 | VNET_DEVICE_CLASS (af_packet_device_class) = { |
| 329 | .name = "af-packet", |
| 330 | .tx_function = af_packet_interface_tx, |
| 331 | .format_device_name = format_af_packet_device_name, |
| 332 | .format_device = format_af_packet_device, |
| 333 | .format_tx_trace = format_af_packet_tx_trace, |
| 334 | .tx_function_n_errors = AF_PACKET_TX_N_ERROR, |
| 335 | .tx_function_error_strings = af_packet_tx_func_error_strings, |
| 336 | .rx_redirect_to_node = af_packet_set_interface_next_node, |
| 337 | .clear_counters = af_packet_clear_hw_interface_counters, |
| 338 | .admin_up_down_function = af_packet_interface_admin_up_down, |
| 339 | .subif_add_del_function = af_packet_subif_add_del_function, |
Ray Kinsella | 2038ad0 | 2017-05-18 11:56:28 +0100 | [diff] [blame] | 340 | .mac_addr_change_function = af_packet_set_mac_address_function, |
Alpesh Patel | 83cc4e1 | 2016-04-05 12:49:30 -0700 | [diff] [blame] | 341 | }; |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 342 | |
| 343 | VLIB_DEVICE_TX_FUNCTION_MULTIARCH (af_packet_device_class, |
| 344 | af_packet_interface_tx) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 345 | /* *INDENT-ON* */ |
| 346 | |
| 347 | /* |
| 348 | * fd.io coding-style-patch-verification: ON |
| 349 | * |
| 350 | * Local Variables: |
| 351 | * eval: (c-set-style "gnu") |
| 352 | * End: |
| 353 | */ |