]>
Commit | Line | Data |
---|---|---|
e26aedbe PG |
1 | /* |
2 | * Logical Router functions. | |
3 | * Copyright (C) 2018 6WIND S.A. | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License as published by the Free | |
7 | * Software Foundation; either version 2 of the License, or (at your option) | |
8 | * any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
13 | * more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along | |
16 | * with this program; see the file COPYING; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | */ | |
19 | ||
20 | #include <zebra.h> | |
21 | ||
22 | #include "ns.h" | |
23 | #include "log.h" | |
24 | #include "memory.h" | |
25 | ||
26 | #include "command.h" | |
27 | #include "vty.h" | |
28 | #include "logicalrouter.h" | |
29 | ||
30 | /* Comment that useless define to avoid compilation error | |
31 | * in order to use it, one could provide the kind of NETNS to NS backend | |
32 | * so that the allocation will match the logical router | |
33 | * DEFINE_MTYPE_STATIC(LIB, LOGICALROUTER, "LogicalRouter Context") | |
34 | */ | |
35 | DEFINE_MTYPE_STATIC(LIB, LOGICALROUTER_NAME, "Logical Router Name") | |
36 | ||
37 | /* Logical Router node has no interface. */ | |
996c9314 | 38 | static struct cmd_node logicalrouter_node = {LOGICALROUTER_NODE, "", 1}; |
e26aedbe PG |
39 | |
40 | static int logicalrouter_backend; | |
41 | ||
42 | /* Get a NS. If not found, create one. */ | |
43 | static struct ns *logicalrouter_get(ns_id_t ns_id) | |
44 | { | |
45 | struct ns *ns; | |
46 | ||
47 | ns = ns_lookup(ns_id); | |
48 | if (ns) | |
49 | return (ns); | |
50 | ns = ns_get_created(ns, NULL, ns_id); | |
51 | return ns; | |
52 | } | |
53 | ||
54 | static int logicalrouter_is_backend_netns(void) | |
55 | { | |
56 | return (logicalrouter_backend == LOGICALROUTER_BACKEND_NETNS); | |
57 | } | |
58 | ||
59 | ||
60 | DEFUN_NOSH (logicalrouter, | |
61 | logicalrouter_cmd, | |
62 | "logical-router (1-65535) ns NAME", | |
63 | "Enable a logical-router\n" | |
64 | "Specify the logical-router indentifier\n" | |
65 | "The Name Space\n" | |
66 | "The file name in " NS_RUN_DIR ", or a full pathname\n") | |
67 | { | |
68 | int idx_number = 1; | |
69 | int idx_name = 3; | |
70 | ns_id_t ns_id; | |
71 | struct ns *ns = NULL; | |
72 | char *pathname = ns_netns_pathname(vty, argv[idx_name]->arg); | |
73 | ||
74 | if (!pathname) | |
75 | return CMD_WARNING_CONFIG_FAILED; | |
76 | ||
77 | ns_id = strtoul(argv[idx_number]->arg, NULL, 10); | |
78 | ns = logicalrouter_get(ns_id); | |
79 | ||
80 | if (ns->name && strcmp(ns->name, pathname) != 0) { | |
81 | vty_out(vty, "NS %u is already configured with NETNS %s\n", | |
82 | ns->ns_id, ns->name); | |
83 | return CMD_WARNING; | |
84 | } | |
85 | ||
86 | if (!ns->name) | |
87 | ns->name = XSTRDUP(MTYPE_LOGICALROUTER_NAME, pathname); | |
88 | ||
89 | if (!ns_enable(ns, NULL)) { | |
90 | vty_out(vty, "Can not associate NS %u with NETNS %s\n", | |
91 | ns->ns_id, ns->name); | |
92 | return CMD_WARNING_CONFIG_FAILED; | |
93 | } | |
94 | ||
95 | return CMD_SUCCESS; | |
96 | } | |
97 | ||
98 | DEFUN (no_logicalrouter, | |
99 | no_logicalrouter_cmd, | |
100 | "no logical-router (1-65535) ns NAME", | |
101 | NO_STR | |
102 | "Enable a Logical-Router\n" | |
103 | "Specify the Logical-Router identifier\n" | |
104 | "The Name Space\n" | |
105 | "The file name in " NS_RUN_DIR ", or a full pathname\n") | |
106 | { | |
107 | int idx_number = 2; | |
108 | int idx_name = 4; | |
109 | ns_id_t ns_id; | |
110 | struct ns *ns = NULL; | |
111 | char *pathname = ns_netns_pathname(vty, argv[idx_name]->arg); | |
112 | ||
113 | if (!pathname) | |
114 | return CMD_WARNING_CONFIG_FAILED; | |
115 | ||
116 | ns_id = strtoul(argv[idx_number]->arg, NULL, 10); | |
117 | ns = ns_lookup(ns_id); | |
118 | ||
119 | if (!ns) { | |
120 | vty_out(vty, "NS %u is not found\n", ns_id); | |
121 | return CMD_SUCCESS; | |
122 | } | |
123 | ||
124 | if (ns->name && strcmp(ns->name, pathname) != 0) { | |
125 | vty_out(vty, "Incorrect NETNS file name\n"); | |
126 | return CMD_WARNING_CONFIG_FAILED; | |
127 | } | |
128 | ||
129 | ns_disable(ns); | |
130 | ||
131 | if (ns->name) { | |
132 | XFREE(MTYPE_LOGICALROUTER_NAME, ns->name); | |
133 | ns->name = NULL; | |
134 | } | |
135 | ||
136 | return CMD_SUCCESS; | |
137 | } | |
138 | ||
139 | /* Initialize NS module. */ | |
140 | void logicalrouter_init(int (*writefunc)(struct vty *vty)) | |
141 | { | |
142 | if (ns_have_netns() && logicalrouter_is_backend_netns()) { | |
143 | /* Install LogicalRouter commands. */ | |
144 | install_node(&logicalrouter_node, writefunc); | |
145 | install_element(CONFIG_NODE, &logicalrouter_cmd); | |
146 | install_element(CONFIG_NODE, &no_logicalrouter_cmd); | |
147 | } | |
148 | } | |
149 | ||
150 | void logicalrouter_terminate(void) | |
151 | { | |
152 | ns_terminate(); | |
153 | } | |
154 | ||
155 | void logicalrouter_configure_backend(int backend_netns) | |
156 | { | |
157 | logicalrouter_backend = backend_netns; | |
158 | } |