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
, "",
41 static int logicalrouter_backend
;
43 /* Get a NS. If not found, create one. */
44 static struct ns
*logicalrouter_get(ns_id_t ns_id
)
48 ns
= ns_lookup(ns_id
);
51 ns
= ns_get_created(ns
, NULL
, ns_id
);
55 static int logicalrouter_is_backend_netns(void)
57 return (logicalrouter_backend
== LOGICALROUTER_BACKEND_NETNS
);
61 DEFUN_NOSH (logicalrouter
,
63 "logical-router (1-65535) ns NAME",
64 "Enable a logical-router\n"
65 "Specify the logical-router indentifier\n"
67 "The file name in " NS_RUN_DIR
", or a full pathname\n")
73 char *pathname
= ns_netns_pathname(vty
, argv
[idx_name
]->arg
);
76 return CMD_WARNING_CONFIG_FAILED
;
78 ns_id
= strtoul(argv
[idx_number
]->arg
, NULL
, 10);
79 ns
= logicalrouter_get(ns_id
);
81 if (ns
->name
&& strcmp(ns
->name
, pathname
) != 0) {
82 vty_out(vty
, "NS %u is already configured with NETNS %s\n",
88 ns
->name
= XSTRDUP(MTYPE_LOGICALROUTER_NAME
, pathname
);
90 if (!ns_enable(ns
, NULL
)) {
91 vty_out(vty
, "Can not associate NS %u with NETNS %s\n",
93 return CMD_WARNING_CONFIG_FAILED
;
99 DEFUN (no_logicalrouter
,
100 no_logicalrouter_cmd
,
101 "no logical-router (1-65535) ns NAME",
103 "Enable a Logical-Router\n"
104 "Specify the Logical-Router identifier\n"
106 "The file name in " NS_RUN_DIR
", or a full pathname\n")
111 struct ns
*ns
= NULL
;
112 char *pathname
= ns_netns_pathname(vty
, argv
[idx_name
]->arg
);
115 return CMD_WARNING_CONFIG_FAILED
;
117 ns_id
= strtoul(argv
[idx_number
]->arg
, NULL
, 10);
118 ns
= ns_lookup(ns_id
);
121 vty_out(vty
, "NS %u is not found\n", ns_id
);
125 if (ns
->name
&& strcmp(ns
->name
, pathname
) != 0) {
126 vty_out(vty
, "Incorrect NETNS file name\n");
127 return CMD_WARNING_CONFIG_FAILED
;
133 XFREE(MTYPE_LOGICALROUTER_NAME
, ns
->name
);
140 /* Initialize NS module. */
141 void logicalrouter_init(int (*writefunc
)(struct vty
*vty
))
143 if (ns_have_netns() && logicalrouter_is_backend_netns()) {
144 /* Install LogicalRouter commands. */
145 install_node(&logicalrouter_node
, writefunc
);
146 install_element(CONFIG_NODE
, &logicalrouter_cmd
);
147 install_element(CONFIG_NODE
, &no_logicalrouter_cmd
);
151 void logicalrouter_terminate(void)
156 void logicalrouter_configure_backend(int backend_netns
)
158 logicalrouter_backend
= backend_netns
;