test.c revision 4c047107
1/*
2 * Copyright (c) 2016 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 <librtnl/netns.h>
17
18#include <vnet/plugin/plugin.h>
19#include <librtnl/mapper.h>
20#include <vnet/ip/ip.h>
21
22u32 handles[10];
23
24static void
25test_notify(void *obj, netns_type_t type, u32 flags, uword opaque) {
26  u32 index = (u32) opaque;
27  const char *action = (flags & NETNS_F_ADD)?"add":(flags & NETNS_F_DEL)?"del":"mod";
28
29  switch (type) {
30    case NETNS_TYPE_ADDR:
31      clib_warning("%d: addr %s %U", index, action, format_ns_addr, (ns_addr_t *)obj);
32      break;
33    case NETNS_TYPE_ROUTE:
34      clib_warning("%d: route %s %U", index, action, format_ns_route, (ns_route_t *)obj);
35      break;
36    case NETNS_TYPE_LINK:
37      clib_warning("%d:link %s %U", index, action, format_ns_link, (ns_link_t *)obj);
38      break;
39    case NETNS_TYPE_NEIGH:
40      clib_warning("%d: neigh %s %U", index, action, format_ns_neigh, (ns_neigh_t *)obj);
41      break;
42  }
43}
44
45static clib_error_t *
46test_enable_command_fn (vlib_main_t * vm,
47                        unformat_input_t * input,
48                        vlib_cli_command_t * cmd)
49{
50  char *nsname = 0;
51  u32 index;
52  if (!unformat(input, "%s", &nsname)) {
53    return clib_error_return(0, "unknown input `%U'",
54                             format_unformat_error, input);
55  }
56  if (!unformat(input, "%d", &index)) {
57    return clib_error_return(0, "unknown input `%U'",
58                             format_unformat_error, input);
59  }
60
61  if (!strcmp(nsname, "default"))
62    nsname[0] = 0;
63
64  netns_sub_t sub;
65  sub.notify = test_notify;
66  sub.opaque = index;
67  handles[index] = netns_open(nsname, &sub);
68  if (handles[index] == ~0) {
69    return clib_error_create("Could not open netns with name %s", nsname);
70  }
71  return 0;
72}
73
74static clib_error_t *
75test_disable_command_fn (vlib_main_t * vm,
76                         unformat_input_t * input,
77                         vlib_cli_command_t * cmd)
78{
79  u32 index;
80  if (!unformat(input, "%d", &index)) {
81    return clib_error_return(0, "unknown input `%U'",
82                             format_unformat_error, input);
83  }
84
85  netns_close(handles[index]);
86  return 0;
87}
88
89VLIB_CLI_COMMAND (rtnl_enable_command, static) = {
90    .path = "test netns enable",
91    .short_help = "test netns enable [<ns-name>|default] <index>",
92    .function = test_enable_command_fn,
93};
94
95VLIB_CLI_COMMAND (rtnl_disable_command, static) = {
96    .path = "test netns disable",
97    .short_help = "test rtnl disable <index>",
98    .function = test_disable_command_fn,
99};
100
101u32 mapper_indexes[10];
102
103static clib_error_t *
104mapper_ns_add_command_fn (vlib_main_t * vm,
105                         unformat_input_t * input,
106                         vlib_cli_command_t * cmd)
107{
108  u32 index;
109  char *nsname;
110  u32 table_id;
111  if (!unformat(input, "%d", &index))
112    return clib_error_return(0, "invalid index `%U'",
113                             format_unformat_error, input);
114  if (!unformat(input, "%s", &nsname))
115    return clib_error_return(0, "invalid nsname `%U'",
116                             format_unformat_error, input);
117  if (!unformat(input, "%d", &table_id))
118      return clib_error_return(0, "invalid fib index `%U'",
119                               format_unformat_error, input);
120
121  if (!strcmp(nsname, "default"))
122    nsname[0] = 0;
123
124  u32 fib4 = find_ip4_fib_by_table_index_or_id(&ip4_main, table_id, 0) - ip4_main.fibs;
125  u32 fib6 = find_ip6_fib_by_table_index_or_id(&ip6_main, table_id, 0) - ip6_main.fibs;
126
127  if (mapper_add_ns(nsname, fib4, fib6, &mapper_indexes[index]))
128    return clib_error_return(0, "Could not add ns %s", nsname);
129  return 0;
130}
131
132VLIB_CLI_COMMAND (mapper_ns_add_command, static) = {
133    .path = "test mapper ns add",
134    .short_help = "test mapper ns add <index> <nsname> <table-id>",
135    .function = mapper_ns_add_command_fn,
136};
137
138static clib_error_t *
139mapper_ns_del_command_fn (vlib_main_t * vm,
140                         unformat_input_t * input,
141                         vlib_cli_command_t * cmd)
142{
143  u32 index;
144  if (!unformat(input, "%d", &index))
145    return clib_error_return(0, "invalid index `%U'",
146                             format_unformat_error, input);
147
148  if (mapper_del_ns(mapper_indexes[index]))
149    return clib_error_return(0, "Could not del ns %d", index);
150  return 0;
151}
152
153VLIB_CLI_COMMAND (mapper_ns_del_command, static) = {
154    .path = "test mapper ns delete",
155    .short_help = "test mapper ns delete <index>",
156    .function = mapper_ns_del_command_fn,
157};
158
159static clib_error_t *
160mapper_iface_command_fn (vlib_main_t * vm,
161                         unformat_input_t * input,
162                         vlib_cli_command_t * cmd)
163{
164  u32 nsindex;
165  u32 ifindex;
166  u32 sw_if_index;
167  int del = 0;
168  if (!unformat(input, "%d", &nsindex))
169    return clib_error_return(0, "invalid nsindex `%U'",
170                             format_unformat_error, input);
171  if (!unformat(input, "%d", &ifindex))
172    return clib_error_return(0, "invalid ifindex `%U'",
173                             format_unformat_error, input);
174  if (!unformat(input, "%d", &sw_if_index))
175    return clib_error_return(0, "invalid sw_if_index `%U'",
176                             format_unformat_error, input);
177  if (unformat(input, "del"))
178    del = 1;
179
180  clib_warning("mapper_add_del %d %d %d %d", mapper_indexes[nsindex], ifindex, sw_if_index, del);
181
182  if (mapper_add_del(mapper_indexes[nsindex], ifindex, sw_if_index, del))
183    return clib_error_return(0, "Could not add iface");
184  return 0;
185}
186
187
188VLIB_CLI_COMMAND (mapper_iface_command, static) = {
189    .path = "test mapper iface",
190    .short_help = "test mapper iface <nsindex> <linux-ifindex> <sw_if_index> [del]",
191    .function = mapper_iface_command_fn,
192};
193
194clib_error_t *
195vlib_plugin_register (vlib_main_t * vm, vnet_plugin_handoff_t * h,
196                      int from_early_init)
197{
198  clib_warning("Loaded module");
199  return 0;
200}
201
202