X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fvnet%2Funix%2Ftuntap.c;h=72c4f73e6df843c4b94026a8d67263df80020b08;hb=c4ae0fffb;hp=6e2a53fefd2a582f25aab2e01256d23721f524a8;hpb=4cd257667406d0500a81323ef91f5c7c8c902b25;p=vpp.git diff --git a/src/vnet/unix/tuntap.c b/src/vnet/unix/tuntap.c index 6e2a53fefd2..72c4f73e6df 100644 --- a/src/vnet/unix/tuntap.c +++ b/src/vnet/unix/tuntap.c @@ -1,4 +1,4 @@ -/* +/* *------------------------------------------------------------------ * tuntap.c - kernel stack (reverse) punt/inject path * @@ -55,13 +55,14 @@ static vnet_device_class_t tuntap_dev_class; static vnet_hw_interface_class_t tuntap_interface_class; static void tuntap_punt_frame (vlib_main_t * vm, - vlib_node_runtime_t * node, - vlib_frame_t * frame); + vlib_node_runtime_t * node, + vlib_frame_t * frame); static void tuntap_nopunt_frame (vlib_main_t * vm, - vlib_node_runtime_t * node, - vlib_frame_t * frame); + vlib_node_runtime_t * node, + vlib_frame_t * frame); -typedef struct { +typedef struct +{ u32 sw_if_index; u8 is_v6; u8 addr[16]; @@ -74,18 +75,19 @@ typedef struct { /** Vector of VLIB rx buffers to use. We allocate them in blocks of VLIB_FRAME_SIZE (256). */ - u32 * rx_buffers; + u32 *rx_buffers; /** Vector of iovecs for readv/writev calls. */ - struct iovec * iovecs; + struct iovec *iovecs; } tuntap_per_thread_t; /** * @brief TUNTAP node main state */ -typedef struct { +typedef struct +{ /** per thread variables */ - tuntap_per_thread_t * threads; + tuntap_per_thread_t *threads; /** File descriptors for /dev/net/tun and provisioning socket. */ int dev_net_tun_fd, dev_tap_fd; @@ -104,7 +106,7 @@ typedef struct { u32 mtu_bytes, mtu_buffers; /** Linux interface name for tun device. */ - char * tun_name; + char *tun_name; /** Pool of subinterface addresses */ subif_address_t *subifs; @@ -142,32 +144,31 @@ static tuntap_main_t tuntap_main = { * */ static uword -tuntap_tx (vlib_main_t * vm, - vlib_node_runtime_t * node, - vlib_frame_t * frame) +tuntap_tx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) { - u32 * buffers = vlib_frame_args (frame); + u32 *buffers = vlib_frame_vector_args (frame); uword n_packets = frame->n_vectors; - tuntap_main_t * tm = &tuntap_main; + tuntap_main_t *tm = &tuntap_main; vnet_main_t *vnm = vnet_get_main (); vnet_interface_main_t *im = &vnm->interface_main; u32 n_bytes = 0; int i; - u16 thread_index = vlib_get_thread_index (); + u16 thread_index = vm->thread_index; for (i = 0; i < n_packets; i++) { - struct iovec * iov; - vlib_buffer_t * b; + struct iovec *iov; + vlib_buffer_t *b; uword l; b = vlib_get_buffer (vm, buffers[i]); if (tm->is_ether && (!tm->have_normal_interface)) - { - vlib_buffer_reset(b); - clib_memcpy (vlib_buffer_get_current (b), tm->ether_dst_mac, 6); - } + { + vlib_buffer_reset (b); + clib_memcpy_fast (vlib_buffer_get_current (b), tm->ether_dst_mac, + 6); + } /* Re-set iovecs if present. */ if (tm->threads[thread_index].iovecs) @@ -180,15 +181,17 @@ tuntap_tx (vlib_main_t * vm, if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT)) { - do { - b = vlib_get_buffer (vm, b->next_buffer); + do + { + b = vlib_get_buffer (vm, b->next_buffer); - vec_add2 (tm->threads[thread_index].iovecs, iov, 1); + vec_add2 (tm->threads[thread_index].iovecs, iov, 1); - iov->iov_base = b->data + b->current_data; - iov->iov_len = b->current_length; - l += b->current_length; - } while (b->flags & VLIB_BUFFER_NEXT_PRESENT); + iov->iov_base = b->data + b->current_data; + iov->iov_len = b->current_length; + l += b->current_length; + } + while (b->flags & VLIB_BUFFER_NEXT_PRESENT); } if (writev (tm->dev_net_tun_fd, tm->threads[thread_index].iovecs, @@ -214,12 +217,14 @@ tuntap_tx (vlib_main_t * vm, return n_packets; } +/* *INDENT-OFF* */ VLIB_REGISTER_NODE (tuntap_tx_node,static) = { .function = tuntap_tx, .name = "tuntap-tx", .type = VLIB_NODE_TYPE_INTERNAL, .vector_size = 4, }; +/* *INDENT-ON* */ /** * @brief TUNTAP receive node @@ -233,15 +238,13 @@ VLIB_REGISTER_NODE (tuntap_tx_node,static) = { * */ static uword -tuntap_rx (vlib_main_t * vm, - vlib_node_runtime_t * node, - vlib_frame_t * frame) +tuntap_rx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) { - tuntap_main_t * tm = &tuntap_main; - vlib_buffer_t * b; + tuntap_main_t *tm = &tuntap_main; + vlib_buffer_t *b; u32 bi; - const uword buffer_size = VLIB_BUFFER_DATA_SIZE; - u16 thread_index = vlib_get_thread_index (); + const uword buffer_size = vlib_buffer_get_default_data_size (vm); + u16 thread_index = vm->thread_index; /** Make sure we have some RX buffers. */ { @@ -250,10 +253,13 @@ tuntap_rx (vlib_main_t * vm, if (n_left < VLIB_FRAME_SIZE / 2) { - if (! tm->threads[thread_index].rx_buffers) + if (!tm->threads[thread_index].rx_buffers) vec_alloc (tm->threads[thread_index].rx_buffers, VLIB_FRAME_SIZE); - n_alloc = vlib_buffer_alloc (vm, tm->threads[thread_index].rx_buffers + n_left, VLIB_FRAME_SIZE - n_left); + n_alloc = + vlib_buffer_alloc (vm, + tm->threads[thread_index].rx_buffers + n_left, + VLIB_FRAME_SIZE - n_left); _vec_len (tm->threads[thread_index].rx_buffers) = n_left + n_alloc; } } @@ -262,27 +268,31 @@ tuntap_rx (vlib_main_t * vm, Turn them into iovecs to pass to readv. */ { uword i_rx = vec_len (tm->threads[thread_index].rx_buffers) - 1; - vlib_buffer_t * b; + vlib_buffer_t *b; word i, n_bytes_left, n_bytes_in_packet; /** We should have enough buffers left for an MTU sized packet. */ - ASSERT (vec_len (tm->threads[thread_index].rx_buffers) >= tm->mtu_buffers); + ASSERT (vec_len (tm->threads[thread_index].rx_buffers) >= + tm->mtu_buffers); vec_validate (tm->threads[thread_index].iovecs, tm->mtu_buffers - 1); for (i = 0; i < tm->mtu_buffers; i++) { - b = vlib_get_buffer (vm, tm->threads[thread_index].rx_buffers[i_rx - i]); + b = + vlib_get_buffer (vm, + tm->threads[thread_index].rx_buffers[i_rx - i]); tm->threads[thread_index].iovecs[i].iov_base = b->data; tm->threads[thread_index].iovecs[i].iov_len = buffer_size; } - n_bytes_left = readv (tm->dev_net_tun_fd, tm->threads[thread_index].iovecs, - tm->mtu_buffers); + n_bytes_left = + readv (tm->dev_net_tun_fd, tm->threads[thread_index].iovecs, + tm->mtu_buffers); n_bytes_in_packet = n_bytes_left; if (n_bytes_left <= 0) { - if (errno != EAGAIN) - clib_unix_warning ("readv %d", n_bytes_left); + if (errno != EAGAIN) + clib_unix_warning ("readv %d", n_bytes_left); return 0; } @@ -293,14 +303,15 @@ tuntap_rx (vlib_main_t * vm, b = vlib_get_buffer (vm, tm->threads[thread_index].rx_buffers[i_rx]); b->flags = 0; b->current_data = 0; - b->current_length = n_bytes_left < buffer_size ? n_bytes_left : buffer_size; + b->current_length = + n_bytes_left < buffer_size ? n_bytes_left : buffer_size; n_bytes_left -= buffer_size; if (n_bytes_left <= 0) - { - break; - } + { + break; + } i_rx--; b->flags |= VLIB_BUFFER_NEXT_PRESENT; @@ -309,11 +320,9 @@ tuntap_rx (vlib_main_t * vm, /** Interface counters for tuntap interface. */ vlib_increment_combined_counter - (vnet_main.interface_main.combined_sw_if_counters - + VNET_INTERFACE_COUNTER_RX, - thread_index, - tm->sw_if_index, - 1, n_bytes_in_packet); + (vnet_main.interface_main.combined_sw_if_counters + + VNET_INTERFACE_COUNTER_RX, + thread_index, tm->sw_if_index, 1, n_bytes_in_packet); _vec_len (tm->threads[thread_index].rx_buffers) = i_rx; } @@ -325,7 +334,7 @@ tuntap_rx (vlib_main_t * vm, uword n_trace = vlib_get_trace_count (vm, node); vnet_buffer (b)->sw_if_index[VLIB_RX] = tm->sw_if_index; - vnet_buffer (b)->sw_if_index[VLIB_TX] = (u32)~0; + vnet_buffer (b)->sw_if_index[VLIB_TX] = (u32) ~ 0; /* * Turn this on if you run into @@ -333,7 +342,7 @@ tuntap_rx (vlib_main_t * vm, * which nodes they've visited... */ if (VLIB_BUFFER_TRACE_TRAJECTORY) - b->pre_data[0] = 0; + b->pre_data[0] = 0; b->error = node->errors[0]; @@ -343,26 +352,26 @@ tuntap_rx (vlib_main_t * vm, } else switch (b->data[0] & 0xf0) - { - case 0x40: - next_index = VNET_DEVICE_INPUT_NEXT_IP4_INPUT; - break; - case 0x60: - next_index = VNET_DEVICE_INPUT_NEXT_IP6_INPUT; - break; - default: - next_index = VNET_DEVICE_INPUT_NEXT_DROP; - break; - } + { + case 0x40: + next_index = VNET_DEVICE_INPUT_NEXT_IP4_INPUT; + break; + case 0x60: + next_index = VNET_DEVICE_INPUT_NEXT_IP6_INPUT; + break; + default: + next_index = VNET_DEVICE_INPUT_NEXT_DROP; + break; + } /* The linux kernel couldn't care less if our interface is up */ if (tm->have_normal_interface) { - vnet_main_t *vnm = vnet_get_main(); - vnet_sw_interface_t * si; - si = vnet_get_sw_interface (vnm, tm->sw_if_index); - if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)) - next_index = VNET_DEVICE_INPUT_NEXT_DROP; + vnet_main_t *vnm = vnet_get_main (); + vnet_sw_interface_t *si; + si = vnet_get_sw_interface (vnm, tm->sw_if_index); + if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)) + next_index = VNET_DEVICE_INPUT_NEXT_DROP; } vnet_feature_start_device_input_x1 (tm->sw_if_index, &next_index, b); @@ -371,9 +380,8 @@ tuntap_rx (vlib_main_t * vm, if (n_trace > 0) { - vlib_trace_buffer (vm, node, next_index, - b, /* follow_chain */ 1); - vlib_set_trace_count (vm, node, n_trace - 1); + vlib_trace_buffer (vm, node, next_index, b, /* follow_chain */ 1); + vlib_set_trace_count (vm, node, n_trace - 1); } } @@ -383,12 +391,14 @@ tuntap_rx (vlib_main_t * vm, /** * @brief TUNTAP_RX error strings */ -static char * tuntap_rx_error_strings[] = { +static char *tuntap_rx_error_strings[] = { "unknown packet type", }; +/* *INDENT-OFF* */ VLIB_REGISTER_NODE (tuntap_rx_node,static) = { .function = tuntap_rx, + .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED, .name = "tuntap-rx", .sibling_of = "device-input", .type = VLIB_NODE_TYPE_INPUT, @@ -397,6 +407,7 @@ VLIB_REGISTER_NODE (tuntap_rx_node,static) = { .n_errors = 1, .error_strings = tuntap_rx_error_strings, }; +/* *INDENT-ON* */ /** * @brief Gets called when file descriptor is ready from epoll. @@ -405,9 +416,10 @@ VLIB_REGISTER_NODE (tuntap_rx_node,static) = { * * @return error - clib_error_t */ -static clib_error_t * tuntap_read_ready (clib_file_t * uf) +static clib_error_t * +tuntap_read_ready (clib_file_t * uf) { - vlib_main_t * vm = vlib_get_main(); + vlib_main_t *vm = vlib_get_main (); vlib_node_set_interrupt_pending (vm, tuntap_rx_node.index); return 0; } @@ -428,15 +440,15 @@ tuntap_exit (vlib_main_t * vm) int sfd; /* Not present. */ - if (! tm->dev_net_tun_fd || tm->dev_net_tun_fd < 0) + if (!tm->dev_net_tun_fd || tm->dev_net_tun_fd < 0) return 0; sfd = socket (AF_INET, SOCK_STREAM, 0); if (sfd < 0) - clib_unix_warning("provisioning socket"); + clib_unix_warning ("provisioning socket"); - memset(&ifr, 0, sizeof (ifr)); - strncpy (ifr.ifr_name, tm->tun_name, sizeof (ifr.ifr_name)-1); + clib_memset (&ifr, 0, sizeof (ifr)); + strncpy (ifr.ifr_name, tm->tun_name, sizeof (ifr.ifr_name) - 1); /* get flags, modify to bring down interface... */ if (ioctl (sfd, SIOCGIFFLAGS, &ifr) < 0) @@ -450,9 +462,9 @@ tuntap_exit (vlib_main_t * vm) /* Turn off persistence */ if (ioctl (tm->dev_net_tun_fd, TUNSETPERSIST, 0) < 0) clib_unix_warning ("TUNSETPERSIST"); - close(tm->dev_tap_fd); + close (tm->dev_tap_fd); if (tm->dev_net_tun_fd >= 0) - close(tm->dev_net_tun_fd); + close (tm->dev_net_tun_fd); if (sfd >= 0) close (sfd); @@ -474,27 +486,26 @@ static clib_error_t * tuntap_config (vlib_main_t * vm, unformat_input_t * input) { tuntap_main_t *tm = &tuntap_main; - clib_error_t * error = 0; + clib_error_t *error = 0; struct ifreq ifr; - u8 * name; + u8 *name; int flags = IFF_TUN | IFF_NO_PI; int is_enabled = 0, is_ether = 0, have_normal_interface = 0; - const uword buffer_size = VLIB_BUFFER_DATA_SIZE; + const uword buffer_size = vlib_buffer_get_default_data_size (vm); while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "mtu %d", &tm->mtu_bytes)) ; else if (unformat (input, "enable")) - is_enabled = 1; + is_enabled = 1; else if (unformat (input, "disable")) - is_enabled = 0; - else if (unformat (input, "ethernet") || - unformat (input, "ether")) - is_ether = 1; + is_enabled = 0; + else if (unformat (input, "ethernet") || unformat (input, "ether")) + is_ether = 1; else if (unformat (input, "have-normal-interface") || - unformat (input, "have-normal")) - have_normal_interface = 1; + unformat (input, "have-normal")) + have_normal_interface = 1; else if (unformat (input, "name %s", &name)) tm->tun_name = (char *) name; else @@ -508,11 +519,11 @@ tuntap_config (vlib_main_t * vm, unformat_input_t * input) if (is_enabled == 0) return 0; - if (geteuid()) + if (geteuid ()) { clib_warning ("tuntap disabled: must be superuser"); return 0; - } + } tm->is_ether = is_ether; tm->have_normal_interface = have_normal_interface; @@ -526,15 +537,15 @@ tuntap_config (vlib_main_t * vm, unformat_input_t * input) goto done; } - memset (&ifr, 0, sizeof (ifr)); - strncpy(ifr.ifr_name, tm->tun_name, sizeof(ifr.ifr_name)-1); + clib_memset (&ifr, 0, sizeof (ifr)); + strncpy (ifr.ifr_name, tm->tun_name, sizeof (ifr.ifr_name) - 1); ifr.ifr_flags = flags; - if (ioctl (tm->dev_net_tun_fd, TUNSETIFF, (void *)&ifr) < 0) + if (ioctl (tm->dev_net_tun_fd, TUNSETIFF, (void *) &ifr) < 0) { error = clib_error_return_unix (0, "ioctl TUNSETIFF"); goto done; } - + /* Make it persistent, at least until we split. */ if (ioctl (tm->dev_net_tun_fd, TUNSETPERSIST, 1) < 0) { @@ -543,8 +554,7 @@ tuntap_config (vlib_main_t * vm, unformat_input_t * input) } /* Open a provisioning socket */ - if ((tm->dev_tap_fd = socket(PF_PACKET, SOCK_RAW, - htons(ETH_P_ALL))) < 0 ) + if ((tm->dev_tap_fd = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0) { error = clib_error_return_unix (0, "socket"); goto done; @@ -555,21 +565,21 @@ tuntap_config (vlib_main_t * vm, unformat_input_t * input) struct ifreq ifr; struct sockaddr_ll sll; - memset (&ifr, 0, sizeof(ifr)); - strncpy (ifr.ifr_name, tm->tun_name, sizeof(ifr.ifr_name)-1); - if (ioctl (tm->dev_tap_fd, SIOCGIFINDEX, &ifr) < 0 ) + clib_memset (&ifr, 0, sizeof (ifr)); + strncpy (ifr.ifr_name, tm->tun_name, sizeof (ifr.ifr_name) - 1); + if (ioctl (tm->dev_tap_fd, SIOCGIFINDEX, &ifr) < 0) { error = clib_error_return_unix (0, "ioctl SIOCGIFINDEX"); goto done; } /* Bind the provisioning socket to the interface. */ - memset(&sll, 0, sizeof(sll)); - sll.sll_family = AF_PACKET; - sll.sll_ifindex = ifr.ifr_ifindex; - sll.sll_protocol = htons(ETH_P_ALL); + clib_memset (&sll, 0, sizeof (sll)); + sll.sll_family = AF_PACKET; + sll.sll_ifindex = ifr.ifr_ifindex; + sll.sll_protocol = htons (ETH_P_ALL); - if (bind(tm->dev_tap_fd, (struct sockaddr*) &sll, sizeof(sll)) < 0) + if (bind (tm->dev_tap_fd, (struct sockaddr *) &sll, sizeof (sll)) < 0) { error = clib_error_return_unix (0, "bind"); goto done; @@ -613,58 +623,54 @@ tuntap_config (vlib_main_t * vm, unformat_input_t * input) if (is_ether) { if (ioctl (tm->dev_tap_fd, SIOCGIFHWADDR, &ifr) < 0) - { - error = clib_error_return_unix (0, "ioctl SIOCGIFHWADDR"); - goto done; - } + { + error = clib_error_return_unix (0, "ioctl SIOCGIFHWADDR"); + goto done; + } else - clib_memcpy (tm->ether_dst_mac, ifr.ifr_hwaddr.sa_data, 6); + clib_memcpy_fast (tm->ether_dst_mac, ifr.ifr_hwaddr.sa_data, 6); } if (have_normal_interface) { - vnet_main_t *vnm = vnet_get_main(); + vnet_main_t *vnm = vnet_get_main (); error = ethernet_register_interface - (vnm, - tuntap_dev_class.index, - 0 /* device instance */, - tm->ether_dst_mac /* ethernet address */, - &tm->hw_if_index, - 0 /* flag change */); + (vnm, tuntap_dev_class.index, 0 /* device instance */ , + tm->ether_dst_mac /* ethernet address */ , + &tm->hw_if_index, 0 /* flag change */ ); if (error) - clib_error_report (error); + clib_error_report (error); tm->sw_if_index = tm->hw_if_index; vm->os_punt_frame = tuntap_nopunt_frame; } else { - vnet_main_t *vnm = vnet_get_main(); - vnet_hw_interface_t * hi; - + vnet_main_t *vnm = vnet_get_main (); + vnet_hw_interface_t *hi; + vm->os_punt_frame = tuntap_punt_frame; - + tm->hw_if_index = vnet_register_interface - (vnm, - tuntap_dev_class.index, 0 /* device instance */, - tuntap_interface_class.index, 0); + (vnm, tuntap_dev_class.index, 0 /* device instance */ , + tuntap_interface_class.index, 0); hi = vnet_get_hw_interface (vnm, tm->hw_if_index); tm->sw_if_index = hi->sw_if_index; - + /* Interface is always up. */ - vnet_hw_interface_set_flags (vnm, tm->hw_if_index, - VNET_HW_INTERFACE_FLAG_LINK_UP); - vnet_sw_interface_set_flags (vnm, tm->sw_if_index, - VNET_SW_INTERFACE_FLAG_ADMIN_UP); + vnet_hw_interface_set_flags (vnm, tm->hw_if_index, + VNET_HW_INTERFACE_FLAG_LINK_UP); + vnet_sw_interface_set_flags (vnm, tm->sw_if_index, + VNET_SW_INTERFACE_FLAG_ADMIN_UP); } { - clib_file_t template = {0}; + clib_file_t template = { 0 }; template.read_function = tuntap_read_ready; template.file_descriptor = tm->dev_net_tun_fd; tm->clib_file_index = clib_file_add (&file_main, &template); } - done: +done: if (error) { if (tm->dev_net_tun_fd >= 0) @@ -694,32 +700,30 @@ tuntap_ip4_add_del_interface_address (ip4_main_t * im, u32 sw_if_index, ip4_address_t * address, u32 address_length, - u32 if_address_index, - u32 is_delete) + u32 if_address_index, u32 is_delete) { - tuntap_main_t * tm = &tuntap_main; + tuntap_main_t *tm = &tuntap_main; struct ifreq ifr; - subif_address_t subif_addr, * ap; - uword * p; + subif_address_t subif_addr, *ap; + uword *p; /** Tuntap disabled, or using a "normal" interface. */ - if (tm->have_normal_interface || tm->dev_tap_fd < 0) + if (tm->have_normal_interface || tm->dev_tap_fd < 0) return; /* if the address is being applied to an interface that is not in * the same table/VRF as this tap, then ignore it. - * If we don't do this overlapping address spaces in the diferent tables + * If we don't do this overlapping address spaces in the different tables * breaks the linux host's routing tables */ - if (fib_table_get_index_for_sw_if_index(FIB_PROTOCOL_IP4, - sw_if_index) != - fib_table_get_index_for_sw_if_index(FIB_PROTOCOL_IP4, - tm->sw_if_index)) - return; + if (fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, + sw_if_index) != + fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, tm->sw_if_index)) + return; /** See if we already know about this subif */ - memset (&subif_addr, 0, sizeof (subif_addr)); + clib_memset (&subif_addr, 0, sizeof (subif_addr)); subif_addr.sw_if_index = sw_if_index; - clib_memcpy (&subif_addr.addr, address, sizeof (*address)); + clib_memcpy_fast (&subif_addr.addr, address, sizeof (*address)); p = mhash_get (&tm->subif_mhash, &subif_addr); @@ -733,9 +737,9 @@ tuntap_ip4_add_del_interface_address (ip4_main_t * im, } /* Use subif pool index to select alias device. */ - memset (&ifr, 0, sizeof (ifr)); - snprintf (ifr.ifr_name, sizeof(ifr.ifr_name), - "%s:%d", tm->tun_name, (int)(ap - tm->subifs)); + clib_memset (&ifr, 0, sizeof (ifr)); + snprintf (ifr.ifr_name, sizeof (ifr.ifr_name), + "%s:%d", tm->tun_name, (int) (ap - tm->subifs)); /* the tuntap punt/inject is enabled for IPv4 RX so long as * any vpp interface has an IPv4 address. @@ -743,25 +747,25 @@ tuntap_ip4_add_del_interface_address (ip4_main_t * im, */ ip4_sw_interface_enable_disable (tm->sw_if_index, !is_delete); - if (! is_delete) + if (!is_delete) { - struct sockaddr_in * sin; + struct sockaddr_in *sin; - sin = (struct sockaddr_in *)&ifr.ifr_addr; + sin = (struct sockaddr_in *) &ifr.ifr_addr; /* Set ipv4 address, netmask. */ sin->sin_family = AF_INET; - clib_memcpy (&sin->sin_addr.s_addr, address, 4); + clib_memcpy_fast (&sin->sin_addr.s_addr, address, 4); if (ioctl (tm->dev_tap_fd, SIOCSIFADDR, &ifr) < 0) clib_unix_warning ("ioctl SIOCSIFADDR"); - + sin->sin_addr.s_addr = im->fib_masks[address_length]; if (ioctl (tm->dev_tap_fd, SIOCSIFNETMASK, &ifr) < 0) clib_unix_warning ("ioctl SIOCSIFNETMASK"); } else { - mhash_unset (&tm->subif_mhash, &subif_addr, 0 /* old value ptr */); + mhash_unset (&tm->subif_mhash, &subif_addr, 0 /* old value ptr */ ); pool_put (tm->subifs, ap); } @@ -783,17 +787,18 @@ tuntap_ip4_add_del_interface_address (ip4_main_t * im, * including @c causes multiple definitions if * @c have_normal_interface || tm->dev_tap_fd < 0) + if (tm->have_normal_interface || tm->dev_tap_fd < 0) return; /* if the address is being applied to an interface that is not in * the same table/VRF as this tap, then ignore it. - * If we don't do this overlapping address spaces in the diferent tables + * If we don't do this overlapping address spaces in the different tables * breaks the linux host's routing tables */ - if (fib_table_get_index_for_sw_if_index(FIB_PROTOCOL_IP6, - sw_if_index) != - fib_table_get_index_for_sw_if_index(FIB_PROTOCOL_IP6, - tm->sw_if_index)) - return; + if (fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, + sw_if_index) != + fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, tm->sw_if_index)) + return; /* See if we already know about this subif */ - memset (&subif_addr, 0, sizeof (subif_addr)); + clib_memset (&subif_addr, 0, sizeof (subif_addr)); subif_addr.sw_if_index = sw_if_index; subif_addr.is_v6 = 1; - clib_memcpy (&subif_addr.addr, address, sizeof (*address)); + clib_memcpy_fast (&subif_addr.addr, address, sizeof (*address)); p = mhash_get (&tm->subif_mhash, &subif_addr); @@ -851,10 +854,10 @@ tuntap_ip6_add_del_interface_address (ip6_main_t * im, } /* Use subif pool index to select alias device. */ - memset (&ifr, 0, sizeof (ifr)); - memset (&ifr6, 0, sizeof (ifr6)); - snprintf (ifr.ifr_name, sizeof(ifr.ifr_name), - "%s:%d", tm->tun_name, (int)(ap - tm->subifs)); + clib_memset (&ifr, 0, sizeof (ifr)); + clib_memset (&ifr6, 0, sizeof (ifr6)); + snprintf (ifr.ifr_name, sizeof (ifr.ifr_name), + "%s:%d", tm->tun_name, (int) (ap - tm->subifs)); /* the tuntap punt/inject is enabled for IPv6 RX so long as * any vpp interface has an IPv6 address. @@ -862,45 +865,45 @@ tuntap_ip6_add_del_interface_address (ip6_main_t * im, */ ip6_sw_interface_enable_disable (tm->sw_if_index, !is_delete); - if (! is_delete) + if (!is_delete) { int sockfd = socket (AF_INET6, SOCK_STREAM, 0); if (sockfd < 0) - clib_unix_warning ("get ifindex socket"); + clib_unix_warning ("get ifindex socket"); if (ioctl (sockfd, SIOGIFINDEX, &ifr) < 0) - clib_unix_warning ("get ifindex"); + clib_unix_warning ("get ifindex"); ifr6.ifr6_ifindex = ifr.ifr_ifindex; ifr6.ifr6_prefixlen = address_length; - clib_memcpy (&ifr6.ifr6_addr, address, 16); + clib_memcpy_fast (&ifr6.ifr6_addr, address, 16); if (ioctl (sockfd, SIOCSIFADDR, &ifr6) < 0) - clib_unix_warning ("set address"); + clib_unix_warning ("set address"); if (sockfd >= 0) - close (sockfd); + close (sockfd); } else { int sockfd = socket (AF_INET6, SOCK_STREAM, 0); if (sockfd < 0) - clib_unix_warning ("get ifindex socket"); + clib_unix_warning ("get ifindex socket"); if (ioctl (sockfd, SIOGIFINDEX, &ifr) < 0) - clib_unix_warning ("get ifindex"); + clib_unix_warning ("get ifindex"); ifr6.ifr6_ifindex = ifr.ifr_ifindex; ifr6.ifr6_prefixlen = address_length; - clib_memcpy (&ifr6.ifr6_addr, address, 16); + clib_memcpy_fast (&ifr6.ifr6_addr, address, 16); if (ioctl (sockfd, SIOCDIFADDR, &ifr6) < 0) - clib_unix_warning ("del address"); + clib_unix_warning ("del address"); if (sockfd >= 0) - close (sockfd); + close (sockfd); - mhash_unset (&tm->subif_mhash, &subif_addr, 0 /* old value ptr */); + mhash_unset (&tm->subif_mhash, &subif_addr, 0 /* old value ptr */ ); pool_put (tm->subifs, ap); } } @@ -915,8 +918,7 @@ tuntap_ip6_add_del_interface_address (ip6_main_t * im, */ static void tuntap_punt_frame (vlib_main_t * vm, - vlib_node_runtime_t * node, - vlib_frame_t * frame) + vlib_node_runtime_t * node, vlib_frame_t * frame) { tuntap_tx (vm, node, frame); vlib_frame_free (vm, node, frame); @@ -932,19 +934,20 @@ tuntap_punt_frame (vlib_main_t * vm, */ static void tuntap_nopunt_frame (vlib_main_t * vm, - vlib_node_runtime_t * node, - vlib_frame_t * frame) + vlib_node_runtime_t * node, vlib_frame_t * frame) { - u32 * buffers = vlib_frame_args (frame); + u32 *buffers = vlib_frame_vector_args (frame); uword n_packets = frame->n_vectors; vlib_buffer_free (vm, buffers, n_packets); vlib_frame_free (vm, node, frame); } +/* *INDENT-OFF* */ VNET_HW_INTERFACE_CLASS (tuntap_interface_class,static) = { .name = "tuntap", .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P, }; +/* *INDENT-ON* */ /** * @brief Format tun/tap interface name @@ -955,7 +958,8 @@ VNET_HW_INTERFACE_CLASS (tuntap_interface_class,static) = { * @return *s - u8 - formatted string * */ -static u8 * format_tuntap_interface_name (u8 * s, va_list * args) +static u8 * +format_tuntap_interface_name (u8 * s, va_list * args) { u32 i = va_arg (*args, u32); @@ -975,11 +979,10 @@ static u8 * format_tuntap_interface_name (u8 * s, va_list * args) */ static uword tuntap_intfc_tx (vlib_main_t * vm, - vlib_node_runtime_t * node, - vlib_frame_t * frame) + vlib_node_runtime_t * node, vlib_frame_t * frame) { - tuntap_main_t * tm = &tuntap_main; - u32 * buffers = vlib_frame_args (frame); + tuntap_main_t *tm = &tuntap_main; + u32 *buffers = vlib_frame_vector_args (frame); uword n_buffers = frame->n_vectors; /* Normal interface transmit happens only on the normal interface... */ @@ -990,11 +993,13 @@ tuntap_intfc_tx (vlib_main_t * vm, return n_buffers; } +/* *INDENT-OFF* */ VNET_DEVICE_CLASS (tuntap_dev_class,static) = { .name = "tuntap", .tx_function = tuntap_intfc_tx, .format_device_name = format_tuntap_interface_name, }; +/* *INDENT-ON* */ /** * @brief tun/tap node init @@ -1007,19 +1012,14 @@ VNET_DEVICE_CLASS (tuntap_dev_class,static) = { static clib_error_t * tuntap_init (vlib_main_t * vm) { - clib_error_t * error; - ip4_main_t * im4 = &ip4_main; - ip6_main_t * im6 = &ip6_main; + ip4_main_t *im4 = &ip4_main; + ip6_main_t *im6 = &ip6_main; ip4_add_del_interface_address_callback_t cb4; ip6_add_del_interface_address_callback_t cb6; - tuntap_main_t * tm = &tuntap_main; - vlib_thread_main_t * m = vlib_get_thread_main (); - - error = vlib_call_init_function (vm, ip4_init); - if (error) - return error; + tuntap_main_t *tm = &tuntap_main; + vlib_thread_main_t *m = vlib_get_thread_main (); - mhash_init (&tm->subif_mhash, sizeof (u32), sizeof(subif_address_t)); + mhash_init (&tm->subif_mhash, sizeof (u32), sizeof (subif_address_t)); cb4.function = tuntap_ip4_add_del_interface_address; cb4.function_opaque = 0; @@ -1034,4 +1034,17 @@ tuntap_init (vlib_main_t * vm) return 0; } -VLIB_INIT_FUNCTION (tuntap_init); +/* *INDENT-OFF* */ +VLIB_INIT_FUNCTION (tuntap_init) = +{ + .runs_after = VLIB_INITS("ip4_init"), +}; +/* *INDENT-ON* */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */