linux-cp: add callbacks for pair management
[vpp.git] / src / plugins / linux-cp / lcp_interface.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #define _GNU_SOURCE
17 #include <sched.h>
18 #include <fcntl.h>
19 #include <ctype.h>
20 #include <sys/socket.h>
21 #include <net/if.h>
22
23 #include <linux-cp/lcp_interface.h>
24 #include <netlink/route/link/vlan.h>
25
26 #include <vnet/plugin/plugin.h>
27 #include <vnet/plugin/plugin.h>
28
29 #include <vnet/ip/ip_punt_drop.h>
30 #include <vnet/fib/fib_table.h>
31 #include <vnet/adj/adj_mcast.h>
32 #include <vnet/udp/udp.h>
33 #include <vnet/tcp/tcp.h>
34 #include <vnet/devices/tap/tap.h>
35 #include <vnet/devices/virtio/virtio.h>
36 #include <vnet/devices/netlink.h>
37 #include <vlibapi/api_helper_macros.h>
38 #include <vnet/ipsec/ipsec_punt.h>
39
40 static vlib_log_class_t lcp_itf_pair_logger;
41
42 /**
43  * Pool of LIP objects
44  */
45 lcp_itf_pair_t *lcp_itf_pair_pool;
46
47 u32
48 lcp_itf_num_pairs (void)
49 {
50   return pool_elts (lcp_itf_pair_pool);
51 }
52
53 /**
54  * DBs of interface-pair objects:
55  *  - key'd by VIF (linux ID)
56  *  - key'd by VPP's physical interface
57  *  - number of shared uses of VPP's tap/host interface
58  */
59 static uword *lip_db_by_vif;
60 index_t *lip_db_by_phy;
61 u32 *lip_db_by_host;
62
63 /**
64  * vector of virtual function table
65  */
66 static lcp_itf_pair_vft_t *lcp_itf_vfts = NULL;
67
68 void
69 lcp_itf_pair_register_vft (lcp_itf_pair_vft_t *lcp_itf_vft)
70 {
71   vec_add1 (lcp_itf_vfts, *lcp_itf_vft);
72 }
73
74 #define LCP_ITF_PAIR_DBG(...)                                                 \
75   vlib_log_notice (lcp_itf_pair_logger, __VA_ARGS__);
76
77 #define LCP_ITF_PAIR_INFO(...)                                                \
78   vlib_log_notice (lcp_itf_pair_logger, __VA_ARGS__);
79
80 u8 *
81 format_lcp_itf_pair (u8 *s, va_list *args)
82 {
83   vnet_main_t *vnm = vnet_get_main ();
84   lcp_itf_pair_t *lip = va_arg (*args, lcp_itf_pair_t *);
85   vnet_sw_interface_t *swif_phy;
86   vnet_sw_interface_t *swif_host;
87
88   s = format (s, "itf-pair: [%d]", lip - lcp_itf_pair_pool);
89
90   swif_phy = vnet_get_sw_interface_or_null (vnm, lip->lip_phy_sw_if_index);
91   if (!swif_phy)
92     s = format (s, " <no-phy-if>");
93   else
94     s = format (s, " %U", format_vnet_sw_interface_name, vnm, swif_phy);
95
96   swif_host = vnet_get_sw_interface_or_null (vnm, lip->lip_host_sw_if_index);
97   if (!swif_host)
98     s = format (s, " <no-host-if>");
99   else
100     s = format (s, " %U", format_vnet_sw_interface_name, vnm, swif_host);
101
102   s = format (s, " %v %d type %s", lip->lip_host_name, lip->lip_vif_index,
103               (lip->lip_host_type == LCP_ITF_HOST_TAP) ? "tap" : "tun");
104
105   if (lip->lip_namespace)
106     s = format (s, " netns %s", lip->lip_namespace);
107
108   return s;
109 }
110
111 static walk_rc_t
112 lcp_itf_pair_walk_show_cb (index_t api, void *ctx)
113 {
114   vlib_main_t *vm;
115   lcp_itf_pair_t *lip;
116
117   lip = lcp_itf_pair_get (api);
118   if (!lip)
119     return WALK_STOP;
120
121   vm = vlib_get_main ();
122   vlib_cli_output (vm, "%U\n", format_lcp_itf_pair, lip);
123
124   return WALK_CONTINUE;
125 }
126
127 void
128 lcp_itf_pair_show (u32 phy_sw_if_index)
129 {
130   vlib_main_t *vm;
131   u8 *ns;
132   index_t api;
133
134   vm = vlib_get_main ();
135   ns = lcp_get_default_ns ();
136   vlib_cli_output (vm, "lcp default netns '%s'\n",
137                    ns ? (char *) ns : "<unset>");
138
139   if (phy_sw_if_index == ~0)
140     {
141       lcp_itf_pair_walk (lcp_itf_pair_walk_show_cb, 0);
142     }
143   else
144     {
145       api = lcp_itf_pair_find_by_phy (phy_sw_if_index);
146       if (api != INDEX_INVALID)
147         lcp_itf_pair_walk_show_cb (api, 0);
148     }
149 }
150
151 lcp_itf_pair_t *
152 lcp_itf_pair_get (u32 index)
153 {
154   return pool_elt_at_index (lcp_itf_pair_pool, index);
155 }
156
157 index_t
158 lcp_itf_pair_find_by_vif (u32 vif_index)
159 {
160   uword *p;
161
162   p = hash_get (lip_db_by_vif, vif_index);
163
164   if (p)
165     return p[0];
166
167   return INDEX_INVALID;
168 }
169
170 int
171 lcp_itf_pair_add_sub (u32 vif, u8 *host_if_name, u32 sub_sw_if_index,
172                       u32 phy_sw_if_index, u8 *ns)
173 {
174   lcp_itf_pair_t *lip;
175
176   lip = lcp_itf_pair_get (lcp_itf_pair_find_by_phy (phy_sw_if_index));
177
178   return lcp_itf_pair_add (lip->lip_host_sw_if_index, sub_sw_if_index,
179                            host_if_name, vif, lip->lip_host_type, ns);
180 }
181
182 const char *lcp_itf_l3_feat_names[N_LCP_ITF_HOST][N_AF] = {
183   [LCP_ITF_HOST_TAP] = {
184     [AF_IP4] = "linux-cp-xc-ip4",
185     [AF_IP6] = "linux-cp-xc-ip6",
186   },
187   [LCP_ITF_HOST_TUN] = {
188     [AF_IP4] = "linux-cp-xc-l3-ip4",
189     [AF_IP6] = "linux-cp-xc-l3-ip6",
190   },
191 };
192
193 const fib_route_path_flags_t lcp_itf_route_path_flags[N_LCP_ITF_HOST] = {
194   [LCP_ITF_HOST_TAP] = FIB_ROUTE_PATH_DVR,
195   [LCP_ITF_HOST_TUN] = FIB_ROUTE_PATH_FLAG_NONE,
196 };
197
198 static void
199 lcp_itf_unset_adjs (lcp_itf_pair_t *lip)
200 {
201   adj_unlock (lip->lip_phy_adjs.adj_index[AF_IP4]);
202   adj_unlock (lip->lip_phy_adjs.adj_index[AF_IP6]);
203 }
204
205 static void
206 lcp_itf_set_adjs (lcp_itf_pair_t *lip)
207 {
208   if (lip->lip_host_type == LCP_ITF_HOST_TUN)
209     {
210       lip->lip_phy_adjs.adj_index[AF_IP4] = adj_nbr_add_or_lock (
211         FIB_PROTOCOL_IP4, VNET_LINK_IP4, &zero_addr, lip->lip_phy_sw_if_index);
212       lip->lip_phy_adjs.adj_index[AF_IP6] = adj_nbr_add_or_lock (
213         FIB_PROTOCOL_IP6, VNET_LINK_IP6, &zero_addr, lip->lip_phy_sw_if_index);
214     }
215   else
216     {
217       lip->lip_phy_adjs.adj_index[AF_IP4] = adj_mcast_add_or_lock (
218         FIB_PROTOCOL_IP4, VNET_LINK_IP4, lip->lip_phy_sw_if_index);
219       lip->lip_phy_adjs.adj_index[AF_IP6] = adj_mcast_add_or_lock (
220         FIB_PROTOCOL_IP6, VNET_LINK_IP6, lip->lip_phy_sw_if_index);
221     }
222
223   ip_adjacency_t *adj;
224
225   adj = adj_get (lip->lip_phy_adjs.adj_index[AF_IP4]);
226
227   lip->lip_rewrite_len = adj->rewrite_header.data_bytes;
228 }
229
230 int
231 lcp_itf_pair_add (u32 host_sw_if_index, u32 phy_sw_if_index, u8 *host_name,
232                   u32 host_index, lip_host_type_t host_type, u8 *ns)
233 {
234   index_t lipi;
235   lcp_itf_pair_t *lip;
236
237   lipi = lcp_itf_pair_find_by_phy (phy_sw_if_index);
238
239   LCP_ITF_PAIR_INFO ("add: host:%U phy:%U, host_if:%v vif:%d ns:%v",
240                      format_vnet_sw_if_index_name, vnet_get_main (),
241                      host_sw_if_index, format_vnet_sw_if_index_name,
242                      vnet_get_main (), phy_sw_if_index, host_name, host_index,
243                      ns);
244
245   if (lipi != INDEX_INVALID)
246     return VNET_API_ERROR_VALUE_EXIST;
247
248   /*
249    * Create a new pair.
250    */
251   pool_get (lcp_itf_pair_pool, lip);
252
253   lipi = lip - lcp_itf_pair_pool;
254
255   vec_validate_init_empty (lip_db_by_phy, phy_sw_if_index, INDEX_INVALID);
256   vec_validate_init_empty (lip_db_by_host, host_sw_if_index, INDEX_INVALID);
257   lip_db_by_phy[phy_sw_if_index] = lipi;
258   lip_db_by_host[host_sw_if_index] = lipi;
259   hash_set (lip_db_by_vif, host_index, lipi);
260
261   lip->lip_host_sw_if_index = host_sw_if_index;
262   lip->lip_phy_sw_if_index = phy_sw_if_index;
263   lip->lip_host_name = vec_dup (host_name);
264   lip->lip_host_type = host_type;
265   lip->lip_vif_index = host_index;
266   lip->lip_namespace = vec_dup (ns);
267
268   if (lip->lip_host_sw_if_index == ~0)
269     return 0;
270
271   /*
272    * First use of this host interface.
273    * Enable the x-connect feature on the host to send
274    * all packets to the phy.
275    */
276   ip_address_family_t af;
277
278   FOR_EACH_IP_ADDRESS_FAMILY (af)
279   ip_feature_enable_disable (af, N_SAFI, IP_FEATURE_INPUT,
280                              lcp_itf_l3_feat_names[lip->lip_host_type][af],
281                              lip->lip_host_sw_if_index, 1, NULL, 0);
282
283   /*
284    * Configure passive punt to the host interface.
285    */
286   fib_route_path_t *rpaths = NULL, rpath = {
287     .frp_flags = lcp_itf_route_path_flags[lip->lip_host_type],
288     .frp_proto = DPO_PROTO_IP4,
289     .frp_sw_if_index = lip->lip_host_sw_if_index,
290     .frp_weight = 1,
291     .frp_fib_index = ~0,
292   };
293
294   vec_add1 (rpaths, rpath);
295
296   ip4_punt_redirect_add_paths (lip->lip_phy_sw_if_index, rpaths);
297
298   rpaths[0].frp_proto = DPO_PROTO_IP6;
299
300   ip6_punt_redirect_add_paths (lip->lip_phy_sw_if_index, rpaths);
301
302   vec_free (rpaths);
303
304   lcp_itf_set_adjs (lip);
305
306   /* enable ARP feature node for broadcast interfaces */
307   if (lip->lip_host_type != LCP_ITF_HOST_TUN)
308     {
309       vnet_feature_enable_disable ("arp", "linux-cp-arp-phy",
310                                    lip->lip_phy_sw_if_index, 1, NULL, 0);
311       vnet_feature_enable_disable ("arp", "linux-cp-arp-host",
312                                    lip->lip_host_sw_if_index, 1, NULL, 0);
313     }
314   else
315     {
316       vnet_feature_enable_disable ("ip4-punt", "linux-cp-punt-l3", 0, 1, NULL,
317                                    0);
318       vnet_feature_enable_disable ("ip6-punt", "linux-cp-punt-l3", 0, 1, NULL,
319                                    0);
320     }
321
322   /* invoke registered callbacks for pair addition */
323   lcp_itf_pair_vft_t *vft;
324
325   vec_foreach (vft, lcp_itf_vfts)
326     {
327       if (vft->pair_add_fn)
328         vft->pair_add_fn (lip);
329     }
330
331   /* set timestamp when pair entered service */
332   lip->lip_create_ts = vlib_time_now (vlib_get_main ());
333
334   return 0;
335 }
336
337 static clib_error_t *
338 lcp_netlink_add_link_vlan (int parent, u32 vlan, const char *name)
339 {
340   struct rtnl_link *link;
341   struct nl_sock *sk;
342   int err;
343
344   sk = nl_socket_alloc ();
345   if ((err = nl_connect (sk, NETLINK_ROUTE)) < 0)
346     return clib_error_return (NULL, "Unable to connect socket: %d", err);
347
348   link = rtnl_link_vlan_alloc ();
349
350   rtnl_link_set_link (link, parent);
351   rtnl_link_set_name (link, name);
352
353   rtnl_link_vlan_set_id (link, vlan);
354
355   if ((err = rtnl_link_add (sk, link, NLM_F_CREATE)) < 0)
356     return clib_error_return (NULL, "Unable to add link %s: %d", name, err);
357
358   rtnl_link_put (link);
359   nl_close (sk);
360
361   return NULL;
362 }
363
364 static clib_error_t *
365 lcp_netlink_del_link (const char *name)
366 {
367   struct rtnl_link *link;
368   struct nl_sock *sk;
369   int err;
370
371   sk = nl_socket_alloc ();
372   if ((err = nl_connect (sk, NETLINK_ROUTE)) < 0)
373     return clib_error_return (NULL, "Unable to connect socket: %d", err);
374
375   link = rtnl_link_alloc ();
376   rtnl_link_set_name (link, name);
377
378   if ((err = rtnl_link_delete (sk, link)) < 0)
379     return clib_error_return (NULL, "Unable to del link %s: %d", name, err);
380
381   rtnl_link_put (link);
382   nl_close (sk);
383
384   return NULL;
385 }
386
387 int
388 lcp_itf_pair_del (u32 phy_sw_if_index)
389 {
390   ip_address_family_t af;
391   lcp_itf_pair_t *lip;
392   u32 lipi;
393   lcp_itf_pair_vft_t *vft;
394
395   lipi = lcp_itf_pair_find_by_phy (phy_sw_if_index);
396
397   if (lipi == INDEX_INVALID)
398     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
399
400   lip = lcp_itf_pair_get (lipi);
401
402   LCP_ITF_PAIR_INFO ("pair delete: {%U, %U, %s}", format_vnet_sw_if_index_name,
403                      vnet_get_main (), lip->lip_phy_sw_if_index,
404                      format_vnet_sw_if_index_name, vnet_get_main (),
405                      lip->lip_host_sw_if_index, lip->lip_host_name);
406
407   /* invoke registered callbacks for pair deletion */
408   vec_foreach (vft, lcp_itf_vfts)
409     {
410       if (vft->pair_del_fn)
411         vft->pair_del_fn (lip);
412     }
413
414   FOR_EACH_IP_ADDRESS_FAMILY (af)
415   ip_feature_enable_disable (af, N_SAFI, IP_FEATURE_INPUT,
416                              lcp_itf_l3_feat_names[lip->lip_host_type][af],
417                              lip->lip_host_sw_if_index, 0, NULL, 0);
418
419   lcp_itf_unset_adjs (lip);
420
421   ip4_punt_redirect_del (lip->lip_phy_sw_if_index);
422   ip6_punt_redirect_del (lip->lip_phy_sw_if_index);
423
424   /* disable ARP feature node for broadcast interfaces */
425   if (lip->lip_host_type != LCP_ITF_HOST_TUN)
426     {
427       vnet_feature_enable_disable ("arp", "linux-cp-arp-phy",
428                                    lip->lip_phy_sw_if_index, 0, NULL, 0);
429       vnet_feature_enable_disable ("arp", "linux-cp-arp-host",
430                                    lip->lip_host_sw_if_index, 0, NULL, 0);
431     }
432   else
433     {
434       vnet_feature_enable_disable ("ip4-punt", "linux-cp-punt-l3", 0, 0, NULL,
435                                    0);
436       vnet_feature_enable_disable ("ip6-punt", "linux-cp-punt-l3", 0, 0, NULL,
437                                    0);
438     }
439
440   lip_db_by_phy[phy_sw_if_index] = INDEX_INVALID;
441   lip_db_by_phy[lip->lip_host_sw_if_index] = INDEX_INVALID;
442
443   vec_free (lip->lip_host_name);
444   vec_free (lip->lip_namespace);
445   pool_put (lcp_itf_pair_pool, lip);
446
447   return 0;
448 }
449
450 static void
451 lcp_itf_pair_delete_by_index (index_t lipi)
452 {
453   u32 host_sw_if_index;
454   lcp_itf_pair_t *lip;
455   u8 *host_name;
456
457   lip = lcp_itf_pair_get (lipi);
458
459   host_name = vec_dup (lip->lip_host_name);
460   host_sw_if_index = lip->lip_host_sw_if_index;
461
462   lcp_itf_pair_del (lip->lip_phy_sw_if_index);
463
464   if (vnet_sw_interface_is_sub (vnet_get_main (), host_sw_if_index))
465     {
466       lcp_netlink_del_link ((const char *) host_name);
467       vnet_delete_sub_interface (host_sw_if_index);
468     }
469   else
470     tap_delete_if (vlib_get_main (), host_sw_if_index);
471
472   vec_free (host_name);
473 }
474
475 int
476 lcp_itf_pair_delete (u32 phy_sw_if_index)
477 {
478   index_t lipi;
479
480   lipi = lcp_itf_pair_find_by_phy (phy_sw_if_index);
481
482   if (lipi == INDEX_INVALID)
483     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
484
485   lcp_itf_pair_delete_by_index (lipi);
486
487   return 0;
488 }
489
490 void
491 lcp_itf_pair_walk (lcp_itf_pair_walk_cb_t cb, void *ctx)
492 {
493   u32 api;
494
495   pool_foreach_index (api, lcp_itf_pair_pool)
496     {
497       if (!cb (api, ctx))
498         break;
499     };
500 }
501
502 typedef struct lcp_itf_pair_names_t_
503 {
504   u8 *lipn_host_name;
505   u8 *lipn_phy_name;
506   u8 *lipn_namespace;
507   u32 lipn_phy_sw_if_index;
508 } lcp_itf_pair_names_t;
509
510 static lcp_itf_pair_names_t *lipn_names;
511
512 static clib_error_t *
513 lcp_itf_pair_config (vlib_main_t *vm, unformat_input_t *input)
514 {
515   u8 *host, *phy;
516   u8 *ns;
517   u8 *default_ns;
518
519   host = phy = ns = default_ns = NULL;
520
521   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
522     {
523       vec_reset_length (host);
524
525       if (unformat (input, "pair %s %s %s", &phy, &host, &ns))
526         {
527           lcp_itf_pair_names_t *lipn;
528
529           if (vec_len (ns) > LCP_NS_LEN)
530             {
531               return clib_error_return (0,
532                                         "linux-cp IF namespace must"
533                                         " be less than %d characters",
534                                         LCP_NS_LEN);
535             }
536
537           vec_add2 (lipn_names, lipn, 1);
538
539           lipn->lipn_host_name = vec_dup (host);
540           lipn->lipn_phy_name = vec_dup (phy);
541           lipn->lipn_namespace = vec_dup (ns);
542         }
543       else if (unformat (input, "pair %v %v", &phy, &host))
544         {
545           lcp_itf_pair_names_t *lipn;
546
547           vec_add2 (lipn_names, lipn, 1);
548
549           lipn->lipn_host_name = vec_dup (host);
550           lipn->lipn_phy_name = vec_dup (phy);
551           lipn->lipn_namespace = 0;
552         }
553       else if (unformat (input, "default netns %v", &default_ns))
554         {
555           vec_add1 (default_ns, 0);
556           if (lcp_set_default_ns (default_ns) < 0)
557             {
558               return clib_error_return (0,
559                                         "linux-cp default namespace must"
560                                         " be less than %d characters",
561                                         LCP_NS_LEN);
562             }
563         }
564       else if (unformat (input, "interface-auto-create"))
565         lcp_set_auto_intf (1 /* is_auto */);
566       else
567         return clib_error_return (0, "interfaces not found");
568     }
569
570   vec_free (host);
571   vec_free (phy);
572   vec_free (default_ns);
573
574   return NULL;
575 }
576
577 VLIB_EARLY_CONFIG_FUNCTION (lcp_itf_pair_config, "linux-cp");
578
579 /*
580  * Returns 1 if the tap name is valid.
581  * Returns 0 if the tap name is invalid.
582  */
583 static int
584 lcp_validate_if_name (u8 *name)
585 {
586   int len;
587   char *p;
588
589   p = (char *) name;
590   len = clib_strnlen (p, IFNAMSIZ);
591   if (len >= IFNAMSIZ)
592     return 0;
593
594   for (; *p; ++p)
595     {
596       if (isalnum (*p))
597         continue;
598
599       switch (*p)
600         {
601         case '-':
602         case '_':
603         case '%':
604         case '@':
605         case ':':
606         case '.':
607           continue;
608         }
609
610       return 0;
611     }
612
613   return 1;
614 }
615
616 static int
617 lcp_itf_get_ns_fd (char *ns_name)
618 {
619   char ns_path[256] = "/proc/self/ns/net";
620
621   if (ns_name)
622     snprintf (ns_path, sizeof (ns_path) - 1, "/var/run/netns/%s", ns_name);
623
624   return open (ns_path, O_RDONLY);
625 }
626
627 static void
628 lcp_itf_set_vif_link_state (u32 vif_index, u8 up, u8 *ns)
629 {
630   int curr_ns_fd, vif_ns_fd;
631
632   curr_ns_fd = vif_ns_fd = -1;
633
634   if (ns)
635     {
636       u8 *ns_path = 0;
637
638       curr_ns_fd = open ("/proc/self/ns/net", O_RDONLY);
639       ns_path = format (0, "/var/run/netns/%s%c", (char *) ns, 0);
640       vif_ns_fd = open ((char *) ns_path, O_RDONLY);
641       if (vif_ns_fd != -1)
642         setns (vif_ns_fd, CLONE_NEWNET);
643     }
644
645   vnet_netlink_set_link_state (vif_index, up);
646
647   if (vif_ns_fd != -1)
648     close (vif_ns_fd);
649
650   if (curr_ns_fd != -1)
651     {
652       setns (curr_ns_fd, CLONE_NEWNET);
653       close (curr_ns_fd);
654     }
655 }
656
657 int
658 lcp_itf_pair_create (u32 phy_sw_if_index, u8 *host_if_name,
659                      lip_host_type_t host_if_type, u8 *ns,
660                      u32 *host_sw_if_indexp)
661 {
662   vlib_main_t *vm;
663   vnet_main_t *vnm;
664   u32 vif_index = 0, host_sw_if_index;
665   const vnet_sw_interface_t *sw;
666   const vnet_hw_interface_t *hw;
667
668   if (!vnet_sw_if_index_is_api_valid (phy_sw_if_index))
669     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
670
671   if (!lcp_validate_if_name (host_if_name))
672     return VNET_API_ERROR_INVALID_ARGUMENT;
673
674   vnm = vnet_get_main ();
675   sw = vnet_get_sw_interface (vnm, phy_sw_if_index);
676   hw = vnet_get_sup_hw_interface (vnm, phy_sw_if_index);
677
678   /*
679    * Use interface-specific netns if supplied.
680    * Otherwise, use default netns if defined.
681    * Otherwise ignore a netns and use the OS default.
682    */
683   if (ns == 0 || ns[0] == 0)
684     ns = lcp_get_default_ns ();
685
686   /* sub interfaces do not need a tap created */
687   if (vnet_sw_interface_is_sub (vnm, phy_sw_if_index))
688     {
689       const lcp_itf_pair_t *lip;
690       int orig_ns_fd, ns_fd;
691       clib_error_t *err;
692       u16 vlan;
693
694       /*
695        * Find the parent tap by finding the pair from the parent phy
696        */
697       lip = lcp_itf_pair_get (lcp_itf_pair_find_by_phy (sw->sup_sw_if_index));
698       vlan = sw->sub.eth.outer_vlan_id;
699
700       /*
701        * see if the requested host interface has already been created
702        */
703       orig_ns_fd = ns_fd = -1;
704       err = NULL;
705
706       if (ns && ns[0] != 0)
707         {
708           orig_ns_fd = lcp_itf_get_ns_fd (NULL);
709           ns_fd = lcp_itf_get_ns_fd ((char *) ns);
710           if (orig_ns_fd == -1 || ns_fd == -1)
711             goto socket_close;
712
713           setns (ns_fd, CLONE_NEWNET);
714         }
715
716       vif_index = if_nametoindex ((const char *) host_if_name);
717
718       if (!vif_index)
719         {
720           /*
721            * no existing host interface, create it now
722            */
723           err = lcp_netlink_add_link_vlan (lip->lip_vif_index, vlan,
724                                            (const char *) host_if_name);
725
726           if (!err && -1 != ns_fd)
727             err = vnet_netlink_set_link_netns (vif_index, ns_fd, NULL);
728
729           if (!err)
730             vif_index = if_nametoindex ((char *) host_if_name);
731         }
732
733       /*
734        * create a sub-interface on the tap
735        */
736       if (!err && vnet_create_sub_interface (lip->lip_host_sw_if_index,
737                                              sw->sub.id, sw->sub.eth.raw_flags,
738                                              sw->sub.eth.inner_vlan_id, vlan,
739                                              &host_sw_if_index))
740         LCP_ITF_PAIR_INFO ("failed create vlan: %d on %U", vlan,
741                            format_vnet_sw_if_index_name, vnet_get_main (),
742                            lip->lip_host_sw_if_index);
743
744     socket_close:
745       if (orig_ns_fd != -1)
746         {
747           setns (orig_ns_fd, CLONE_NEWNET);
748           close (orig_ns_fd);
749         }
750       if (ns_fd != -1)
751         close (ns_fd);
752
753       if (err)
754         return VNET_API_ERROR_INVALID_ARGUMENT;
755     }
756   else
757     {
758       tap_create_if_args_t args = {
759         .num_rx_queues = clib_max (1, vlib_num_workers ()),
760         .id = hw->hw_if_index,
761         .sw_if_index = ~0,
762         .rx_ring_sz = 256,
763         .tx_ring_sz = 256,
764         .host_if_name = host_if_name,
765         .host_namespace = 0,
766       };
767       ethernet_interface_t *ei;
768
769       if (host_if_type == LCP_ITF_HOST_TUN)
770         args.tap_flags |= TAP_FLAG_TUN;
771       else
772         {
773           ei = pool_elt_at_index (ethernet_main.interfaces, hw->hw_instance);
774           mac_address_copy (&args.host_mac_addr, &ei->address.mac);
775         }
776
777       if (sw->mtu[VNET_MTU_L3])
778         {
779           args.host_mtu_set = 1;
780           args.host_mtu_size = sw->mtu[VNET_MTU_L3];
781         }
782
783       if (ns && ns[0] != 0)
784         args.host_namespace = ns;
785
786       vm = vlib_get_main ();
787       tap_create_if (vm, &args);
788
789       if (args.rv < 0)
790         {
791           return args.rv;
792         }
793
794       /*
795        * get the hw and ethernet of the tap
796        */
797       hw = vnet_get_sup_hw_interface (vnm, args.sw_if_index);
798
799       /*
800        * Set the interface down on the host side.
801        * This controls whether the host can RX/TX.
802        */
803       virtio_main_t *mm = &virtio_main;
804       virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
805
806       lcp_itf_set_vif_link_state (vif->ifindex, 0 /* down */,
807                                   args.host_namespace);
808
809       /*
810        * Leave the TAP permanently up on the VPP side.
811        * This TAP will be shared by many sub-interface.
812        * Therefore we can't use it to manage admin state.
813        * force the tap in promiscuous mode.
814        */
815       if (host_if_type == LCP_ITF_HOST_TAP)
816         {
817           ei = pool_elt_at_index (ethernet_main.interfaces, hw->hw_instance);
818           ei->flags |= ETHERNET_INTERFACE_FLAG_STATUS_L3;
819         }
820
821       vif_index = vif->ifindex;
822       host_sw_if_index = args.sw_if_index;
823     }
824
825   if (!vif_index)
826     {
827       LCP_ITF_PAIR_INFO ("failed pair add (no vif index): {%U, %U, %s}",
828                          format_vnet_sw_if_index_name, vnet_get_main (),
829                          phy_sw_if_index, format_vnet_sw_if_index_name,
830                          vnet_get_main (), host_sw_if_index, host_if_name);
831       return -1;
832     }
833
834   vnet_sw_interface_admin_up (vnm, host_sw_if_index);
835   lcp_itf_pair_add (host_sw_if_index, phy_sw_if_index, host_if_name, vif_index,
836                     host_if_type, ns);
837
838   LCP_ITF_PAIR_INFO ("pair create: {%U, %U, %s}", format_vnet_sw_if_index_name,
839                      vnet_get_main (), phy_sw_if_index,
840                      format_vnet_sw_if_index_name, vnet_get_main (),
841                      host_sw_if_index, host_if_name);
842
843   if (host_sw_if_indexp)
844     *host_sw_if_indexp = host_sw_if_index;
845
846   return 0;
847 }
848
849 static walk_rc_t
850 lcp_itf_pair_walk_mark (index_t lipi, void *ctx)
851 {
852   lcp_itf_pair_t *lip;
853
854   lip = lcp_itf_pair_get (lipi);
855
856   lip->lip_flags |= LIP_FLAG_STALE;
857
858   return (WALK_CONTINUE);
859 }
860
861 int
862 lcp_itf_pair_replace_begin (void)
863 {
864   lcp_itf_pair_walk (lcp_itf_pair_walk_mark, NULL);
865
866   return (0);
867 }
868
869 typedef struct lcp_itf_pair_sweep_ctx_t_
870 {
871   index_t *indicies;
872 } lcp_itf_pair_sweep_ctx_t;
873
874 static walk_rc_t
875 lcp_itf_pair_walk_sweep (index_t lipi, void *arg)
876 {
877   lcp_itf_pair_sweep_ctx_t *ctx = arg;
878   lcp_itf_pair_t *lip;
879
880   lip = lcp_itf_pair_get (lipi);
881
882   if (lip->lip_flags & LIP_FLAG_STALE)
883     vec_add1 (ctx->indicies, lipi);
884
885   return (WALK_CONTINUE);
886 }
887
888 int
889 lcp_itf_pair_replace_end (void)
890 {
891   lcp_itf_pair_sweep_ctx_t ctx = {
892     .indicies = NULL,
893   };
894   index_t *lipi;
895
896   lcp_itf_pair_walk (lcp_itf_pair_walk_sweep, &ctx);
897
898   vec_foreach (lipi, ctx.indicies)
899     lcp_itf_pair_delete_by_index (*lipi);
900
901   vec_free (ctx.indicies);
902   return (0);
903 }
904
905 static uword
906 lcp_itf_pair_process (vlib_main_t *vm, vlib_node_runtime_t *rt,
907                       vlib_frame_t *f)
908 {
909   uword *event_data = 0;
910   uword *lipn_index;
911
912   while (1)
913     {
914       vlib_process_wait_for_event (vm);
915
916       vlib_process_get_events (vm, &event_data);
917
918       vec_foreach (lipn_index, event_data)
919         {
920           lcp_itf_pair_names_t *lipn;
921
922           lipn = &lipn_names[*lipn_index];
923           lcp_itf_pair_create (lipn->lipn_phy_sw_if_index,
924                                lipn->lipn_host_name, LCP_ITF_HOST_TAP,
925                                lipn->lipn_namespace, NULL);
926         }
927
928       vec_reset_length (event_data);
929     }
930
931   return 0;
932 }
933
934 VLIB_REGISTER_NODE (lcp_itf_pair_process_node, static) = {
935   .function = lcp_itf_pair_process,
936   .name = "linux-cp-itf-process",
937   .type = VLIB_NODE_TYPE_PROCESS,
938 };
939
940 static clib_error_t *
941 lcp_itf_phy_add (vnet_main_t *vnm, u32 sw_if_index, u32 is_create)
942 {
943   lcp_itf_pair_names_t *lipn;
944   vlib_main_t *vm = vlib_get_main ();
945   vnet_hw_interface_t *hw;
946
947   if (!is_create || vnet_sw_interface_is_sub (vnm, sw_if_index))
948     return NULL;
949
950   hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
951
952   vec_foreach (lipn, lipn_names)
953     {
954       if (!vec_cmp (hw->name, lipn->lipn_phy_name))
955         {
956           lipn->lipn_phy_sw_if_index = sw_if_index;
957
958           vlib_process_signal_event (vm, lcp_itf_pair_process_node.index, 0,
959                                      lipn - lipn_names);
960           break;
961         }
962     }
963
964   return NULL;
965 }
966
967 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (lcp_itf_phy_add);
968
969 static clib_error_t *
970 lcp_itf_pair_link_up_down (vnet_main_t *vnm, u32 hw_if_index, u32 flags)
971 {
972   vnet_hw_interface_t *hi;
973   vnet_sw_interface_t *si;
974   index_t lipi;
975   lcp_itf_pair_t *lip;
976
977   hi = vnet_get_hw_interface_or_null (vnm, hw_if_index);
978   if (!hi)
979     return 0;
980
981   lipi = lcp_itf_pair_find_by_phy (hi->sw_if_index);
982   if (lipi == INDEX_INVALID)
983     return 0;
984
985   lip = lcp_itf_pair_get (lipi);
986   si = vnet_get_sw_interface_or_null (vnm, lip->lip_host_sw_if_index);
987   if (!si)
988     return 0;
989
990   if (!lcp_main.test_mode)
991     {
992       tap_set_carrier (si->hw_if_index,
993                        (flags & VNET_HW_INTERFACE_FLAG_LINK_UP));
994
995       if (flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
996         {
997           tap_set_speed (si->hw_if_index, hi->link_speed / 1000);
998         }
999     }
1000
1001   return 0;
1002 }
1003
1004 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (lcp_itf_pair_link_up_down);
1005
1006 static clib_error_t *
1007 lcp_itf_pair_init (vlib_main_t *vm)
1008 {
1009   vlib_punt_hdl_t punt_hdl = vlib_punt_client_register ("linux-cp");
1010
1011   /* punt IKE */
1012   vlib_punt_register (punt_hdl, ipsec_punt_reason[IPSEC_PUNT_IP4_SPI_UDP_0],
1013                       "linux-cp-punt");
1014
1015   /* punt all unknown ports */
1016   udp_punt_unknown (vm, 0, 1);
1017   udp_punt_unknown (vm, 1, 1);
1018   tcp_punt_unknown (vm, 0, 1);
1019   tcp_punt_unknown (vm, 1, 1);
1020
1021   lcp_itf_pair_logger = vlib_log_register_class ("linux-cp", "itf");
1022
1023   return NULL;
1024 }
1025
1026 VLIB_INIT_FUNCTION (lcp_itf_pair_init) = {
1027   .runs_after = VLIB_INITS ("vnet_interface_init", "tcp_init", "udp_init"),
1028 };
1029
1030 /*
1031  * fd.io coding-style-patch-verification: ON
1032  *
1033  * Local Variables:
1034  * eval: (c-set-style "gnu")
1035  * End:
1036  */