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