linux-cp: Linux Interface Mirroring for Control Plane Integration
[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
24 lcp_main_t lcp_main;
25
26 u8 *
27 lcp_get_default_ns (void)
28 {
29   lcp_main_t *lcpm = &lcp_main;
30
31   if (lcpm->default_namespace[0] == 0)
32     return 0;
33   return lcpm->default_namespace;
34 }
35
36 int
37 lcp_get_default_ns_fd (void)
38 {
39   lcp_main_t *lcpm = &lcp_main;
40
41   return lcpm->default_ns_fd;
42 }
43
44 /*
45  * ns is expected to be or look like a NUL-terminated C string.
46  */
47 int
48 lcp_set_default_ns (u8 *ns)
49 {
50   lcp_main_t *lcpm = &lcp_main;
51   char *p;
52   int len;
53   u8 *s;
54
55   p = (char *) ns;
56   len = clib_strnlen (p, LCP_NS_LEN);
57   if (len >= LCP_NS_LEN)
58     return -1;
59
60   if (!p || *p == 0)
61     {
62       clib_memset (lcpm->default_namespace, 0,
63                    sizeof (lcpm->default_namespace));
64       if (lcpm->default_ns_fd > 0)
65         close (lcpm->default_ns_fd);
66       lcpm->default_ns_fd = 0;
67       return 0;
68     }
69
70   clib_strncpy ((char *) lcpm->default_namespace, p, LCP_NS_LEN - 1);
71
72   s = format (0, "/var/run/netns/%s%c", (char *) lcpm->default_namespace, 0);
73   lcpm->default_ns_fd = open ((char *) s, O_RDONLY);
74   vec_free (s);
75
76   return 0;
77 }
78
79 /*
80  * fd.io coding-style-patch-verification: ON
81  *
82  * Local Variables:
83  * eval: (c-set-style "gnu")
84  * End:
85  */