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