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