test.c revision fbaa56c3
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#ifdef find_ip4_fib_by_table_index_or_id
125  u32 fib4 = find_ip4_fib_by_table_index_or_id(&ip4_main, table_id, 0) - ip4_main.fibs;
126#else
127  u32 fib4 = fib_table_id_find_fib_index (FIB_PROTOCOL_IP4, table_id);
128#endif
129
130#ifdef find_ip6_fib_by_table_index_or_id
131  u32 fib6 = find_ip6_fib_by_table_index_or_id(&ip6_main, table_id, 0) - ip6_main.fibs;
132#else
133  u32 fib6 = fib_table_id_find_fib_index (FIB_PROTOCOL_IP6, table_id);
134#endif
135
136  if (mapper_add_ns(nsname, fib4, fib6, &mapper_indexes[index]))
137    return clib_error_return(0, "Could not add ns %s", nsname);
138  return 0;
139}
140
141VLIB_CLI_COMMAND (mapper_ns_add_command, static) = {
142    .path = "test mapper ns add",
143    .short_help = "test mapper ns add <index> <nsname> <table-id>",
144    .function = mapper_ns_add_command_fn,
145};
146
147static clib_error_t *
148mapper_ns_del_command_fn (vlib_main_t * vm,
149                         unformat_input_t * input,
150                         vlib_cli_command_t * cmd)
151{
152  u32 index;
153  if (!unformat(input, "%d", &index))
154    return clib_error_return(0, "invalid index `%U'",
155                             format_unformat_error, input);
156
157  if (mapper_del_ns(mapper_indexes[index]))
158    return clib_error_return(0, "Could not del ns %d", index);
159  return 0;
160}
161
162VLIB_CLI_COMMAND (mapper_ns_del_command, static) = {
163    .path = "test mapper ns delete",
164    .short_help = "test mapper ns delete <index>",
165    .function = mapper_ns_del_command_fn,
166};
167
168static clib_error_t *
169mapper_iface_command_fn (vlib_main_t * vm,
170                         unformat_input_t * input,
171                         vlib_cli_command_t * cmd)
172{
173  u32 nsindex;
174  u32 ifindex;
175  u32 sw_if_index;
176  int del = 0;
177  if (!unformat(input, "%d", &nsindex))
178    return clib_error_return(0, "invalid nsindex `%U'",
179                             format_unformat_error, input);
180  if (!unformat(input, "%d", &ifindex))
181    return clib_error_return(0, "invalid ifindex `%U'",
182                             format_unformat_error, input);
183  if (!unformat(input, "%d", &sw_if_index))
184    return clib_error_return(0, "invalid sw_if_index `%U'",
185                             format_unformat_error, input);
186  if (unformat(input, "del"))
187    del = 1;
188
189  clib_warning("mapper_add_del %d %d %d %d", mapper_indexes[nsindex], ifindex, sw_if_index, del);
190
191  if (mapper_add_del(mapper_indexes[nsindex], ifindex, sw_if_index, del))
192    return clib_error_return(0, "Could not add iface");
193  return 0;
194}
195
196
197VLIB_CLI_COMMAND (mapper_iface_command, static) = {
198    .path = "test mapper iface",
199    .short_help = "test mapper iface <nsindex> <linux-ifindex> <sw_if_index> [del]",
200    .function = mapper_iface_command_fn,
201};
202
203clib_error_t *
204vlib_plugin_register (vlib_main_t * vm, vnet_plugin_handoff_t * h,
205                      int from_early_init)
206{
207  clib_warning("Loaded module");
208  return 0;
209}
210
211