linux-cp: Add VPP->Linux synchronization
[vpp.git] / src / plugins / linux-cp / lcp.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 #include <sched.h>
17 #include <fcntl.h>
18 #include <ctype.h>
19 #include <sys/socket.h>
20 #include <net/if.h>
21
22 #include <plugins/linux-cp/lcp.h>
23 #include <plugins/linux-cp/lcp_interface.h>
24
25 lcp_main_t lcp_main;
26
27 u8 *
28 lcp_get_default_ns (void)
29 {
30   lcp_main_t *lcpm = &lcp_main;
31
32   if (!lcpm->default_namespace || lcpm->default_namespace[0] == 0)
33     return NULL;
34
35   return lcpm->default_namespace;
36 }
37
38 int
39 lcp_get_default_ns_fd (void)
40 {
41   lcp_main_t *lcpm = &lcp_main;
42
43   return lcpm->default_ns_fd;
44 }
45
46 /*
47  * ns is expected to be or look like a NUL-terminated C string.
48  */
49 int
50 lcp_set_default_ns (u8 *ns)
51 {
52   lcp_main_t *lcpm = &lcp_main;
53   char *p;
54   int len;
55   u8 *s;
56
57   p = (char *) ns;
58   len = clib_strnlen (p, LCP_NS_LEN);
59   if (len >= LCP_NS_LEN)
60     return -1;
61
62   if (!p || *p == 0)
63     {
64       lcpm->default_namespace = NULL;
65       if (lcpm->default_ns_fd > 0)
66         close (lcpm->default_ns_fd);
67       lcpm->default_ns_fd = 0;
68       return 0;
69     }
70
71   vec_validate_init_c_string (lcpm->default_namespace, p,
72                               clib_strnlen (p, LCP_NS_LEN));
73   s = format (0, "/var/run/netns/%s%c", (char *) lcpm->default_namespace, 0);
74   lcpm->default_ns_fd = open ((char *) s, O_RDONLY);
75   vec_free (s);
76
77   return 0;
78 }
79
80 void
81 lcp_set_sync (u8 is_auto)
82 {
83   lcp_main_t *lcpm = &lcp_main;
84
85   lcpm->lcp_sync = (is_auto != 0);
86
87   // If we set to 'on', do a one-off sync of LCP interfaces
88   if (is_auto)
89     lcp_itf_pair_sync_state_all ();
90 }
91
92 int
93 lcp_sync (void)
94 {
95   lcp_main_t *lcpm = &lcp_main;
96
97   return lcpm->lcp_sync;
98 }
99
100 void
101 lcp_set_auto_subint (u8 is_auto)
102 {
103   lcp_main_t *lcpm = &lcp_main;
104
105   lcpm->lcp_auto_subint = (is_auto != 0);
106 }
107
108 int
109 lcp_auto_subint (void)
110 {
111   lcp_main_t *lcpm = &lcp_main;
112
113   return lcpm->lcp_auto_subint;
114 }
115
116 /*
117  * fd.io coding-style-patch-verification: ON
118  *
119  * Local Variables:
120  * eval: (c-set-style "gnu")
121  * End:
122  */