2 * Copyright (c) 2020 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
19 #include <sys/socket.h>
22 #include <linux-cp/lcp_interface.h>
23 #include <netlink/route/link/vlan.h>
24 #include <linux/if_ether.h>
26 #include <vnet/plugin/plugin.h>
27 #include <vnet/plugin/plugin.h>
29 #include <vppinfra/linux/netns.h>
31 #include <vnet/ip/ip_punt_drop.h>
32 #include <vnet/fib/fib_table.h>
33 #include <vnet/adj/adj_mcast.h>
34 #include <vnet/udp/udp.h>
35 #include <vnet/tcp/tcp.h>
36 #include <vnet/devices/tap/tap.h>
37 #include <vnet/devices/virtio/virtio.h>
38 #include <vnet/devices/netlink.h>
39 #include <vlibapi/api_helper_macros.h>
40 #include <vnet/ipsec/ipsec_punt.h>
42 vlib_log_class_t lcp_itf_pair_logger;
47 lcp_itf_pair_t *lcp_itf_pair_pool = NULL;
50 lcp_itf_num_pairs (void)
52 return pool_elts (lcp_itf_pair_pool);
56 * DBs of interface-pair objects:
57 * - key'd by VIF (linux ID)
58 * - key'd by VPP's physical interface
59 * - number of shared uses of VPP's tap/host interface
61 static uword *lip_db_by_vif;
62 index_t *lip_db_by_phy;
66 * vector of virtual function table
68 static lcp_itf_pair_vft_t *lcp_itf_vfts = NULL;
71 lcp_itf_pair_register_vft (lcp_itf_pair_vft_t *lcp_itf_vft)
73 vec_add1 (lcp_itf_vfts, *lcp_itf_vft);
77 format_lcp_itf_pair (u8 *s, va_list *args)
79 vnet_main_t *vnm = vnet_get_main ();
80 lcp_itf_pair_t *lip = va_arg (*args, lcp_itf_pair_t *);
81 vnet_sw_interface_t *swif_phy;
82 vnet_sw_interface_t *swif_host;
84 s = format (s, "itf-pair: [%d]", lip - lcp_itf_pair_pool);
86 swif_phy = vnet_get_sw_interface_or_null (vnm, lip->lip_phy_sw_if_index);
88 s = format (s, " <no-phy-if>");
90 s = format (s, " %U", format_vnet_sw_interface_name, vnm, swif_phy);
92 swif_host = vnet_get_sw_interface_or_null (vnm, lip->lip_host_sw_if_index);
94 s = format (s, " <no-host-if>");
96 s = format (s, " %U", format_vnet_sw_interface_name, vnm, swif_host);
98 s = format (s, " %v %d type %s", lip->lip_host_name, lip->lip_vif_index,
99 (lip->lip_host_type == LCP_ITF_HOST_TAP) ? "tap" : "tun");
101 if (lip->lip_namespace)
102 s = format (s, " netns %s", lip->lip_namespace);
108 lcp_itf_pair_walk_show_cb (index_t api, void *ctx)
113 lip = lcp_itf_pair_get (api);
117 vm = vlib_get_main ();
118 vlib_cli_output (vm, "%U\n", format_lcp_itf_pair, lip);
120 return WALK_CONTINUE;
124 lcp_itf_pair_show (u32 phy_sw_if_index)
130 vm = vlib_get_main ();
131 ns = lcp_get_default_ns ();
132 vlib_cli_output (vm, "lcp default netns '%s'\n",
133 ns ? (char *) ns : "<unset>");
134 vlib_cli_output (vm, "lcp lcp-auto-subint %s\n",
135 lcp_auto_subint () ? "on" : "off");
136 vlib_cli_output (vm, "lcp lcp-sync %s\n", lcp_sync () ? "on" : "off");
138 if (phy_sw_if_index == ~0)
140 lcp_itf_pair_walk (lcp_itf_pair_walk_show_cb, 0);
144 api = lcp_itf_pair_find_by_phy (phy_sw_if_index);
145 if (api != INDEX_INVALID)
146 lcp_itf_pair_walk_show_cb (api, 0);
151 lcp_itf_pair_get (u32 index)
153 if (!lcp_itf_pair_pool)
155 if (index == INDEX_INVALID)
158 return pool_elt_at_index (lcp_itf_pair_pool, index);
162 lcp_itf_pair_find_by_vif (u32 vif_index)
166 p = hash_get (lip_db_by_vif, vif_index);
171 return INDEX_INVALID;
174 const char *lcp_itf_l3_feat_names[N_LCP_ITF_HOST][N_AF] = {
175 [LCP_ITF_HOST_TAP] = {
176 [AF_IP4] = "linux-cp-xc-ip4",
177 [AF_IP6] = "linux-cp-xc-ip6",
179 [LCP_ITF_HOST_TUN] = {
180 [AF_IP4] = "linux-cp-xc-l3-ip4",
181 [AF_IP6] = "linux-cp-xc-l3-ip6",
185 const fib_route_path_flags_t lcp_itf_route_path_flags[N_LCP_ITF_HOST] = {
186 [LCP_ITF_HOST_TAP] = FIB_ROUTE_PATH_DVR,
187 [LCP_ITF_HOST_TUN] = FIB_ROUTE_PATH_FLAG_NONE,
191 lcp_itf_unset_adjs (lcp_itf_pair_t *lip)
193 adj_unlock (lip->lip_phy_adjs.adj_index[AF_IP4]);
194 adj_unlock (lip->lip_phy_adjs.adj_index[AF_IP6]);
198 lcp_itf_set_adjs (lcp_itf_pair_t *lip)
200 if (lip->lip_host_type == LCP_ITF_HOST_TUN)
202 lip->lip_phy_adjs.adj_index[AF_IP4] = adj_nbr_add_or_lock (
203 FIB_PROTOCOL_IP4, VNET_LINK_IP4, &zero_addr, lip->lip_phy_sw_if_index);
204 lip->lip_phy_adjs.adj_index[AF_IP6] = adj_nbr_add_or_lock (
205 FIB_PROTOCOL_IP6, VNET_LINK_IP6, &zero_addr, lip->lip_phy_sw_if_index);
209 lip->lip_phy_adjs.adj_index[AF_IP4] = adj_mcast_add_or_lock (
210 FIB_PROTOCOL_IP4, VNET_LINK_IP4, lip->lip_phy_sw_if_index);
211 lip->lip_phy_adjs.adj_index[AF_IP6] = adj_mcast_add_or_lock (
212 FIB_PROTOCOL_IP6, VNET_LINK_IP6, lip->lip_phy_sw_if_index);
217 adj = adj_get (lip->lip_phy_adjs.adj_index[AF_IP4]);
219 lip->lip_rewrite_len = adj->rewrite_header.data_bytes;
223 lcp_itf_pair_add (u32 host_sw_if_index, u32 phy_sw_if_index, u8 *host_name,
224 u32 host_index, lip_host_type_t host_type, u8 *ns)
229 if (host_sw_if_index == ~0)
231 LCP_ITF_PAIR_ERR ("pair_add: Cannot add LIP - invalid host");
232 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
235 lipi = lcp_itf_pair_find_by_phy (phy_sw_if_index);
237 if (lipi != INDEX_INVALID)
238 return VNET_API_ERROR_VALUE_EXIST;
240 LCP_ITF_PAIR_INFO ("add: host:%U phy:%U, host_if:%v vif:%d ns:%s",
241 format_vnet_sw_if_index_name, vnet_get_main (),
242 host_sw_if_index, format_vnet_sw_if_index_name,
243 vnet_get_main (), phy_sw_if_index, host_name, host_index,
249 pool_get (lcp_itf_pair_pool, lip);
251 lipi = lip - lcp_itf_pair_pool;
253 vec_validate_init_empty (lip_db_by_phy, phy_sw_if_index, INDEX_INVALID);
254 vec_validate_init_empty (lip_db_by_host, host_sw_if_index, INDEX_INVALID);
255 lip_db_by_phy[phy_sw_if_index] = lipi;
256 lip_db_by_host[host_sw_if_index] = lipi;
257 hash_set (lip_db_by_vif, host_index, lipi);
259 lip->lip_host_sw_if_index = host_sw_if_index;
260 lip->lip_phy_sw_if_index = phy_sw_if_index;
261 lip->lip_host_name = vec_dup (host_name);
262 lip->lip_host_type = host_type;
263 lip->lip_vif_index = host_index;
264 lip->lip_namespace = vec_dup (ns);
267 * First use of this host interface.
268 * Enable the x-connect feature on the host to send
269 * all packets to the phy.
271 ip_address_family_t af;
273 FOR_EACH_IP_ADDRESS_FAMILY (af)
274 ip_feature_enable_disable (af, N_SAFI, IP_FEATURE_INPUT,
275 lcp_itf_l3_feat_names[lip->lip_host_type][af],
276 lip->lip_host_sw_if_index, 1, NULL, 0);
279 * Configure passive punt to the host interface.
281 fib_route_path_t *rpaths = NULL, rpath = {
282 .frp_flags = lcp_itf_route_path_flags[lip->lip_host_type],
283 .frp_proto = DPO_PROTO_IP4,
284 .frp_sw_if_index = lip->lip_host_sw_if_index,
289 vec_add1 (rpaths, rpath);
291 ip4_punt_redirect_add_paths (lip->lip_phy_sw_if_index, rpaths);
293 rpaths[0].frp_proto = DPO_PROTO_IP6;
295 ip6_punt_redirect_add_paths (lip->lip_phy_sw_if_index, rpaths);
299 lcp_itf_set_adjs (lip);
301 /* enable ARP feature node for broadcast interfaces */
302 if (lip->lip_host_type != LCP_ITF_HOST_TUN)
304 vnet_feature_enable_disable ("arp", "linux-cp-arp-phy",
305 lip->lip_phy_sw_if_index, 1, NULL, 0);
306 vnet_feature_enable_disable ("arp", "linux-cp-arp-host",
307 lip->lip_host_sw_if_index, 1, NULL, 0);
311 vnet_feature_enable_disable ("ip4-punt", "linux-cp-punt-l3", 0, 1, NULL,
313 vnet_feature_enable_disable ("ip6-punt", "linux-cp-punt-l3", 0, 1, NULL,
317 /* invoke registered callbacks for pair addition */
318 lcp_itf_pair_vft_t *vft;
320 vec_foreach (vft, lcp_itf_vfts)
322 if (vft->pair_add_fn)
323 vft->pair_add_fn (lip);
326 /* set timestamp when pair entered service */
327 lip->lip_create_ts = vlib_time_now (vlib_get_main ());
332 static clib_error_t *
333 lcp_netlink_add_link_vlan (int parent, u32 vlan, u16 proto, const char *name)
335 struct rtnl_link *link;
339 sk = nl_socket_alloc ();
340 if ((err = nl_connect (sk, NETLINK_ROUTE)) < 0)
342 LCP_ITF_PAIR_ERR ("netlink_add_link_vlan: connect error: %s",
344 return clib_error_return (NULL, "Unable to connect socket: %d", err);
347 link = rtnl_link_vlan_alloc ();
349 rtnl_link_set_link (link, parent);
350 rtnl_link_set_name (link, name);
351 rtnl_link_vlan_set_id (link, vlan);
352 rtnl_link_vlan_set_protocol (link, htons (proto));
354 if ((err = rtnl_link_add (sk, link, NLM_F_CREATE)) < 0)
356 LCP_ITF_PAIR_ERR ("netlink_add_link_vlan: link add error: %s",
358 return clib_error_return (NULL, "Unable to add link %s: %d", name, err);
361 rtnl_link_put (link);
367 static clib_error_t *
368 lcp_netlink_del_link (const char *name)
370 struct rtnl_link *link;
374 sk = nl_socket_alloc ();
375 if ((err = nl_connect (sk, NETLINK_ROUTE)) < 0)
376 return clib_error_return (NULL, "Unable to connect socket: %d", err);
378 link = rtnl_link_alloc ();
379 rtnl_link_set_name (link, name);
381 if ((err = rtnl_link_delete (sk, link)) < 0)
382 return clib_error_return (NULL, "Unable to del link %s: %d", name, err);
384 rtnl_link_put (link);
391 lcp_itf_pair_del (u32 phy_sw_if_index)
393 ip_address_family_t af;
396 lcp_itf_pair_vft_t *vft;
398 lipi = lcp_itf_pair_find_by_phy (phy_sw_if_index);
400 if (lipi == INDEX_INVALID)
401 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
403 lip = lcp_itf_pair_get (lipi);
405 LCP_ITF_PAIR_NOTICE (
406 "pair_del: host:%U phy:%U host_if:%s vif:%d ns:%s",
407 format_vnet_sw_if_index_name, vnet_get_main (), lip->lip_host_sw_if_index,
408 format_vnet_sw_if_index_name, vnet_get_main (), lip->lip_phy_sw_if_index,
409 lip->lip_host_name, lip->lip_vif_index, lip->lip_namespace);
411 /* invoke registered callbacks for pair deletion */
412 vec_foreach (vft, lcp_itf_vfts)
414 if (vft->pair_del_fn)
415 vft->pair_del_fn (lip);
418 FOR_EACH_IP_ADDRESS_FAMILY (af)
419 ip_feature_enable_disable (af, N_SAFI, IP_FEATURE_INPUT,
420 lcp_itf_l3_feat_names[lip->lip_host_type][af],
421 lip->lip_host_sw_if_index, 0, NULL, 0);
423 lcp_itf_unset_adjs (lip);
425 ip4_punt_redirect_del (lip->lip_phy_sw_if_index);
426 ip6_punt_redirect_del (lip->lip_phy_sw_if_index);
428 /* disable ARP feature node for broadcast interfaces */
429 if (lip->lip_host_type != LCP_ITF_HOST_TUN)
431 vnet_feature_enable_disable ("arp", "linux-cp-arp-phy",
432 lip->lip_phy_sw_if_index, 0, NULL, 0);
433 vnet_feature_enable_disable ("arp", "linux-cp-arp-host",
434 lip->lip_host_sw_if_index, 0, NULL, 0);
438 vnet_feature_enable_disable ("ip4-punt", "linux-cp-punt-l3", 0, 0, NULL,
440 vnet_feature_enable_disable ("ip6-punt", "linux-cp-punt-l3", 0, 0, NULL,
444 lip_db_by_phy[phy_sw_if_index] = INDEX_INVALID;
445 lip_db_by_host[lip->lip_host_sw_if_index] = INDEX_INVALID;
446 hash_unset (lip_db_by_vif, lip->lip_vif_index);
448 vec_free (lip->lip_host_name);
449 vec_free (lip->lip_namespace);
450 pool_put (lcp_itf_pair_pool, lip);
456 lcp_itf_pair_delete_by_index (index_t lipi)
458 u32 host_sw_if_index;
462 lip = lcp_itf_pair_get (lipi);
464 host_name = vec_dup (lip->lip_host_name);
465 host_sw_if_index = lip->lip_host_sw_if_index;
466 ns = vec_dup (lip->lip_namespace);
468 lcp_itf_pair_del (lip->lip_phy_sw_if_index);
470 if (vnet_sw_interface_is_sub (vnet_get_main (), host_sw_if_index))
476 curr_ns_fd = clib_netns_open (NULL /* self */);
477 vif_ns_fd = clib_netns_open ((u8 *) ns);
479 clib_setns (vif_ns_fd);
482 lcp_netlink_del_link ((const char *) host_name);
486 if (curr_ns_fd != -1)
488 clib_setns (curr_ns_fd);
492 vnet_delete_sub_interface (host_sw_if_index);
495 tap_delete_if (vlib_get_main (), host_sw_if_index);
497 vec_free (host_name);
502 lcp_itf_pair_delete (u32 phy_sw_if_index)
506 lipi = lcp_itf_pair_find_by_phy (phy_sw_if_index);
508 if (lipi == INDEX_INVALID)
509 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
511 lcp_itf_pair_delete_by_index (lipi);
517 * lcp_itf_interface_add_del
519 * Registered to receive interface Add and delete notifications
521 static clib_error_t *
522 lcp_itf_interface_add_del (vnet_main_t *vnm, u32 sw_if_index, u32 is_add)
525 /* remove any interface pair we have for this interface */
526 lcp_itf_pair_delete (sw_if_index);
531 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (lcp_itf_interface_add_del);
534 lcp_itf_pair_walk (lcp_itf_pair_walk_cb_t cb, void *ctx)
538 pool_foreach_index (api, lcp_itf_pair_pool)
545 static clib_error_t *
546 lcp_itf_pair_config (vlib_main_t *vm, unformat_input_t *input)
552 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
554 if (unformat (input, "default netns %v", &default_ns))
556 vec_add1 (default_ns, 0);
557 if (lcp_set_default_ns (default_ns) < 0)
559 return clib_error_return (0,
560 "linux-cp default namespace must"
561 " be less than %d characters",
565 else if (unformat (input, "lcp-auto-subint"))
566 lcp_set_auto_subint (1 /* is_auto */);
567 else if (unformat (input, "lcp-sync"))
568 lcp_set_sync (1 /* is_auto */);
570 return clib_error_return (0, "interfaces not found");
573 vec_free (default_ns);
578 VLIB_EARLY_CONFIG_FUNCTION (lcp_itf_pair_config, "linux-cp");
581 * Returns 1 if the tap name is valid.
582 * Returns 0 if the tap name is invalid.
585 lcp_validate_if_name (u8 *name)
591 len = clib_strnlen (p, IFNAMSIZ);
618 lcp_itf_set_link_state (const lcp_itf_pair_t *lip, u8 state)
620 int curr_ns_fd, vif_ns_fd;
625 curr_ns_fd = vif_ns_fd = -1;
627 if (lip->lip_namespace)
629 curr_ns_fd = clib_netns_open (NULL /* self */);
630 vif_ns_fd = clib_netns_open (lip->lip_namespace);
632 clib_setns (vif_ns_fd);
635 /* Set the same link state on the netlink interface
637 vnet_netlink_set_link_state (lip->lip_vif_index, state);
642 if (curr_ns_fd != -1)
644 clib_setns (curr_ns_fd);
652 lcp_itf_set_interface_addr (const lcp_itf_pair_t *lip)
654 ip4_main_t *im4 = &ip4_main;
655 ip6_main_t *im6 = &ip6_main;
656 ip_lookup_main_t *lm4 = &im4->lookup_main;
657 ip_lookup_main_t *lm6 = &im6->lookup_main;
658 ip_interface_address_t *ia = 0;
665 if (lip->lip_namespace)
667 curr_ns_fd = clib_netns_open (NULL /* self */);
668 vif_ns_fd = clib_netns_open (lip->lip_namespace);
670 clib_setns (vif_ns_fd);
673 /* Sync any IP4 addressing info into LCP */
674 foreach_ip_interface_address (
675 lm4, ia, lip->lip_phy_sw_if_index, 1 /* honor unnumbered */, ({
676 ip4_address_t *r4 = ip_interface_address_get_address (lm4, ia);
677 LCP_ITF_PAIR_NOTICE ("set_interface_addr: %U add ip4 %U/%d",
678 format_lcp_itf_pair, lip, format_ip4_address, r4,
680 vnet_netlink_add_ip4_addr (lip->lip_vif_index, r4, ia->address_length);
683 /* Sync any IP6 addressing info into LCP */
684 foreach_ip_interface_address (
685 lm6, ia, lip->lip_phy_sw_if_index, 1 /* honor unnumbered */, ({
686 ip6_address_t *r6 = ip_interface_address_get_address (lm6, ia);
687 LCP_ITF_PAIR_NOTICE ("set_interface_addr: %U add ip6 %U/%d",
688 format_lcp_itf_pair, lip, format_ip6_address, r6,
690 vnet_netlink_add_ip6_addr (lip->lip_vif_index, r6, ia->address_length);
696 if (curr_ns_fd != -1)
698 clib_setns (curr_ns_fd);
708 u32 matched_sw_if_index;
712 lcp_itf_pair_find_walk (vnet_main_t *vnm, u32 sw_if_index, void *arg)
714 lcp_itf_match_t *match = arg;
715 const vnet_sw_interface_t *sw;
717 sw = vnet_get_sw_interface (vnm, sw_if_index);
718 if (sw && (sw->sub.eth.inner_vlan_id == 0) &&
719 (sw->sub.eth.outer_vlan_id == match->vlan) &&
720 (sw->sub.eth.flags.dot1ad == match->dot1ad))
722 LCP_ITF_PAIR_DBG ("find_walk: found match outer %d dot1ad %d "
723 "inner-dot1q %d: interface %U",
724 sw->sub.eth.outer_vlan_id, sw->sub.eth.flags.dot1ad,
725 sw->sub.eth.inner_vlan_id,
726 format_vnet_sw_if_index_name, vnet_get_main (),
728 match->matched_sw_if_index = sw->sw_if_index;
732 return WALK_CONTINUE;
735 /* Return the index of the sub-int on the phy that has the given vlan and
739 lcp_itf_pair_find_by_outer_vlan (u32 sup_if_index, u16 vlan, bool dot1ad)
741 lcp_itf_match_t match;
742 const vnet_hw_interface_t *hw;
745 match.dot1ad = dot1ad;
746 match.matched_sw_if_index = INDEX_INVALID;
747 hw = vnet_get_sup_hw_interface (vnet_get_main (), sup_if_index);
749 vnet_hw_interface_walk_sw (vnet_get_main (), hw->hw_if_index,
750 lcp_itf_pair_find_walk, &match);
752 if (match.matched_sw_if_index >= vec_len (lip_db_by_phy))
753 return INDEX_INVALID;
755 return lip_db_by_phy[match.matched_sw_if_index];
759 lcp_itf_pair_create (u32 phy_sw_if_index, u8 *host_if_name,
760 lip_host_type_t host_if_type, u8 *ns,
761 u32 *host_sw_if_indexp)
765 u32 vif_index = 0, host_sw_if_index;
766 const vnet_sw_interface_t *sw;
767 const vnet_hw_interface_t *hw;
768 const lcp_itf_pair_t *lip;
770 if (!vnet_sw_if_index_is_api_valid (phy_sw_if_index))
772 LCP_ITF_PAIR_ERR ("pair_create: invalid phy index %u", phy_sw_if_index);
773 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
776 if (!lcp_validate_if_name (host_if_name))
778 LCP_ITF_PAIR_ERR ("pair_create: invalid host-if-name '%s'",
780 return VNET_API_ERROR_INVALID_ARGUMENT;
783 vnm = vnet_get_main ();
784 sw = vnet_get_sw_interface (vnm, phy_sw_if_index);
785 hw = vnet_get_sup_hw_interface (vnm, phy_sw_if_index);
788 LCP_ITF_PAIR_ERR ("pair_create: invalid interface");
789 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
793 * Use interface-specific netns if supplied.
794 * Otherwise, use netns if defined, otherwise use the OS default.
796 if (ns == 0 || ns[0] == 0)
797 ns = lcp_get_default_ns ();
799 /* sub interfaces do not need a tap created */
800 if (vnet_sw_interface_is_sub (vnm, phy_sw_if_index))
802 index_t parent_if_index;
803 int orig_ns_fd, ns_fd;
805 u16 outer_vlan, inner_vlan;
806 u16 outer_proto, inner_proto;
808 u32 parent_vif_index;
810 err = vnet_sw_interface_supports_addressing (vnm, phy_sw_if_index);
813 LCP_ITF_PAIR_ERR ("pair_create: can't create LCP for a "
814 "sub-interface without exact-match set");
815 return VNET_API_ERROR_INVALID_ARGUMENT;
818 outer_vlan = sw->sub.eth.outer_vlan_id;
819 inner_vlan = sw->sub.eth.inner_vlan_id;
820 outer_proto = inner_proto = ETH_P_8021Q;
821 if (1 == sw->sub.eth.flags.dot1ad)
822 outer_proto = ETH_P_8021AD;
824 LCP_ITF_PAIR_INFO ("pair_create: subif: dot1%s outer %d inner %d on %U",
825 sw->sub.eth.flags.dot1ad ? "ad" : "q", outer_vlan,
826 inner_vlan, format_vnet_sw_if_index_name, vnm,
829 parent_if_index = lcp_itf_pair_find_by_phy (sw->sup_sw_if_index);
830 if (INDEX_INVALID == parent_if_index)
832 LCP_ITF_PAIR_ERR ("pair_create: can't find LCP for %U",
833 format_vnet_sw_if_index_name, vnet_get_main (),
834 sw->sup_sw_if_index);
835 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
837 lip = lcp_itf_pair_get (parent_if_index);
840 LCP_ITF_PAIR_ERR ("pair_create: can't create LCP for a "
841 "sub-interface without an LCP on the parent");
842 return VNET_API_ERROR_INVALID_ARGUMENT;
844 LCP_ITF_PAIR_DBG ("pair_create: parent %U", format_lcp_itf_pair, lip);
845 parent_vif_index = lip->lip_vif_index;
848 * see if the requested host interface has already been created
850 orig_ns_fd = ns_fd = -1;
853 if (ns && ns[0] != 0)
855 orig_ns_fd = clib_netns_open (NULL /* self */);
856 ns_fd = clib_netns_open (ns);
857 if (orig_ns_fd == -1 || ns_fd == -1)
863 vif_index = if_nametoindex ((const char *) host_if_name);
868 * no existing host interface, create it now
872 * Find the parent tap:
873 * - if this is an outer VLAN, use the pair from the parent phy
874 * - if this is an inner VLAN, find the pair from the outer sub-int,
879 index_t linux_parent_if_index;
880 const lcp_itf_pair_t *llip;
884 linux_parent_if_index = lcp_itf_pair_find_by_outer_vlan (
885 hw->sw_if_index, sw->sub.eth.outer_vlan_id,
886 sw->sub.eth.flags.dot1ad);
887 if (INDEX_INVALID == linux_parent_if_index ||
888 !(llip = lcp_itf_pair_get (linux_parent_if_index)))
891 "pair_create: can't find LCP for outer vlan %d "
894 outer_proto == ETH_P_8021AD ? "dot1ad" : "dot1q",
895 format_vnet_sw_if_index_name, vnm, hw->sw_if_index);
896 err = clib_error_return (0, "parent pair not found");
900 LCP_ITF_PAIR_DBG ("pair_create: linux parent %U",
901 format_lcp_itf_pair, llip);
902 parent_vif_index = llip->lip_vif_index;
910 err = lcp_netlink_add_link_vlan (parent_vif_index, vlan, proto,
911 (const char *) host_if_name);
914 LCP_ITF_PAIR_ERR ("pair_create: cannot create link "
915 "outer(proto:0x%04x,vlan:%u).inner(proto:0x%"
916 "04x,vlan:%u) name:'%s'",
917 outer_proto, outer_vlan, inner_proto,
918 inner_vlan, host_if_name);
922 vif_index = if_nametoindex ((char *) host_if_name);
926 * create a sub-interface on the tap
929 vnet_create_sub_interface (lip->lip_host_sw_if_index, sw->sub.id,
930 sw->sub.eth.raw_flags, inner_vlan,
931 outer_vlan, &host_sw_if_index))
934 "pair_create: failed to create tap subint: %d.%d on %U",
935 outer_vlan, inner_vlan, format_vnet_sw_if_index_name, vnm,
936 lip->lip_host_sw_if_index);
937 err = clib_error_return (
938 0, "failed to create tap subint: %d.%d. on %U", outer_vlan,
939 inner_vlan, format_vnet_sw_if_index_name, vnm,
940 lip->lip_host_sw_if_index);
944 if (orig_ns_fd != -1)
946 clib_setns (orig_ns_fd);
953 return VNET_API_ERROR_INVALID_ARGUMENT;
957 tap_create_if_args_t args = {
958 .num_rx_queues = clib_max (1, vlib_num_workers ()),
960 .id = hw->hw_if_index,
964 .host_if_name = host_if_name,
967 ethernet_interface_t *ei;
969 if (host_if_type == LCP_ITF_HOST_TUN)
970 args.tap_flags |= TAP_FLAG_TUN;
973 ei = pool_elt_at_index (ethernet_main.interfaces, hw->hw_instance);
974 mac_address_copy (&args.host_mac_addr, &ei->address.mac);
977 if (sw->mtu[VNET_MTU_L3])
979 args.host_mtu_set = 1;
980 args.host_mtu_size = sw->mtu[VNET_MTU_L3];
983 if (ns && ns[0] != 0)
984 args.host_namespace = ns;
986 vm = vlib_get_main ();
987 tap_create_if (vm, &args);
991 LCP_ITF_PAIR_ERR ("pair_create: could not create tap, retval:%d",
997 * The TAP interface does copy forward the host MTU based on the VPP
998 * interface's L3 MTU, but it should also ensure that the VPP tap
999 * interface has an MTU that is greater-or-equal to those. Considering
1000 * users can set the interfaces at runtime (set interface mtu packet ...)
1001 * ensure that the tap MTU is large enough, taking the VPP interface L3
1002 * if it's set, and otherwise a sensible default.
1004 if (sw->mtu[VNET_MTU_L3])
1005 vnet_sw_interface_set_mtu (vnm, args.sw_if_index,
1006 sw->mtu[VNET_MTU_L3]);
1008 vnet_sw_interface_set_mtu (vnm, args.sw_if_index,
1009 ETHERNET_MAX_PACKET_BYTES);
1012 * get the hw and ethernet of the tap
1014 hw = vnet_get_sup_hw_interface (vnm, args.sw_if_index);
1015 virtio_main_t *mm = &virtio_main;
1016 virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
1019 * Leave the TAP permanently up on the VPP side.
1020 * This TAP will be shared by many sub-interface.
1021 * Therefore we can't use it to manage admin state.
1022 * force the tap in promiscuous mode.
1024 if (host_if_type == LCP_ITF_HOST_TAP)
1026 ei = pool_elt_at_index (ethernet_main.interfaces, hw->hw_instance);
1027 ei->flags |= ETHERNET_INTERFACE_FLAG_STATUS_L3;
1030 vif_index = vif->ifindex;
1031 host_sw_if_index = args.sw_if_index;
1036 LCP_ITF_PAIR_INFO ("failed pair add (no vif index): {%U, %U, %s}",
1037 format_vnet_sw_if_index_name, vnet_get_main (),
1038 phy_sw_if_index, format_vnet_sw_if_index_name,
1039 vnet_get_main (), host_sw_if_index, host_if_name);
1043 LCP_ITF_PAIR_INFO ("pair create: {%U, %U, %s}", format_vnet_sw_if_index_name,
1044 vnet_get_main (), phy_sw_if_index,
1045 format_vnet_sw_if_index_name, vnet_get_main (),
1046 host_sw_if_index, host_if_name);
1047 lcp_itf_pair_add (host_sw_if_index, phy_sw_if_index, host_if_name, vif_index,
1051 * Copy the link state from VPP into the host side.
1052 * The TAP is shared by many interfaces, always keep it up.
1053 * This controls whether the host can RX/TX.
1056 lip = lcp_itf_pair_get (lcp_itf_pair_find_by_vif (vif_index));
1057 LCP_ITF_PAIR_INFO ("pair create: %U sw-flags %u hw-flags %u",
1058 format_lcp_itf_pair, lip, sw->flags, hw->flags);
1059 vnet_sw_interface_admin_up (vnm, host_sw_if_index);
1060 lcp_itf_set_link_state (lip, sw->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP);
1062 if (host_sw_if_indexp)
1063 *host_sw_if_indexp = host_sw_if_index;
1069 lcp_itf_pair_walk_mark (index_t lipi, void *ctx)
1071 lcp_itf_pair_t *lip;
1073 lip = lcp_itf_pair_get (lipi);
1075 lip->lip_flags |= LIP_FLAG_STALE;
1077 return (WALK_CONTINUE);
1081 lcp_itf_pair_replace_begin (void)
1083 lcp_itf_pair_walk (lcp_itf_pair_walk_mark, NULL);
1088 typedef struct lcp_itf_pair_sweep_ctx_t_
1091 } lcp_itf_pair_sweep_ctx_t;
1094 lcp_itf_pair_walk_sweep (index_t lipi, void *arg)
1096 lcp_itf_pair_sweep_ctx_t *ctx = arg;
1097 lcp_itf_pair_t *lip;
1099 lip = lcp_itf_pair_get (lipi);
1101 if (lip->lip_flags & LIP_FLAG_STALE)
1102 vec_add1 (ctx->indicies, lipi);
1104 return (WALK_CONTINUE);
1108 lcp_itf_pair_replace_end (void)
1110 lcp_itf_pair_sweep_ctx_t ctx = {
1115 lcp_itf_pair_walk (lcp_itf_pair_walk_sweep, &ctx);
1117 vec_foreach (lipi, ctx.indicies)
1118 lcp_itf_pair_delete_by_index (*lipi);
1120 vec_free (ctx.indicies);
1124 static clib_error_t *
1125 lcp_itf_pair_link_up_down (vnet_main_t *vnm, u32 hw_if_index, u32 flags)
1127 vnet_hw_interface_t *hi;
1128 vnet_sw_interface_t *si;
1130 lcp_itf_pair_t *lip;
1132 hi = vnet_get_hw_interface_or_null (vnm, hw_if_index);
1136 lipi = lcp_itf_pair_find_by_phy (hi->sw_if_index);
1137 if (lipi == INDEX_INVALID)
1140 lip = lcp_itf_pair_get (lipi);
1141 si = vnet_get_sw_interface_or_null (vnm, lip->lip_host_sw_if_index);
1145 if (!lcp_main.test_mode)
1147 tap_set_carrier (si->hw_if_index,
1148 (flags & VNET_HW_INTERFACE_FLAG_LINK_UP));
1150 if (flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
1152 tap_set_speed (si->hw_if_index, hi->link_speed / 1000);
1159 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (lcp_itf_pair_link_up_down);
1161 static clib_error_t *
1162 lcp_itf_pair_init (vlib_main_t *vm)
1164 vlib_punt_hdl_t punt_hdl = vlib_punt_client_register ("linux-cp");
1167 vlib_punt_register (punt_hdl, ipsec_punt_reason[IPSEC_PUNT_IP4_SPI_UDP_0],
1170 /* punt all unknown ports */
1171 udp_punt_unknown (vm, 0, 1);
1172 udp_punt_unknown (vm, 1, 1);
1173 tcp_punt_unknown (vm, 0, 1);
1174 tcp_punt_unknown (vm, 1, 1);
1176 lcp_itf_pair_logger = vlib_log_register_class ("linux-cp", "itf");
1181 VLIB_INIT_FUNCTION (lcp_itf_pair_init) = {
1182 .runs_after = VLIB_INITS ("vnet_interface_init", "tcp_init", "udp_init"),
1186 * fd.io coding-style-patch-verification: ON
1189 * eval: (c-set-style "gnu")