cnat: Fix backend LB
[vpp.git] / src / plugins / cnat / cnat_node_vip.c
1 /*
2  * Copyright (c) 2020 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 <vlibmemory/api.h>
17 #include <cnat/cnat_node.h>
18 #include <cnat/cnat_translation.h>
19 #include <cnat/cnat_inline.h>
20 #include <cnat/cnat_src_policy.h>
21
22 #include <vnet/dpo/load_balance.h>
23 #include <vnet/dpo/load_balance_map.h>
24
25 typedef struct cnat_translation_trace_t_
26 {
27   cnat_session_t session;
28   cnat_translation_t tr;
29   u32 found_session;
30   u32 created_session;
31   u32 has_tr;
32 } cnat_translation_trace_t;
33
34 typedef enum cnat_translation_next_t_
35 {
36   CNAT_TRANSLATION_NEXT_DROP,
37   CNAT_TRANSLATION_NEXT_LOOKUP,
38   CNAT_TRANSLATION_N_NEXT,
39 } cnat_translation_next_t;
40
41 vlib_node_registration_t cnat_vip_ip4_node;
42 vlib_node_registration_t cnat_vip_ip6_node;
43
44 static u8 *
45 format_cnat_translation_trace (u8 * s, va_list * args)
46 {
47   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
48   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
49   cnat_translation_trace_t *t = va_arg (*args, cnat_translation_trace_t *);
50
51   if (t->found_session)
52     s = format (s, "found: %U", format_cnat_session, &t->session, 1);
53   else if (t->created_session)
54     s = format (s, "created: %U\n  tr: %U",
55                 format_cnat_session, &t->session, 1,
56                 format_cnat_translation, &t->tr, 0);
57   else if (t->has_tr)
58     s = format (s, "tr pass: %U", format_cnat_translation, &t->tr, 0);
59   else
60     s = format (s, "not found");
61   return s;
62 }
63
64 /* CNat sub for NAT behind a fib entry (VIP or interposed real IP) */
65 always_inline uword
66 cnat_vip_inline (vlib_main_t * vm,
67                  vlib_node_runtime_t * node,
68                  vlib_buffer_t * b,
69                  cnat_node_ctx_t * ctx, int rv, cnat_session_t * session)
70 {
71   vlib_combined_counter_main_t *cntm = &cnat_translation_counters;
72   const cnat_translation_t *ct = NULL;
73   ip4_header_t *ip4 = NULL;
74   ip_protocol_t iproto;
75   ip6_header_t *ip6 = NULL;
76   udp_header_t *udp0;
77   cnat_client_t *cc;
78   u16 next0;
79   index_t cti;
80   int created_session = 0;
81   cnat_src_policy_main_t *cspm = &cnat_src_policy_main;
82   if (AF_IP4 == ctx->af)
83     {
84       ip4 = vlib_buffer_get_current (b);
85       iproto = ip4->protocol;
86       udp0 = (udp_header_t *) (ip4 + 1);
87     }
88   else
89     {
90       ip6 = vlib_buffer_get_current (b);
91       iproto = ip6->protocol;
92       udp0 = (udp_header_t *) (ip6 + 1);
93     }
94
95   cc = cnat_client_get (vnet_buffer (b)->ip.adj_index[VLIB_TX]);
96
97   if (iproto != IP_PROTOCOL_UDP && iproto != IP_PROTOCOL_TCP
98       && iproto != IP_PROTOCOL_ICMP && iproto != IP_PROTOCOL_ICMP6)
99     {
100       /* Dont translate & follow the fib programming */
101       next0 = cc->cc_parent.dpoi_next_node;
102       vnet_buffer (b)->ip.adj_index[VLIB_TX] = cc->cc_parent.dpoi_index;
103       goto trace;
104     }
105
106   ct = cnat_find_translation (cc->parent_cci,
107                               clib_host_to_net_u16 (udp0->dst_port), iproto);
108
109   if (!rv)
110     {
111       /* session table hit */
112       cnat_timestamp_update (session->value.cs_ts_index, ctx->now);
113
114       if (NULL != ct)
115         {
116           /* Translate & follow the translation given LB */
117           next0 = ct->ct_lb.dpoi_next_node;
118           vnet_buffer (b)->ip.adj_index[VLIB_TX] = session->value.cs_lbi;
119         }
120       else if (session->value.flags & CNAT_SESSION_FLAG_HAS_SNAT)
121         {
122           /* The return needs DNAT, so we need an additionnal
123            * lookup after translation */
124           next0 = CNAT_TRANSLATION_NEXT_LOOKUP;
125         }
126       else
127         {
128           /* Translate & follow the fib programming */
129           next0 = cc->cc_parent.dpoi_next_node;
130           vnet_buffer (b)->ip.adj_index[VLIB_TX] = cc->cc_parent.dpoi_index;
131         }
132     }
133   else
134     {
135       if (NULL == ct)
136         {
137           /* Dont translate & Follow the fib programming */
138           vnet_buffer (b)->ip.adj_index[VLIB_TX] = cc->cc_parent.dpoi_index;
139           next0 = cc->cc_parent.dpoi_next_node;
140           goto trace;
141         }
142
143       /* New flow, create the sessions */
144       const load_balance_t *lb0;
145       cnat_ep_trk_t *trk0;
146       u32 hash_c0, bucket0;
147       u32 rsession_flags = 0;
148       const dpo_id_t *dpo0;
149
150       lb0 = load_balance_get (ct->ct_lb.dpoi_index);
151       if (!lb0->lb_n_buckets)
152         {
153           /* Dont translate & Follow the fib programming */
154           vnet_buffer (b)->ip.adj_index[VLIB_TX] = cc->cc_parent.dpoi_index;
155           next0 = cc->cc_parent.dpoi_next_node;
156           goto trace;
157         }
158
159       /* session table miss */
160       hash_c0 = (AF_IP4 == ctx->af ?
161                  ip4_compute_flow_hash (ip4, lb0->lb_hash_config) :
162                  ip6_compute_flow_hash (ip6, lb0->lb_hash_config));
163       bucket0 = hash_c0 % lb0->lb_n_buckets;
164       dpo0 = load_balance_get_fwd_bucket (lb0, bucket0);
165
166       /* add the session */
167       trk0 = &ct->ct_paths[bucket0];
168
169       ip46_address_copy (&session->value.cs_ip[VLIB_TX],
170                          &trk0->ct_ep[VLIB_TX].ce_ip.ip);
171       if (ip_address_is_zero (&trk0->ct_ep[VLIB_RX].ce_ip))
172         {
173           if (AF_IP4 == ctx->af)
174             ip46_address_set_ip4 (&session->value.cs_ip[VLIB_RX],
175                                   &ip4->src_address);
176           else
177             ip46_address_set_ip6 (&session->value.cs_ip[VLIB_RX],
178                                   &ip6->src_address);
179         }
180       else
181         {
182           /* We source NAT with the translation */
183           rsession_flags |= CNAT_SESSION_FLAG_HAS_SNAT;
184           ip46_address_copy (&session->value.cs_ip[VLIB_RX],
185                              &trk0->ct_ep[VLIB_RX].ce_ip.ip);
186         }
187       session->value.cs_port[VLIB_TX] =
188         clib_host_to_net_u16 (trk0->ct_ep[VLIB_TX].ce_port);
189       session->value.cs_port[VLIB_RX] =
190         clib_host_to_net_u16 (trk0->ct_ep[VLIB_RX].ce_port);
191
192       session->value.flags = 0;
193       session->value.cs_lbi = dpo0->dpoi_index;
194
195       rv = cspm->vip_policy (vm, b, session, &rsession_flags, ct, ctx);
196       if (CNAT_SOURCE_ERROR_USE_DEFAULT == rv)
197         rv = cspm->default_policy (vm, b, session, &rsession_flags, ct, ctx);
198       if (rv)
199         {
200           if (CNAT_SOURCE_ERROR_EXHAUSTED_PORTS == rv)
201             vlib_node_increment_counter (vm, cnat_vip_ip4_node.index,
202                                          CNAT_ERROR_EXHAUSTED_PORTS, 1);
203           next0 = CNAT_TRANSLATION_NEXT_DROP;
204           goto trace;
205         }
206
207       /* refcnt session in current client */
208       cnat_client_cnt_session (cc);
209       cnat_session_create (session, ctx, rsession_flags);
210       created_session = 1;
211
212       next0 = ct->ct_lb.dpoi_next_node;
213       vnet_buffer (b)->ip.adj_index[VLIB_TX] = session->value.cs_lbi;
214     }
215
216   if (AF_IP4 == ctx->af)
217     cnat_translation_ip4 (session, ip4, udp0);
218   else
219     cnat_translation_ip6 (session, ip6, udp0);
220
221   if (NULL != ct)
222     {
223       cti = ct - cnat_translation_pool;
224       vlib_increment_combined_counter (cntm, ctx->thread_index, cti, 1,
225                                        vlib_buffer_length_in_chain (vm, b));
226     }
227
228 trace:
229   if (PREDICT_FALSE (ctx->do_trace))
230     {
231       cnat_translation_trace_t *t;
232
233       t = vlib_add_trace (vm, node, b, sizeof (*t));
234
235       t->found_session = !rv;
236       t->created_session = created_session;
237       if (t->found_session || t->created_session)
238         clib_memcpy (&t->session, session, sizeof (t->session));
239       t->has_tr = (NULL != ct);
240       if (t->has_tr)
241         clib_memcpy (&t->tr, ct, sizeof (cnat_translation_t));
242     }
243   return next0;
244 }
245
246 VLIB_NODE_FN (cnat_vip_ip4_node) (vlib_main_t * vm,
247                                   vlib_node_runtime_t * node,
248                                   vlib_frame_t * frame)
249 {
250   if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
251     return cnat_node_inline (vm, node, frame, cnat_vip_inline, AF_IP4,
252                              1 /* do_trace */ );
253   return cnat_node_inline (vm, node, frame, cnat_vip_inline, AF_IP4,
254                            0 /* do_trace */ );
255 }
256
257 VLIB_NODE_FN (cnat_vip_ip6_node) (vlib_main_t * vm,
258                                   vlib_node_runtime_t * node,
259                                   vlib_frame_t * frame)
260 {
261   if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
262     return cnat_node_inline (vm, node, frame, cnat_vip_inline, AF_IP6,
263                              1 /* do_trace */ );
264   return cnat_node_inline (vm, node, frame, cnat_vip_inline, AF_IP6,
265                            0 /* do_trace */ );
266 }
267
268 /* *INDENT-OFF* */
269 VLIB_REGISTER_NODE (cnat_vip_ip4_node) =
270 {
271   .name = "ip4-cnat-tx",
272   .vector_size = sizeof (u32),
273   .format_trace = format_cnat_translation_trace,
274   .type = VLIB_NODE_TYPE_INTERNAL,
275   .n_errors = 0,
276   .n_next_nodes = CNAT_TRANSLATION_N_NEXT,
277   .next_nodes =
278   {
279     [CNAT_TRANSLATION_NEXT_DROP] = "ip4-drop",
280     [CNAT_TRANSLATION_NEXT_LOOKUP] = "ip4-lookup",
281   }
282 };
283 VLIB_REGISTER_NODE (cnat_vip_ip6_node) =
284 {
285   .name = "ip6-cnat-tx",
286   .vector_size = sizeof (u32),
287   .format_trace = format_cnat_translation_trace,
288   .type = VLIB_NODE_TYPE_INTERNAL,
289   .n_errors = 0,
290   .n_next_nodes = CNAT_TRANSLATION_N_NEXT,
291   .next_nodes =
292   {
293     [CNAT_TRANSLATION_NEXT_DROP] = "ip6-drop",
294     [CNAT_TRANSLATION_NEXT_LOOKUP] = "ip6-lookup",
295   }
296 };
297 /* *INDENT-ON* */
298
299 /*
300  * fd.io coding-style-patch-verification: ON
301  *
302  * Local Variables:
303  * eval: (c-set-style "gnu")
304  * End:
305  */