2 * Logical Router functions.
3 * Copyright (C) 2018 6WIND S.A.
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)
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
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
28 #include "logicalrouter.h"
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")
35 DEFINE_MTYPE_STATIC(LIB
, LOGICALROUTER_NAME
, "Logical Router Name")
37 /* Logical Router node has no interface. */
38 static struct cmd_node logicalrouter_node
= {LOGICALROUTER_NODE
, "", 1};
40 static int logicalrouter_backend
;
42 /* Get a NS. If not found, create one. */
43 static struct ns
*logicalrouter_get(ns_id_t ns_id
)
47 ns
= ns_lookup(ns_id
);
50 ns
= ns_get_created(ns
, NULL
, ns_id
);
54 static int logicalrouter_is_backend_netns(void)
56 return (logicalrouter_backend
== LOGICALROUTER_BACKEND_NETNS
);
60 DEFUN_NOSH (logicalrouter
,
62 "logical-router (1-65535) ns NAME",
63 "Enable a logical-router\n"
64 "Specify the logical-router indentifier\n"
66 "The file name in " NS_RUN_DIR
", or a full pathname\n")
72 char *pathname
= ns_netns_pathname(vty
, argv
[idx_name
]->arg
);
75 return CMD_WARNING_CONFIG_FAILED
;
77 ns_id
= strtoul(argv
[idx_number
]->arg
, NULL
, 10);
78 ns
= logicalrouter_get(ns_id
);
80 if (ns
->name
&& strcmp(ns
->name
, pathname
) != 0) {
81 vty_out(vty
, "NS %u is already configured with NETNS %s\n",
87 ns
->name
= XSTRDUP(MTYPE_LOGICALROUTER_NAME
, pathname
);
89 if (!ns_enable(ns
, NULL
)) {
90 vty_out(vty
, "Can not associate NS %u with NETNS %s\n",
92 return CMD_WARNING_CONFIG_FAILED
;
98 DEFUN (no_logicalrouter
,
100 "no logical-router (1-65535) ns NAME",
102 "Enable a Logical-Router\n"
103 "Specify the Logical-Router identifier\n"
105 "The file name in " NS_RUN_DIR
", or a full pathname\n")
110 struct ns
*ns
= NULL
;
111 char *pathname
= ns_netns_pathname(vty
, argv
[idx_name
]->arg
);
114 return CMD_WARNING_CONFIG_FAILED
;
116 ns_id
= strtoul(argv
[idx_number
]->arg
, NULL
, 10);
117 ns
= ns_lookup(ns_id
);
120 vty_out(vty
, "NS %u is not found\n", ns_id
);
124 if (ns
->name
&& strcmp(ns
->name
, pathname
) != 0) {
125 vty_out(vty
, "Incorrect NETNS file name\n");
126 return CMD_WARNING_CONFIG_FAILED
;
132 XFREE(MTYPE_LOGICALROUTER_NAME
, ns
->name
);
139 /* Initialize NS module. */
140 void logicalrouter_init(int (*writefunc
)(struct vty
*vty
))
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
);
150 void logicalrouter_terminate(void)
155 void logicalrouter_configure_backend(int backend_netns
)
157 logicalrouter_backend
= backend_netns
;