Add basic support for DS-Lite CE (VPP-1059)
[vpp.git] / src / plugins / nat / dslite.h
1 /*
2  * Copyright (c) 2017 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 #ifndef __included_dslite_h__
16 #define __included_dslite_h__
17
18 #include <vppinfra/bihash_8_8.h>
19 #include <vppinfra/bihash_16_8.h>
20 #include <vppinfra/bihash_24_8.h>
21 #include <nat/nat.h>
22
23 typedef struct
24 {
25   union
26   {
27     struct
28     {
29       ip6_address_t softwire_id;
30       ip4_address_t addr;
31       u16 port;
32       u8 proto;
33       u8 pad;
34     };
35     u64 as_u64[3];
36   };
37 } dslite_session_key_t;
38
39 /* *INDENT-OFF* */
40 typedef CLIB_PACKED (struct
41 {
42   snat_session_key_t out2in;
43   dslite_session_key_t in2out;
44   u32 per_b4_index;
45   u32 per_b4_list_head_index;
46   f64 last_heard;
47   u64 total_bytes;
48   u32 total_pkts;
49   u32 outside_address_index;
50 }) dslite_session_t;
51 /* *INDENT-ON* */
52
53 typedef struct
54 {
55   ip6_address_t addr;
56   u32 sessions_per_b4_list_head_index;
57   u32 nsessions;
58 } dslite_b4_t;
59
60 typedef struct
61 {
62   /* Main lookup tables */
63   clib_bihash_8_8_t out2in;
64   clib_bihash_24_8_t in2out;
65
66   /* Find a B4 */
67   clib_bihash_16_8_t b4_hash;
68
69   /* B4 pool */
70   dslite_b4_t *b4s;
71
72   /* Session pool */
73   dslite_session_t *sessions;
74
75   /* Pool of doubly-linked list elements */
76   dlist_elt_t *list_pool;
77 } dslite_per_thread_data_t;
78
79 typedef struct
80 {
81   ip6_address_t aftr_ip6_addr;
82   ip4_address_t aftr_ip4_addr;
83   ip6_address_t b4_ip6_addr;
84   ip4_address_t b4_ip4_addr;
85   dslite_per_thread_data_t *per_thread_data;
86   snat_address_t *addr_pool;
87   u32 num_workers;
88   u32 first_worker_index;
89   u16 port_per_thread;
90
91   /* If set then the DSLite component behaves as CPE/B4
92    * otherwise it behaves as AFTR */
93   u8 is_ce;
94 } dslite_main_t;
95
96 typedef struct
97 {
98   u32 next_index;
99   u32 session_index;
100 } dslite_trace_t;
101
102 typedef struct
103 {
104   u32 next_index;
105 } dslite_ce_trace_t;
106
107 #define foreach_dslite_error                    \
108 _(IN2OUT, "valid in2out DS-Lite packets")       \
109 _(OUT2IN, "valid out2in DS-Lite packets")       \
110 _(CE_ENCAP, "valid CE encap DS-Lite packets")   \
111 _(CE_DECAP, "valid CE decap DS-Lite packets")   \
112 _(NO_TRANSLATION, "no translation")             \
113 _(BAD_IP6_PROTOCOL, "bad ip6 protocol")         \
114 _(OUT_OF_PORTS, "out of ports")                 \
115 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
116 _(BAD_ICMP_TYPE, "unsupported icmp type")       \
117 _(UNKNOWN, "unknown")
118
119 typedef enum
120 {
121 #define _(sym,str) DSLITE_ERROR_##sym,
122   foreach_dslite_error
123 #undef _
124     DSLITE_N_ERROR,
125 } dslite_error_t;
126
127 extern dslite_main_t dslite_main;
128 extern vlib_node_registration_t dslite_in2out_node;
129 extern vlib_node_registration_t dslite_in2out_slowpath_node;
130 extern vlib_node_registration_t dslite_out2in_node;
131 extern vlib_node_registration_t dslite_ce_encap_node;
132 extern vlib_node_registration_t dslite_ce_decap_node;
133
134 void dslite_init (vlib_main_t * vm);
135 void dslite_set_ce (dslite_main_t * dm, u8 set);
136 int dslite_set_aftr_ip6_addr (dslite_main_t * dm, ip6_address_t * addr);
137 int dslite_set_b4_ip6_addr (dslite_main_t * dm, ip6_address_t * addr);
138 int dslite_set_aftr_ip4_addr (dslite_main_t * dm, ip4_address_t * addr);
139 int dslite_set_b4_ip4_addr (dslite_main_t * dm, ip4_address_t * addr);
140 int dslite_add_del_pool_addr (dslite_main_t * dm, ip4_address_t * addr,
141                               u8 is_add);
142 u8 *format_dslite_trace (u8 * s, va_list * args);
143 u8 *format_dslite_ce_trace (u8 * s, va_list * args);
144
145 #endif /* __included_dslite_h__ */
146
147 /*
148  * fd.io coding-style-patch-verification: ON
149  *
150  * Local Variables:
151  * eval: (c-set-style "gnu")
152  * End:
153  */