socket_test.sh: Don't hard code debug image with gdb.
[vpp.git] / src / plugins / sixrd / sixrd.h
1 /*---------------------------------------------------------------------------
2  * Copyright (c) 2009-2014 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 <stdbool.h>
17 #include <vnet/fib/ip6_fib.h>
18 #include <vnet/ip/ip.h>
19 #include <vnet/vnet.h>
20 #include <vppinfra/error.h>
21
22 #define SIXRD_DEFAULT_MTU 1480  /* 1500 - IPv4 header */
23
24 typedef struct
25 {
26   u32 fib_index;
27   u32 hw_if_index;
28   u32 sw_if_index;
29   u32 tunnel_index;
30   ip6_address_t ip6_prefix;
31   ip4_address_t ip4_prefix;
32   ip4_address_t ip4_src;
33   u8 ip6_prefix_len;
34   u8 ip4_prefix_len;
35
36   /* helpers */
37   u8 shift;
38
39   u16 mtu;
40   bool security_check;
41 } sixrd_tunnel_t;
42
43 typedef struct
44 {
45   u16 msg_id_base;
46
47   /* pool of SIXRD domains */
48   sixrd_tunnel_t *tunnels;
49   u32 *tunnel_index_by_sw_if_index;
50   uword *tunnel_by_ip;
51
52 } sixrd_main_t;
53
54 #define foreach_sixrd_error                                                    \
55   /* Must be first. */                                                         \
56   _(NONE, "valid SIXRD packets")                                               \
57   _(BAD_PROTOCOL, "bad protocol")                                              \
58   _(SEC_CHECK, "security check failed")                                        \
59   _(NO_TUNNEL, "no tunnel")
60
61
62 typedef enum
63 {
64 #define _(sym, str) SIXRD_ERROR_##sym,
65   foreach_sixrd_error
66 #undef _
67     SIXRD_N_ERROR,
68 } sixrd_error_t;
69
70 extern sixrd_main_t sixrd_main;
71
72 /*
73  * sixrd_get_addr
74  */
75 static_always_inline u32
76 sixrd_get_addr_net (const sixrd_tunnel_t * t, u64 dal)
77 {
78   /* 1:1 mode */
79   if (t->ip4_prefix_len == 32)
80     return (t->ip4_prefix.as_u32);
81
82   dal = clib_net_to_host_u64 (dal);
83
84   /* Grab 32 - ip4_prefix_len bits out of IPv6 address from offset
85    * ip6_prefix_len */
86   u32 mask = ~(~0ULL << (32 - t->ip4_prefix_len));
87   u32 ip4 =
88     clib_net_to_host_u32 (t->
89                           ip4_prefix.as_u32) | ((u32) (dal >> t->
90                                                        shift) & mask);
91   return clib_host_to_net_u32 (ip4);
92 }
93
94 static_always_inline sixrd_tunnel_t *
95 find_tunnel_by_ip4_address (ip4_address_t * ip)
96 {
97   sixrd_main_t *sm = &sixrd_main;
98   uword *p;
99   p = hash_get (sm->tunnel_by_ip, ip->as_u32);
100   if (!p)
101     return NULL;
102   return pool_elt_at_index (sm->tunnels, p[0]);
103 }
104
105 static_always_inline sixrd_tunnel_t *
106 ip4_sixrd_get_tunnel (u32 sdi, ip4_address_t * addr, u8 * error)
107 {
108   sixrd_tunnel_t *t = find_tunnel_by_ip4_address (addr);
109   if (!t)
110     {
111       *error = SIXRD_ERROR_NO_TUNNEL;
112       return NULL;
113     }
114   return t;
115 }
116
117 /*
118  * fd.io coding-style-patch-verification: ON
119  *
120  * Local Variables:
121  * eval: (c-set-style "gnu")
122  * End:
123  */