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