NAT: DS-Lite (VPP-1040)
[vpp.git] / src / plugins / nat / dslite.c
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 #include <nat/dslite.h>
16 #include <nat/dslite_dpo.h>
17 #include <vnet/fib/fib_table.h>
18
19 dslite_main_t dslite_main;
20
21 void
22 dslite_init (vlib_main_t * vm)
23 {
24   dslite_main_t *dm = &dslite_main;
25   vlib_thread_registration_t *tr;
26   vlib_thread_main_t *tm = vlib_get_thread_main ();
27   uword *p;
28   dslite_per_thread_data_t *td;
29   u32 translation_buckets = 1024;
30   u32 translation_memory_size = 128 << 20;
31   u32 b4_buckets = 128;
32   u32 b4_memory_size = 64 << 20;
33
34   dm->first_worker_index = 0;
35   dm->num_workers = 0;
36
37   p = hash_get_mem (tm->thread_registrations_by_name, "workers");
38   if (p)
39     {
40       tr = (vlib_thread_registration_t *) p[0];
41       if (tr)
42         {
43           dm->num_workers = tr->count;
44           dm->first_worker_index = tr->first_index;
45         }
46     }
47
48   if (dm->num_workers)
49     dm->port_per_thread = (0xffff - 1024) / dm->num_workers;
50   else
51     dm->port_per_thread = 0xffff - 1024;
52
53   vec_validate (dm->per_thread_data, tm->n_vlib_mains - 1);
54
55   /* *INDENT-OFF* */
56   vec_foreach (td, dm->per_thread_data)
57     {
58       clib_bihash_init_24_8 (&td->in2out, "in2out", translation_buckets,
59                              translation_memory_size);
60
61       clib_bihash_init_8_8 (&td->out2in, "out2in", translation_buckets,
62                             translation_memory_size);
63
64       clib_bihash_init_16_8 (&td->b4_hash, "b4s", b4_buckets, b4_memory_size);
65     }
66   /* *INDENT-ON* */
67
68   dslite_dpo_module_init ();
69 }
70
71 int
72 dslite_set_aftr_ip6_addr (dslite_main_t * dm, ip6_address_t * addr)
73 {
74   dpo_id_t dpo_v6 = DPO_INVALID;
75
76   dslite_dpo_create (DPO_PROTO_IP6, 0, &dpo_v6);
77   fib_prefix_t pfx = {
78     .fp_proto = FIB_PROTOCOL_IP6,
79     .fp_len = 128,
80     .fp_addr.ip6.as_u64[0] = addr->as_u64[0],
81     .fp_addr.ip6.as_u64[1] = addr->as_u64[1],
82   };
83   fib_table_entry_special_dpo_add (0, &pfx, FIB_SOURCE_PLUGIN_HI,
84                                    FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v6);
85   dpo_reset (&dpo_v6);
86
87   dm->aftr_ip6_addr.as_u64[0] = addr->as_u64[0];
88   dm->aftr_ip6_addr.as_u64[1] = addr->as_u64[1];
89   return 0;
90 }
91
92 int
93 dslite_add_del_pool_addr (dslite_main_t * dm, ip4_address_t * addr, u8 is_add)
94 {
95   vlib_thread_main_t *tm = vlib_get_thread_main ();
96   snat_address_t *a = 0;
97   int i = 0;
98   dpo_id_t dpo_v4 = DPO_INVALID;
99   fib_prefix_t pfx = {
100     .fp_proto = FIB_PROTOCOL_IP4,
101     .fp_len = 32,
102     .fp_addr.ip4.as_u32 = addr->as_u32,
103   };
104
105   for (i = 0; i < vec_len (dm->addr_pool); i++)
106     {
107       if (dm->addr_pool[i].addr.as_u32 == addr->as_u32)
108         {
109           a = dm->addr_pool + i;
110           break;
111         }
112     }
113   if (is_add)
114     {
115       if (a)
116         return VNET_API_ERROR_VALUE_EXIST;
117       vec_add2 (dm->addr_pool, a, 1);
118       a->addr = *addr;
119 #define _(N, i, n, s) \
120       clib_bitmap_alloc (a->busy_##n##_port_bitmap, 65535); \
121       a->busy_##n##_ports = 0; \
122       vec_validate_init_empty (a->busy_##n##_ports_per_thread, tm->n_vlib_mains - 1, 0);
123       foreach_snat_protocol
124 #undef _
125         dslite_dpo_create (DPO_PROTO_IP4, 0, &dpo_v4);
126       fib_table_entry_special_dpo_add (0, &pfx, FIB_SOURCE_PLUGIN_HI,
127                                        FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
128       dpo_reset (&dpo_v4);
129     }
130   else
131     {
132       if (!a)
133         return VNET_API_ERROR_NO_SUCH_ENTRY;
134 #define _(N, id, n, s) \
135       clib_bitmap_free (a->busy_##n##_port_bitmap); \
136       vec_free (a->busy_##n##_ports_per_thread);
137       foreach_snat_protocol
138 #undef _
139         fib_table_entry_special_remove (0, &pfx, FIB_SOURCE_PLUGIN_HI);
140       vec_del1 (dm->addr_pool, i);
141     }
142   return 0;
143 }
144
145 u8 *
146 format_dslite_trace (u8 * s, va_list * args)
147 {
148   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
149   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
150   dslite_trace_t *t = va_arg (*args, dslite_trace_t *);
151
152   s =
153     format (s, "next index %d, session %d", t->next_index, t->session_index);
154
155   return s;
156 }
157
158 /*
159  * fd.io coding-style-patch-verification: ON
160  *
161  * Local Variables:
162  * eval: (c-set-style "gnu")
163  * End:
164  */