FD.io VPP  v16.06
Vector Packet Processing
sixrd.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 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 "sixrd.h"
17 
18 /*
19  * This code supports the following sixrd modes:
20  *
21  * 32 EA bits (Complete IPv4 address is embedded):
22  * ea_bits_len = 32
23  * IPv4 suffix is embedded:
24  * ea_bits_len = < 32
25  * No embedded address bits (1:1 mode):
26  * ea_bits_len = 0
27  */
28 
29 int
31  u8 ip6_prefix_len,
32  ip4_address_t *ip4_prefix,
33  u8 ip4_prefix_len,
34  ip4_address_t *ip4_src,
35  u32 *sixrd_domain_index,
36  u16 mtu)
37 {
38  sixrd_main_t *mm = &sixrd_main;
39  ip4_main_t *im4 = &ip4_main;
40  ip6_main_t *im6 = &ip6_main;
41  sixrd_domain_t *d;
42  ip_adjacency_t adj;
45  u32 *p;
46 
47  /* Get domain index */
49  memset(d, 0, sizeof (*d));
50  *sixrd_domain_index = d - mm->domains;
51 
52  /* Init domain struct */
53  d->ip4_prefix.as_u32 = ip4_prefix->as_u32;
54  d->ip4_prefix_len = ip4_prefix_len;
55  d->ip6_prefix = *ip6_prefix;
56  d->ip6_prefix_len = ip6_prefix_len;
57  d->ip4_src = *ip4_src;
58  d->mtu = mtu;
59 
60  if (ip4_prefix_len < 32)
61  d->shift = 64 - ip6_prefix_len + (32 - ip4_prefix_len);
62 
63  /* Init IP adjacency */
64  memset(&adj, 0, sizeof(adj));
65  adj.explicit_fib_index = ~0;
67  p = (u32 *)&adj.rewrite_data[0];
68  *p = (u32) (*sixrd_domain_index);
69 
70  /* Create ip6 adjacency */
71  memset(&args6, 0, sizeof(args6));
72  args6.table_index_or_table_id = 0;
73  args6.flags = IP6_ROUTE_FLAG_ADD;
74  args6.dst_address.as_u64[0] = ip6_prefix->as_u64[0];
75  args6.dst_address.as_u64[1] = ip6_prefix->as_u64[1];
76  args6.dst_address_length = ip6_prefix_len;
77  args6.adj_index = ~0;
78  args6.add_adj = &adj;
79  args6.n_add_adj = 1;
80  ip6_add_del_route(im6, &args6);
81 
82  /* Multiple SIXRD domains may share same source IPv4 TEP */
83  uword *q = ip4_get_route(im4, 0, 0, (u8 *)ip4_src, 32);
84  if (q) {
85  u32 ai = q[0];
87  ip_adjacency_t *adj4 = ip_get_adjacency(lm4, ai);
89  clib_warning("BR source address already assigned: %U", format_ip4_address, ip4_src);
90  pool_put(mm->domains, d);
91  return -1;
92  }
93  /* Shared source */
94  p = (u32 *)&adj4->rewrite_data[0];
95  p[0] = ~0;
96 
97  /* Add refcount, so we don't accidentially delete the route underneath someone */
98  p[1]++;
99  } else {
100  /* Create ip4 adjacency. */
101  memset(&args4, 0, sizeof(args4));
102  args4.table_index_or_table_id = 0;
103  args4.flags = IP4_ROUTE_FLAG_ADD;
104  args4.dst_address.as_u32 = ip4_src->as_u32;
105  args4.dst_address_length = 32;
106  args4.adj_index = ~0;
107  args4.add_adj = &adj;
108  args4.n_add_adj = 1;
109  ip4_add_del_route(im4, &args4);
110  }
111 
112  return 0;
113 }
114 
115 /*
116  * sixrd_delete_domain
117  */
118 int
119 sixrd_delete_domain (u32 sixrd_domain_index)
120 {
121  sixrd_main_t *mm = &sixrd_main;
122  ip4_main_t *im4 = &ip4_main;
123  ip6_main_t *im6 = &ip6_main;
124  sixrd_domain_t *d;
125  ip_adjacency_t adj;
128 
129  if (pool_is_free_index(mm->domains, sixrd_domain_index)) {
130  clib_warning("SIXRD domain delete: domain does not exist: %d", sixrd_domain_index);
131  return -1;
132  }
133 
134  d = pool_elt_at_index(mm->domains, sixrd_domain_index);
135 
136  memset(&adj, 0, sizeof(adj));
137  adj.explicit_fib_index = ~0;
139 
140  /* Delete ip6 adjacency */
141  memset(&args6, 0, sizeof (args6));
142  args6.table_index_or_table_id = 0;
143  args6.flags = IP6_ROUTE_FLAG_DEL;
144  args6.dst_address.as_u64[0] = d->ip6_prefix.as_u64[0];
145  args6.dst_address.as_u64[1] = d->ip6_prefix.as_u64[1];
147  args6.adj_index = 0;
148  args6.add_adj = &adj;
149  args6.n_add_adj = 0;
150  ip6_add_del_route(im6, &args6);
151 
152  /* Delete ip4 adjacency */
153  uword *q = ip4_get_route(im4, 0, 0, (u8 *)&d->ip4_src, 32);
154  if (q) {
155  u32 ai = q[0];
157  ip_adjacency_t *adj4 = ip_get_adjacency(lm4, ai);
158 
159  u32 *p = (u32 *)&adj4->rewrite_data[0];
160  /* Delete route when no other domains use this source */
161  if (p[1] == 0) {
162  memset(&args4, 0, sizeof(args4));
163  args4.table_index_or_table_id = 0;
164  args4.flags = IP4_ROUTE_FLAG_DEL;
165  args4.dst_address.as_u32 = d->ip4_prefix.as_u32;
167  args4.adj_index = 0;
168  args4.add_adj = &adj;
169  args4.n_add_adj = 0;
170  ip4_add_del_route(im4, &args4);
171  }
172  p[1]--;
173  }
174 
175  pool_put(mm->domains, d);
176 
177  return 0;
178 }
179 
180 static clib_error_t *
182  unformat_input_t *input,
183  vlib_cli_command_t *cmd)
184 {
185  unformat_input_t _line_input, *line_input = &_line_input;
186  ip4_address_t ip4_prefix;
187  ip6_address_t ip6_prefix;
188  ip4_address_t ip4_src;
189  u32 ip6_prefix_len, ip4_prefix_len, sixrd_domain_index;
190  u32 num_m_args = 0;
191  /* Optional arguments */
192  u32 mtu = 0;
193 
194  /* Get a line of input. */
195  if (!unformat_user(input, unformat_line_input, line_input))
196  return 0;
197  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
198  if (unformat(line_input, "ip6-pfx %U/%d", unformat_ip6_address, &ip6_prefix, &ip6_prefix_len))
199  num_m_args++;
200  else if (unformat(line_input, "ip4-pfx %U/%d", unformat_ip4_address, &ip4_prefix, &ip4_prefix_len))
201  num_m_args++;
202  else if (unformat(line_input, "ip4-src %U", unformat_ip4_address, &ip4_src))
203  num_m_args++;
204  else if (unformat(line_input, "mtu %d", &mtu))
205  num_m_args++;
206  else
207  return clib_error_return(0, "unknown input `%U'",
208  format_unformat_error, input);
209  }
210  unformat_free(line_input);
211 
212  if (num_m_args < 3)
213  return clib_error_return(0, "mandatory argument(s) missing");
214 
215  sixrd_create_domain(&ip6_prefix, ip6_prefix_len, &ip4_prefix, ip4_prefix_len,
216  &ip4_src, &sixrd_domain_index, mtu);
217 
218  return 0;
219 }
220 
221 static clib_error_t *
223  unformat_input_t *input,
224  vlib_cli_command_t *cmd)
225 {
226  unformat_input_t _line_input, *line_input = &_line_input;
227  u32 num_m_args = 0;
228  u32 sixrd_domain_index;
229 
230  /* Get a line of input. */
231  if (! unformat_user(input, unformat_line_input, line_input))
232  return 0;
233 
234  while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
235  if (unformat(line_input, "index %d", &sixrd_domain_index))
236  num_m_args++;
237  else
238  return clib_error_return(0, "unknown input `%U'",
239  format_unformat_error, input);
240  }
241  unformat_free(line_input);
242 
243  if (num_m_args != 1)
244  return clib_error_return(0, "mandatory argument(s) missing");
245 
246  sixrd_delete_domain(sixrd_domain_index);
247 
248  return 0;
249 }
250 
251 static u8 *
252 format_sixrd_domain (u8 *s, va_list *args)
253 {
254  sixrd_domain_t *d = va_arg(*args, sixrd_domain_t *);
255  sixrd_main_t *mm = &sixrd_main;
256 
257  s = format(s,
258  "[%d] ip6-pfx %U/%d ip4-pfx %U/%d ip4-src %U mtu %d",
259  d - mm->domains,
262  format_ip4_address, &d->ip4_src, d->mtu);
263 
264  return s;
265 }
266 
267 static clib_error_t *
269 {
270  sixrd_main_t *mm = &sixrd_main;
271  sixrd_domain_t *d;
272 
273  if (pool_elts(mm->domains) == 0)
274  vlib_cli_output(vm, "No SIXRD domains are configured...");
275 
276  pool_foreach(d, mm->domains, ({vlib_cli_output(vm, "%U", format_sixrd_domain, d);}));
277 
278  return 0;
279 
280 }
281 
282 static clib_error_t *
284 {
285  sixrd_main_t *mm = &sixrd_main;
286  sixrd_domain_t *d;
287  int domains = 0, domaincount = 0;
288  if (pool_elts (mm->domains) == 0)
289  vlib_cli_output (vm, "No SIXRD domains are configured...");
290 
291  pool_foreach(d, mm->domains, ({
292  domains += sizeof(*d);
293  domaincount++;
294  }));
295 
296  vlib_cli_output(vm, "SIXRD domains structure: %d\n", sizeof (sixrd_domain_t));
297  vlib_cli_output(vm, "SIXRD domains: %d (%d bytes)\n", domaincount, domains);
298 
299  return 0;
300 }
301 
302 /*
303  * packet trace format function
304  */
305 u8 *
306 format_sixrd_trace (u8 *s, va_list *args)
307 {
308  CLIB_UNUSED(vlib_main_t *vm) = va_arg (*args, vlib_main_t *);
309  CLIB_UNUSED(vlib_node_t *node) = va_arg (*args, vlib_node_t *);
310  sixrd_trace_t *t = va_arg (*args, sixrd_trace_t *);
311  u32 sixrd_domain_index = t->sixrd_domain_index;
312 
313  s = format(s, "SIXRD domain index: %d", sixrd_domain_index);
314 
315  return s;
316 }
317 
318 VLIB_CLI_COMMAND(sixrd_add_domain_command, static) = {
319  .path = "sixrd add domain",
320  .short_help =
321  "sixrd add domain ip6-pfx <ip6-pfx> ip4-pfx <ip4-pfx> ip4-src <ip4-addr>",
322  .function = sixrd_add_domain_command_fn,
323 };
324 
325 VLIB_CLI_COMMAND(sixrd_del_command, static) = {
326  .path = "sixrd del domain",
327  .short_help =
328  "sixrd del domain index <domain>",
329  .function = sixrd_del_domain_command_fn,
330 };
331 
332 VLIB_CLI_COMMAND(show_sixrd_domain_command, static) = {
333  .path = "show sixrd domain",
334  .function = show_sixrd_domain_command_fn,
335 };
336 
337 VLIB_CLI_COMMAND(show_sixrd_stats_command, static) = {
338  .path = "show sixrd stats",
339  .function = show_sixrd_stats_command_fn,
340 };
341 
342 /*
343  * sixrd_init
344  */
346 {
347  sixrd_main_t *mm = &sixrd_main;
348 
349  mm->vnet_main = vnet_get_main();
350  mm->vlib_main = vm;
351 
352  return 0;
353 }
354 
ip4_address_t ip4_src
Definition: sixrd.h:30
vlib_main_t * vlib_main
Definition: sixrd.h:45
#define CLIB_UNUSED(x)
Definition: clib.h:79
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:942
format_function_t format_ip6_address
Definition: format.h:87
ip_lookup_next_t lookup_next_index
Definition: lookup.h:163
u64 as_u64[2]
Definition: ip6_packet.h:50
static clib_error_t * show_sixrd_stats_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: sixrd.c:283
always_inline void unformat_free(unformat_input_t *i)
Definition: format.h:160
#define UNFORMAT_END_OF_INPUT
Definition: format.h:142
static u8 * format_sixrd_domain(u8 *s, va_list *args)
Definition: sixrd.c:252
ip_lookup_main_t lookup_main
Definition: ip4.h:129
static clib_error_t * sixrd_add_domain_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: sixrd.c:181
format_function_t format_ip4_address
Definition: format.h:71
always_inline uword unformat_check_input(unformat_input_t *i)
Definition: format.h:168
u8 ip4_prefix_len
Definition: sixrd.h:32
vnet_main_t * vnet_get_main(void)
Definition: misc.c:45
#define pool_foreach(VAR, POOL, BODY)
Definition: pool.h:328
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:109
vnet_main_t * vnet_main
Definition: sixrd.h:46
always_inline uword pool_elts(void *v)
Definition: pool.h:97
#define clib_warning(format, args...)
Definition: error.h:59
u32 table_index_or_table_id
Definition: ip6.h:343
ip4_address_t ip4_prefix
Definition: sixrd.h:29
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:953
void ip6_add_del_route(ip6_main_t *im, ip6_add_del_route_args_t *args)
Definition: ip6_forward.c:208
unformat_function_t unformat_ip4_address
Definition: format.h:68
ip6_address_t dst_address
Definition: ip6.h:346
#define pool_elt_at_index(p, i)
Definition: pool.h:346
static clib_error_t * show_sixrd_domain_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: sixrd.c:268
void * ip4_get_route(ip4_main_t *im, u32 fib_index_or_table_id, u32 flags, u8 *address, u32 address_length)
Definition: ip4_forward.c:469
static clib_error_t * sixrd_del_domain_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: sixrd.c:222
ip_adjacency_t * add_adj
Definition: ip6.h:355
#define pool_put(P, E)
Definition: pool.h:200
sixrd_main_t sixrd_main
Definition: sixrd.h:79
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:538
unformat_function_t unformat_ip6_address
Definition: format.h:86
ip_adjacency_t * add_adj
Definition: ip4.h:293
#define pool_get_aligned(P, E, A)
Definition: pool.h:155
int sixrd_delete_domain(u32 sixrd_domain_index)
Definition: sixrd.c:119
#define IP6_ROUTE_FLAG_DEL
Definition: ip6.h:328
#define pool_is_free_index(P, I)
Definition: pool.h:197
#define IP4_ROUTE_FLAG_DEL
Definition: ip4.h:265
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:150
unsigned int u32
Definition: types.h:88
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:87
ip4_address_t dst_address
Definition: ip4.h:284
#define IP4_ROUTE_FLAG_ADD
Definition: ip4.h:264
ip6_main_t ip6_main
Definition: ip6_forward.c:2490
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:405
ip6_address_t ip6_prefix
Definition: sixrd.h:28
sixrd_domain_t * domains
Definition: sixrd.h:42
always_inline ip_adjacency_t * ip_get_adjacency(ip_lookup_main_t *lm, u32 adj_index)
Definition: lookup.h:423
u64 uword
Definition: types.h:112
u32 sixrd_domain_index
Definition: sixrd.h:76
clib_error_t * sixrd_init(vlib_main_t *vm)
Definition: sixrd.c:345
u8 * format_sixrd_trace(u8 *s, va_list *args)
Definition: sixrd.c:306
unsigned short u16
Definition: types.h:57
unsigned char u8
Definition: types.h:56
int sixrd_create_domain(ip6_address_t *ip6_prefix, u8 ip6_prefix_len, ip4_address_t *ip4_prefix, u8 ip4_prefix_len, ip4_address_t *ip4_src, u32 *sixrd_domain_index, u16 mtu)
Definition: sixrd.c:30
u32 table_index_or_table_id
Definition: ip4.h:281
u8 ip6_prefix_len
Definition: sixrd.h:31
ip4_main_t ip4_main
Definition: ip4_forward.c:1394
#define IP6_ROUTE_FLAG_ADD
Definition: ip6.h:327
i16 explicit_fib_index
Definition: lookup.h:168
#define clib_error_return(e, args...)
Definition: error.h:112
struct _unformat_input_t unformat_input_t
void ip4_add_del_route(ip4_main_t *im, ip4_add_del_route_args_t *args)
Definition: ip4_forward.c:196
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
unformat_function_t unformat_line_input
Definition: format.h:279