stats: counters data model
[vpp.git] / src / plugins / map / map.h
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 #include <stdbool.h>
16 #include <vppinfra/error.h>
17 #include <vnet/vnet.h>
18 #include <vnet/ip/ip.h>
19 #include <vlib/vlib.h>
20 #include <vnet/fib/fib_types.h>
21 #include <vnet/fib/ip4_fib.h>
22 #include <vnet/adj/adj.h>
23 #include <vnet/dpo/load_balance.h>
24 #include "lpm.h"
25 #include <vppinfra/lock.h>
26 #include <map/map.api_enum.h>
27
28 #define MAP_SKIP_IP6_LOOKUP 1
29
30 #define MAP_ERR_GOOD                    0
31 #define MAP_ERR_BAD_POOL_SIZE           -1
32 #define MAP_ERR_BAD_HT_RATIO            -2
33 #define MAP_ERR_BAD_LIFETIME            -3
34 #define MAP_ERR_BAD_BUFFERS             -4
35 #define MAP_ERR_BAD_BUFFERS_TOO_LARGE   -5
36 #define MAP_ERR_UNSUPPORTED             -6
37
38 int map_create_domain (ip4_address_t * ip4_prefix, u8 ip4_prefix_len,
39                        ip6_address_t * ip6_prefix, u8 ip6_prefix_len,
40                        ip6_address_t * ip6_src, u8 ip6_src_len,
41                        u8 ea_bits_len, u8 psid_offset, u8 psid_length,
42                        u32 * map_domain_index, u16 mtu, u8 flags, u8 * tag);
43 int map_delete_domain (u32 map_domain_index);
44 int map_add_del_psid (u32 map_domain_index, u16 psid, ip6_address_t * tep,
45                       bool is_add);
46 int map_if_enable_disable (bool is_enable, u32 sw_if_index,
47                            bool is_translation);
48 u8 *format_map_trace (u8 * s, va_list * args);
49
50 int map_param_set_fragmentation (bool inner, bool ignore_df);
51 int map_param_set_icmp (ip4_address_t * ip4_err_relay_src);
52 int map_param_set_icmp6 (u8 enable_unreachable);
53 void map_pre_resolve (ip4_address_t * ip4, ip6_address_t * ip6, bool is_del);
54 int map_param_set_security_check (bool enable, bool fragments);
55 int map_param_set_traffic_class (bool copy, u8 tc);
56 int map_param_set_tcp (u16 tcp_mss);
57
58
59 typedef enum
60 {
61   MAP_DOMAIN_PREFIX = 1 << 0,
62   MAP_DOMAIN_TRANSLATION = 1 << 1,      // The domain uses MAP-T
63   MAP_DOMAIN_RFC6052 = 1 << 2,
64 } __attribute__ ((__packed__)) map_domain_flags_e;
65
66 //#define IP6_MAP_T_OVERRIDE_TOS 0
67
68 /*
69  * This structure _MUST_ be no larger than a single cache line (64 bytes).
70  * If more space is needed make a union of ip6_prefix and *rules, as
71  * those are mutually exclusive.
72  */
73 typedef struct
74 {
75   /* Required for pool_get_aligned */
76   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
77   ip6_address_t ip6_src;
78   ip6_address_t ip6_prefix;
79   ip6_address_t *rules;
80   u32 suffix_mask;
81   ip4_address_t ip4_prefix;
82   u16 psid_mask;
83   u16 mtu;
84   map_domain_flags_e flags;
85   u8 ip6_prefix_len;
86   u8 ip6_src_len;
87   u8 ea_bits_len;
88   u8 psid_offset;
89   u8 psid_length;
90
91   /* helpers */
92   u8 psid_shift;
93   u8 suffix_shift;
94   u8 ea_shift;
95
96   /* not used by forwarding */
97   u8 ip4_prefix_len;
98 } map_domain_t;
99
100 STATIC_ASSERT ((sizeof (map_domain_t) <= CLIB_CACHE_LINE_BYTES),
101                "MAP domain fits in one cacheline");
102
103 /*
104  * Extra data about a domain that doesn't need to be time/space critical.
105  * This structure is in a vector parallel to the main map_domain_t,
106  * and indexed by the same map-domain-index values.
107  */
108 typedef struct
109 {
110   u8 *tag;                      /* Probably a user-assigned domain name. */
111 } map_domain_extra_t;
112
113 #define MAP_REASS_INDEX_NONE ((u16)0xffff)
114
115 /*
116  * MAP domain counters
117  */
118 typedef enum
119 {
120   /* Simple counters */
121   MAP_DOMAIN_IPV4_FRAGMENT = 0,
122   /* Combined counters */
123   MAP_DOMAIN_COUNTER_RX = 0,
124   MAP_DOMAIN_COUNTER_TX,
125   MAP_N_DOMAIN_COUNTER
126 } map_domain_counter_t;
127
128 #ifdef MAP_SKIP_IP6_LOOKUP
129 /**
130  * A pre-resolved next-hop
131  */
132 typedef struct map_main_pre_resolved_t_
133 {
134   /**
135    * Linkage into the FIB graph
136    */
137   fib_node_t node;
138
139   /**
140    * The FIB entry index of the next-hop
141    */
142   fib_node_index_t fei;
143
144   /**
145    * This object sibling index on the FIB entry's child dependency list
146    */
147   u32 sibling;
148
149   /**
150    * The Load-balance object index to use to forward
151    */
152   dpo_id_t dpo;
153 } map_main_pre_resolved_t;
154
155 /**
156  * Pre-resolved next hops for v4 and v6. Why these are global and not
157  * per-domain is beyond me.
158  */
159 extern map_main_pre_resolved_t pre_resolved[FIB_PROTOCOL_MAX];
160 #endif
161
162 typedef struct
163 {
164   /* pool of MAP domains */
165   map_domain_t *domains;
166   map_domain_extra_t *domain_extras;
167
168   /* MAP Domain packet/byte counters indexed by map domain index */
169   vlib_simple_counter_main_t *simple_domain_counters;
170   vlib_combined_counter_main_t *domain_counters;
171   volatile u32 *counter_lock;
172
173   /* API message id base */
174   u16 msg_id_base;
175
176   /* Traffic class: zero, copy (~0) or fixed value */
177   u8 tc;
178   bool tc_copy;
179
180   bool sec_check;               /* Inbound security check */
181   bool sec_check_frag;          /* Inbound security check for (subsequent) fragments */
182   bool icmp6_enabled;           /* Send destination unreachable for security check failure */
183
184   u16 tcp_mss;                  /* TCP MSS clamp value */
185
186   /* ICMPv6 -> ICMPv4 relay parameters */
187   ip4_address_t icmp4_src_address;
188   vlib_simple_counter_main_t icmp_relayed;
189
190   /* convenience */
191   vlib_main_t *vlib_main;
192   vnet_main_t *vnet_main;
193
194   bool frag_inner;              /* Inner or outer fragmentation */
195   bool frag_ignore_df;          /* Fragment (outer) packet even if DF is set */
196
197   /* Graph node state */
198   uword *bm_trans_enabled_by_sw_if;
199   uword *bm_encap_enabled_by_sw_if;
200
201   /* Lookup tables */
202   lpm_t *ip4_prefix_tbl;
203   lpm_t *ip6_prefix_tbl;
204   lpm_t *ip6_src_prefix_tbl;
205
206   uword ip4_sv_reass_custom_next_index;
207 } map_main_t;
208
209 typedef vl_counter_map_enum_t map_error_t;
210 u64 map_error_counter_get (u32 node_index, map_error_t map_error);
211
212 typedef struct
213 {
214   u32 map_domain_index;
215   u16 port;
216 } map_trace_t;
217
218 always_inline void
219 map_add_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
220                vlib_buffer_t * b, u32 map_domain_index, u16 port)
221 {
222   map_trace_t *tr = vlib_add_trace (vm, node, b, sizeof (*tr));
223   tr->map_domain_index = map_domain_index;
224   tr->port = port;
225 }
226
227 extern map_main_t map_main;
228
229 extern vlib_node_registration_t ip4_map_node;
230 extern vlib_node_registration_t ip6_map_node;
231
232 extern vlib_node_registration_t ip4_map_t_node;
233 extern vlib_node_registration_t ip4_map_t_fragmented_node;
234 extern vlib_node_registration_t ip4_map_t_tcp_udp_node;
235 extern vlib_node_registration_t ip4_map_t_icmp_node;
236
237 extern vlib_node_registration_t ip6_map_t_node;
238 extern vlib_node_registration_t ip6_map_t_fragmented_node;
239 extern vlib_node_registration_t ip6_map_t_tcp_udp_node;
240 extern vlib_node_registration_t ip6_map_t_icmp_node;
241
242 /*
243  * map_get_pfx
244  */
245 static_always_inline u64
246 map_get_pfx (map_domain_t * d, u32 addr, u16 port)
247 {
248   u16 psid = (port >> d->psid_shift) & d->psid_mask;
249
250   if (d->ea_bits_len == 0 && d->rules)
251     return clib_net_to_host_u64 (d->rules[psid].as_u64[0]);
252
253   u32 suffix = (addr >> d->suffix_shift) & d->suffix_mask;
254   u64 ea =
255     d->ea_bits_len == 0 ? 0 : (((u64) suffix << d->psid_length)) | psid;
256
257   return clib_net_to_host_u64 (d->ip6_prefix.as_u64[0]) | ea << d->ea_shift;
258 }
259
260 static_always_inline u64
261 map_get_pfx_net (map_domain_t * d, u32 addr, u16 port)
262 {
263   return clib_host_to_net_u64 (map_get_pfx (d, clib_net_to_host_u32 (addr),
264                                             clib_net_to_host_u16 (port)));
265 }
266
267 /*
268  * map_get_sfx
269  */
270 static_always_inline u64
271 map_get_sfx (map_domain_t * d, u32 addr, u16 port)
272 {
273   u16 psid = (port >> d->psid_shift) & d->psid_mask;
274
275   /* Shared 1:1 mode. */
276   if (d->ea_bits_len == 0 && d->rules)
277     return clib_net_to_host_u64 (d->rules[psid].as_u64[1]);
278   if (d->ip6_prefix_len == 128)
279     return clib_net_to_host_u64 (d->ip6_prefix.as_u64[1]);
280
281   if (d->ip6_src_len == 96)
282     return (clib_net_to_host_u64 (d->ip6_prefix.as_u64[1]) | addr);
283
284   /* IPv4 prefix */
285   if (d->flags & MAP_DOMAIN_PREFIX)
286     return (u64) (addr & (0xFFFFFFFF << d->suffix_shift)) << 16;
287
288   /* Shared or full IPv4 address */
289   return ((u64) addr << 16) | psid;
290 }
291
292 static_always_inline u64
293 map_get_sfx_net (map_domain_t * d, u32 addr, u16 port)
294 {
295   return clib_host_to_net_u64 (map_get_sfx (d, clib_net_to_host_u32 (addr),
296                                             clib_net_to_host_u16 (port)));
297 }
298
299 static_always_inline u32
300 map_get_ip4 (ip6_address_t * addr, u16 prefix_len)
301 {
302   ASSERT (prefix_len == 64 || prefix_len == 96);
303   if (prefix_len == 96)
304     return clib_host_to_net_u32 (clib_net_to_host_u64 (addr->as_u64[1]));
305   else
306     return clib_host_to_net_u32 (clib_net_to_host_u64 (addr->as_u64[1]) >>
307                                  16);
308 }
309
310 static_always_inline map_domain_t *
311 ip4_map_get_domain (ip4_address_t * addr, u32 * map_domain_index, u8 * error)
312 {
313   map_main_t *mm = &map_main;
314
315   u32 mdi = mm->ip4_prefix_tbl->lookup (mm->ip4_prefix_tbl, addr, 32);
316   if (mdi == ~0)
317     {
318       *error = MAP_ERROR_NO_DOMAIN;
319       return 0;
320     }
321   *map_domain_index = mdi;
322   return pool_elt_at_index (mm->domains, mdi);
323 }
324
325 /*
326  * Get the MAP domain from an IPv6 address.
327  * If the IPv6 address or
328  * prefix is shared the IPv4 address must be used.
329  */
330 static_always_inline map_domain_t *
331 ip6_map_get_domain (ip6_address_t * addr, u32 * map_domain_index, u8 * error)
332 {
333   map_main_t *mm = &map_main;
334   u32 mdi =
335     mm->ip6_src_prefix_tbl->lookup (mm->ip6_src_prefix_tbl, addr, 128);
336   if (mdi == ~0)
337     {
338       *error = MAP_ERROR_NO_DOMAIN;
339       return 0;
340     }
341
342   *map_domain_index = mdi;
343   return pool_elt_at_index (mm->domains, mdi);
344 }
345
346 clib_error_t *map_plugin_api_hookup (vlib_main_t * vm);
347
348 void map_ip6_drop_pi (u32 pi);
349
350 /*
351  * Supports prefix of 96 or 64 (with u-octet)
352  */
353 static_always_inline void
354 ip4_map_t_embedded_address (map_domain_t * d,
355                             ip6_address_t * ip6, const ip4_address_t * ip4)
356 {
357   ASSERT (d->ip6_src_len == 96 || d->ip6_src_len == 64);        //No support for other lengths for now
358   u8 offset = d->ip6_src_len == 64 ? 9 : 12;
359   ip6->as_u64[0] = d->ip6_src.as_u64[0];
360   ip6->as_u64[1] = d->ip6_src.as_u64[1];
361   clib_memcpy_fast (&ip6->as_u8[offset], ip4, 4);
362 }
363
364 static_always_inline u32
365 ip6_map_t_embedded_address (map_domain_t * d, ip6_address_t * addr)
366 {
367   ASSERT (d->ip6_src_len == 64 || d->ip6_src_len == 96);
368   u32 x;
369   u8 offset = d->ip6_src_len == 64 ? 9 : 12;
370   clib_memcpy (&x, &addr->as_u8[offset], 4);
371   return x;
372 }
373
374 static inline void
375 map_domain_counter_lock (map_main_t * mm)
376 {
377   if (mm->counter_lock)
378     while (clib_atomic_test_and_set (mm->counter_lock))
379       /* zzzz */ ;
380 }
381
382 static inline void
383 map_domain_counter_unlock (map_main_t * mm)
384 {
385   if (mm->counter_lock)
386     clib_atomic_release (mm->counter_lock);
387 }
388
389
390 static_always_inline void
391 map_send_all_to_node (vlib_main_t * vm, u32 * pi_vector,
392                       vlib_node_runtime_t * node, vlib_error_t * error,
393                       u32 next)
394 {
395   u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
396   //Deal with fragments that are ready
397   from = pi_vector;
398   n_left_from = vec_len (pi_vector);
399   next_index = node->cached_next_index;
400   while (n_left_from > 0)
401     {
402       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
403       while (n_left_from > 0 && n_left_to_next > 0)
404         {
405           u32 pi0 = to_next[0] = from[0];
406           from += 1;
407           n_left_from -= 1;
408           to_next += 1;
409           n_left_to_next -= 1;
410           vlib_buffer_t *p0 = vlib_get_buffer (vm, pi0);
411           p0->error = *error;
412           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
413                                            n_left_to_next, pi0, next);
414         }
415       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
416     }
417 }
418
419 static_always_inline void
420 map_mss_clamping (tcp_header_t * tcp, ip_csum_t * sum, u16 mss_clamping)
421 {
422   u8 *data;
423   u8 opt_len, opts_len, kind;
424   u16 mss;
425   u16 mss_value_net = clib_host_to_net_u16 (mss_clamping);
426
427   if (!tcp_syn (tcp))
428     return;
429
430   opts_len = (tcp_doff (tcp) << 2) - sizeof (tcp_header_t);
431   data = (u8 *) (tcp + 1);
432   for (; opts_len > 0; opts_len -= opt_len, data += opt_len)
433     {
434       kind = data[0];
435
436       if (kind == TCP_OPTION_EOL)
437         break;
438       else if (kind == TCP_OPTION_NOOP)
439         {
440           opt_len = 1;
441           continue;
442         }
443       else
444         {
445           if (opts_len < 2)
446             return;
447           opt_len = data[1];
448
449           if (opt_len < 2 || opt_len > opts_len)
450             return;
451         }
452
453       if (kind == TCP_OPTION_MSS)
454         {
455           mss = *(u16 *) (data + 2);
456           if (clib_net_to_host_u16 (mss) > mss_clamping)
457             {
458               *sum =
459                 ip_csum_update (*sum, mss, mss_value_net, ip4_header_t,
460                                 length);
461               clib_memcpy (data + 2, &mss_value_net, 2);
462             }
463           return;
464         }
465     }
466 }
467
468 static_always_inline bool
469 ip4_map_ip6_lookup_bypass (vlib_buffer_t * p0, ip4_header_t * ip)
470 {
471 #ifdef MAP_SKIP_IP6_LOOKUP
472   if (FIB_NODE_INDEX_INVALID != pre_resolved[FIB_PROTOCOL_IP6].fei)
473     {
474       vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
475         pre_resolved[FIB_PROTOCOL_IP6].dpo.dpoi_index;
476       return (true);
477     }
478 #endif
479   return (false);
480 }
481
482 static_always_inline bool
483 ip6_map_ip4_lookup_bypass (vlib_buffer_t * p0, ip4_header_t * ip)
484 {
485 #ifdef MAP_SKIP_IP6_LOOKUP
486   if (FIB_NODE_INDEX_INVALID != pre_resolved[FIB_PROTOCOL_IP4].fei)
487     {
488       vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
489         pre_resolved[FIB_PROTOCOL_IP4].dpo.dpoi_index;
490       return (true);
491     }
492 #endif
493   return (false);
494 }
495
496 /*
497  * fd.io coding-style-patch-verification: ON
498  *
499  * Local Variables:
500  * eval: (c-set-style "gnu")
501  * End:
502  */