VPP-263 - Coding standards cleanup - vnet/vnet/map
[vpp.git] / vnet / vnet / map / ip4_map.c
1 /*
2  * Copyright (c) 2015 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  * Defines used for testing various optimisation schemes
17  */
18 #define MAP_ENCAP_DUAL 0
19
20 #include "map.h"
21 #include "../ip/ip_frag.h"
22
23 vlib_node_registration_t ip4_map_reass_node;
24
25 enum ip4_map_next_e
26 {
27   IP4_MAP_NEXT_IP6_LOOKUP,
28 #ifdef MAP_SKIP_IP6_LOOKUP
29   IP4_MAP_NEXT_IP6_REWRITE,
30 #endif
31   IP4_MAP_NEXT_IP4_FRAGMENT,
32   IP4_MAP_NEXT_IP6_FRAGMENT,
33   IP4_MAP_NEXT_REASS,
34   IP4_MAP_NEXT_ICMP_ERROR,
35   IP4_MAP_NEXT_DROP,
36   IP4_MAP_N_NEXT,
37 };
38
39 enum ip4_map_reass_next_t
40 {
41   IP4_MAP_REASS_NEXT_IP6_LOOKUP,
42   IP4_MAP_REASS_NEXT_IP4_FRAGMENT,
43   IP4_MAP_REASS_NEXT_DROP,
44   IP4_MAP_REASS_N_NEXT,
45 };
46
47 typedef struct
48 {
49   u32 map_domain_index;
50   u16 port;
51   u8 cached;
52 } map_ip4_map_reass_trace_t;
53
54 u8 *
55 format_ip4_map_reass_trace (u8 * s, va_list * args)
56 {
57   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
58   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
59   map_ip4_map_reass_trace_t *t = va_arg (*args, map_ip4_map_reass_trace_t *);
60   return format (s, "MAP domain index: %d L4 port: %u Status: %s",
61                  t->map_domain_index, t->port,
62                  t->cached ? "cached" : "forwarded");
63 }
64
65 /*
66  * ip4_map_get_port
67  */
68 u16
69 ip4_map_get_port (ip4_header_t * ip, map_dir_e dir)
70 {
71   /* Find port information */
72   if (PREDICT_TRUE ((ip->protocol == IP_PROTOCOL_TCP) ||
73                     (ip->protocol == IP_PROTOCOL_UDP)))
74     {
75       udp_header_t *udp = (void *) (ip + 1);
76       return (dir == MAP_SENDER ? udp->src_port : udp->dst_port);
77     }
78   else if (ip->protocol == IP_PROTOCOL_ICMP)
79     {
80       /*
81        * 1) ICMP Echo request or Echo reply
82        * 2) ICMP Error with inner packet being UDP or TCP
83        * 3) ICMP Error with inner packet being ICMP Echo request or Echo reply
84        */
85       icmp46_header_t *icmp = (void *) (ip + 1);
86       if (icmp->type == ICMP4_echo_request || icmp->type == ICMP4_echo_reply)
87         {
88           return *((u16 *) (icmp + 1));
89         }
90       else if (clib_net_to_host_u16 (ip->length) >= 56)
91         {                       // IP + ICMP + IP + L4 header
92           ip4_header_t *icmp_ip = (ip4_header_t *) (icmp + 2);
93           if (PREDICT_TRUE ((icmp_ip->protocol == IP_PROTOCOL_TCP) ||
94                             (icmp_ip->protocol == IP_PROTOCOL_UDP)))
95             {
96               udp_header_t *udp = (void *) (icmp_ip + 1);
97               return (dir == MAP_SENDER ? udp->dst_port : udp->src_port);
98             }
99           else if (icmp_ip->protocol == IP_PROTOCOL_ICMP)
100             {
101               icmp46_header_t *inner_icmp = (void *) (icmp_ip + 1);
102               if (inner_icmp->type == ICMP4_echo_request
103                   || inner_icmp->type == ICMP4_echo_reply)
104                 return (*((u16 *) (inner_icmp + 1)));
105             }
106         }
107     }
108   return (0);
109 }
110
111 static_always_inline u16
112 ip4_map_port_and_security_check (map_domain_t * d, ip4_header_t * ip,
113                                  u32 * next, u8 * error)
114 {
115   u16 port = 0;
116
117   if (d->psid_length > 0)
118     {
119       if (ip4_get_fragment_offset (ip) == 0)
120         {
121           if (PREDICT_FALSE
122               ((ip->ip_version_and_header_length != 0x45)
123                || clib_host_to_net_u16 (ip->length) < 28))
124             {
125               return 0;
126             }
127           port = ip4_map_get_port (ip, MAP_RECEIVER);
128           if (port)
129             {
130               /* Verify that port is not among the well-known ports */
131               if ((d->psid_offset > 0)
132                   && (clib_net_to_host_u16 (port) <
133                       (0x1 << (16 - d->psid_offset))))
134                 {
135                   *error = MAP_ERROR_ENCAP_SEC_CHECK;
136                 }
137               else
138                 {
139                   if (ip4_get_fragment_more (ip))
140                     *next = IP4_MAP_NEXT_REASS;
141                   return (port);
142                 }
143             }
144           else
145             {
146               *error = MAP_ERROR_BAD_PROTOCOL;
147             }
148         }
149       else
150         {
151           *next = IP4_MAP_NEXT_REASS;
152         }
153     }
154   return (0);
155 }
156
157 /*
158  * ip4_map_vtcfl
159  */
160 static_always_inline u32
161 ip4_map_vtcfl (ip4_header_t * ip4, vlib_buffer_t * p)
162 {
163   map_main_t *mm = &map_main;
164   u8 tc = mm->tc_copy ? ip4->tos : mm->tc;
165   u32 vtcfl = 0x6 << 28;
166   vtcfl |= tc << 20;
167   vtcfl |= vnet_buffer (p)->ip.flow_hash & 0x000fffff;
168
169   return (clib_host_to_net_u32 (vtcfl));
170 }
171
172 static_always_inline bool
173 ip4_map_ip6_lookup_bypass (vlib_buffer_t * p0, ip4_header_t * ip)
174 {
175 #ifdef MAP_SKIP_IP6_LOOKUP
176   map_main_t *mm = &map_main;
177   u32 adj_index0 = mm->adj6_index;
178   if (adj_index0 > 0)
179     {
180       ip_lookup_main_t *lm6 = &ip6_main.lookup_main;
181       ip_adjacency_t *adj = ip_get_adjacency (lm6, mm->adj6_index);
182       if (adj->n_adj > 1)
183         {
184           u32 hash_c0 = ip4_compute_flow_hash (ip, IP_FLOW_HASH_DEFAULT);
185           adj_index0 += (hash_c0 & (adj->n_adj - 1));
186         }
187       vnet_buffer (p0)->ip.adj_index[VLIB_TX] = adj_index0;
188       return (true);
189     }
190 #endif
191   return (false);
192 }
193
194 /*
195  * ip4_map_ttl
196  */
197 static inline void
198 ip4_map_decrement_ttl (ip4_header_t * ip, u8 * error)
199 {
200   i32 ttl = ip->ttl;
201
202   /* Input node should have reject packets with ttl 0. */
203   ASSERT (ip->ttl > 0);
204
205   u32 checksum = ip->checksum + clib_host_to_net_u16 (0x0100);
206   checksum += checksum >= 0xffff;
207   ip->checksum = checksum;
208   ttl -= 1;
209   ip->ttl = ttl;
210   *error = ttl <= 0 ? IP4_ERROR_TIME_EXPIRED : *error;
211
212   /* Verify checksum. */
213   ASSERT (ip->checksum == ip4_header_checksum (ip));
214 }
215
216 static u32
217 ip4_map_fragment (vlib_buffer_t * b, u16 mtu, bool df, u8 * error)
218 {
219   map_main_t *mm = &map_main;
220
221   if (mm->frag_inner)
222     {
223       ip_frag_set_vnet_buffer (b, sizeof (ip6_header_t), mtu,
224                                IP4_FRAG_NEXT_IP6_LOOKUP,
225                                IP_FRAG_FLAG_IP6_HEADER);
226       return (IP4_MAP_NEXT_IP4_FRAGMENT);
227     }
228   else
229     {
230       if (df && !mm->frag_ignore_df)
231         {
232           icmp4_error_set_vnet_buffer (b, ICMP4_destination_unreachable,
233                                        ICMP4_destination_unreachable_fragmentation_needed_and_dont_fragment_set,
234                                        mtu);
235           vlib_buffer_advance (b, sizeof (ip6_header_t));
236           *error = MAP_ERROR_DF_SET;
237           return (IP4_MAP_NEXT_ICMP_ERROR);
238         }
239       ip_frag_set_vnet_buffer (b, 0, mtu, IP6_FRAG_NEXT_IP6_LOOKUP,
240                                IP_FRAG_FLAG_IP6_HEADER);
241       return (IP4_MAP_NEXT_IP6_FRAGMENT);
242     }
243 }
244
245 /*
246  * ip4_map
247  */
248 static uword
249 ip4_map (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
250 {
251   u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
252   vlib_node_runtime_t *error_node =
253     vlib_node_get_runtime (vm, ip4_map_node.index);
254   from = vlib_frame_vector_args (frame);
255   n_left_from = frame->n_vectors;
256   next_index = node->cached_next_index;
257   map_main_t *mm = &map_main;
258   vlib_combined_counter_main_t *cm = mm->domain_counters;
259   u32 cpu_index = os_get_cpu_number ();
260
261   while (n_left_from > 0)
262     {
263       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
264
265       /* Dual loop */
266       while (n_left_from >= 4 && n_left_to_next >= 2)
267         {
268           u32 pi0, pi1;
269           vlib_buffer_t *p0, *p1;
270           map_domain_t *d0, *d1;
271           u8 error0 = MAP_ERROR_NONE, error1 = MAP_ERROR_NONE;
272           ip4_header_t *ip40, *ip41;
273           u16 port0 = 0, port1 = 0;
274           ip6_header_t *ip6h0, *ip6h1;
275           u32 map_domain_index0 = ~0, map_domain_index1 = ~0;
276           u32 next0 = IP4_MAP_NEXT_IP6_LOOKUP, next1 =
277             IP4_MAP_NEXT_IP6_LOOKUP;
278
279           /* Prefetch next iteration. */
280           {
281             vlib_buffer_t *p2, *p3;
282
283             p2 = vlib_get_buffer (vm, from[2]);
284             p3 = vlib_get_buffer (vm, from[3]);
285
286             vlib_prefetch_buffer_header (p2, STORE);
287             vlib_prefetch_buffer_header (p3, STORE);
288             /* IPv4 + 8 = 28. possibly plus -40 */
289             CLIB_PREFETCH (p2->data - 40, 68, STORE);
290             CLIB_PREFETCH (p3->data - 40, 68, STORE);
291           }
292
293           pi0 = to_next[0] = from[0];
294           pi1 = to_next[1] = from[1];
295           from += 2;
296           n_left_from -= 2;
297           to_next += 2;
298           n_left_to_next -= 2;
299
300           p0 = vlib_get_buffer (vm, pi0);
301           p1 = vlib_get_buffer (vm, pi1);
302           ip40 = vlib_buffer_get_current (p0);
303           ip41 = vlib_buffer_get_current (p1);
304           d0 =
305             ip4_map_get_domain (vnet_buffer (p0)->ip.adj_index[VLIB_TX],
306                                 &map_domain_index0);
307           d1 =
308             ip4_map_get_domain (vnet_buffer (p1)->ip.adj_index[VLIB_TX],
309                                 &map_domain_index1);
310           ASSERT (d0);
311           ASSERT (d1);
312
313           /*
314            * Shared IPv4 address
315            */
316           port0 = ip4_map_port_and_security_check (d0, ip40, &next0, &error0);
317           port1 = ip4_map_port_and_security_check (d1, ip41, &next1, &error1);
318
319           /* Decrement IPv4 TTL */
320           ip4_map_decrement_ttl (ip40, &error0);
321           ip4_map_decrement_ttl (ip41, &error1);
322           bool df0 =
323             ip40->
324             flags_and_fragment_offset &
325             clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT);
326           bool df1 =
327             ip41->
328             flags_and_fragment_offset &
329             clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT);
330
331           /* MAP calc */
332           u32 da40 = clib_net_to_host_u32 (ip40->dst_address.as_u32);
333           u32 da41 = clib_net_to_host_u32 (ip41->dst_address.as_u32);
334           u16 dp40 = clib_net_to_host_u16 (port0);
335           u16 dp41 = clib_net_to_host_u16 (port1);
336           u64 dal60 = map_get_pfx (d0, da40, dp40);
337           u64 dal61 = map_get_pfx (d1, da41, dp41);
338           u64 dar60 = map_get_sfx (d0, da40, dp40);
339           u64 dar61 = map_get_sfx (d1, da41, dp41);
340           if (dal60 == 0 && dar60 == 0 && error0 == MAP_ERROR_NONE
341               && next0 != IP4_MAP_NEXT_REASS)
342             error0 = MAP_ERROR_NO_BINDING;
343           if (dal61 == 0 && dar61 == 0 && error1 == MAP_ERROR_NONE
344               && next1 != IP4_MAP_NEXT_REASS)
345             error1 = MAP_ERROR_NO_BINDING;
346
347           /* construct ipv6 header */
348           vlib_buffer_advance (p0, -sizeof (ip6_header_t));
349           vlib_buffer_advance (p1, -sizeof (ip6_header_t));
350           ip6h0 = vlib_buffer_get_current (p0);
351           ip6h1 = vlib_buffer_get_current (p1);
352           vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
353           vnet_buffer (p1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
354
355           ip6h0->ip_version_traffic_class_and_flow_label =
356             ip4_map_vtcfl (ip40, p0);
357           ip6h1->ip_version_traffic_class_and_flow_label =
358             ip4_map_vtcfl (ip41, p1);
359           ip6h0->payload_length = ip40->length;
360           ip6h1->payload_length = ip41->length;
361           ip6h0->protocol = IP_PROTOCOL_IP_IN_IP;
362           ip6h1->protocol = IP_PROTOCOL_IP_IN_IP;
363           ip6h0->hop_limit = 0x40;
364           ip6h1->hop_limit = 0x40;
365           ip6h0->src_address = d0->ip6_src;
366           ip6h1->src_address = d1->ip6_src;
367           ip6h0->dst_address.as_u64[0] = clib_host_to_net_u64 (dal60);
368           ip6h0->dst_address.as_u64[1] = clib_host_to_net_u64 (dar60);
369           ip6h1->dst_address.as_u64[0] = clib_host_to_net_u64 (dal61);
370           ip6h1->dst_address.as_u64[1] = clib_host_to_net_u64 (dar61);
371
372           /*
373            * Determine next node. Can be one of:
374            * ip6-lookup, ip6-rewrite, ip4-fragment, ip4-virtreass, error-drop
375            */
376           if (PREDICT_TRUE (error0 == MAP_ERROR_NONE))
377             {
378               if (PREDICT_FALSE
379                   (d0->mtu
380                    && (clib_net_to_host_u16 (ip6h0->payload_length) +
381                        sizeof (*ip6h0) > d0->mtu)))
382                 {
383                   next0 = ip4_map_fragment (p0, d0->mtu, df0, &error0);
384                 }
385               else
386                 {
387                   next0 =
388                     ip4_map_ip6_lookup_bypass (p0,
389                                                ip40) ?
390                     IP4_MAP_NEXT_IP6_REWRITE : next0;
391                   vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_TX,
392                                                    cpu_index,
393                                                    map_domain_index0, 1,
394                                                    clib_net_to_host_u16
395                                                    (ip6h0->payload_length) +
396                                                    40);
397                 }
398             }
399           else
400             {
401               next0 = IP4_MAP_NEXT_DROP;
402             }
403
404           /*
405            * Determine next node. Can be one of:
406            * ip6-lookup, ip6-rewrite, ip4-fragment, ip4-virtreass, error-drop
407            */
408           if (PREDICT_TRUE (error1 == MAP_ERROR_NONE))
409             {
410               if (PREDICT_FALSE
411                   (d1->mtu
412                    && (clib_net_to_host_u16 (ip6h1->payload_length) +
413                        sizeof (*ip6h1) > d1->mtu)))
414                 {
415                   next1 = ip4_map_fragment (p1, d1->mtu, df1, &error1);
416                 }
417               else
418                 {
419                   next1 =
420                     ip4_map_ip6_lookup_bypass (p1,
421                                                ip41) ?
422                     IP4_MAP_NEXT_IP6_REWRITE : next1;
423                   vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_TX,
424                                                    cpu_index,
425                                                    map_domain_index1, 1,
426                                                    clib_net_to_host_u16
427                                                    (ip6h1->payload_length) +
428                                                    40);
429                 }
430             }
431           else
432             {
433               next1 = IP4_MAP_NEXT_DROP;
434             }
435
436           if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
437             {
438               map_trace_t *tr = vlib_add_trace (vm, node, p0, sizeof (*tr));
439               tr->map_domain_index = map_domain_index0;
440               tr->port = port0;
441             }
442           if (PREDICT_FALSE (p1->flags & VLIB_BUFFER_IS_TRACED))
443             {
444               map_trace_t *tr = vlib_add_trace (vm, node, p1, sizeof (*tr));
445               tr->map_domain_index = map_domain_index1;
446               tr->port = port1;
447             }
448
449           p0->error = error_node->errors[error0];
450           p1->error = error_node->errors[error1];
451
452           vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
453                                            n_left_to_next, pi0, pi1, next0,
454                                            next1);
455         }
456
457       while (n_left_from > 0 && n_left_to_next > 0)
458         {
459           u32 pi0;
460           vlib_buffer_t *p0;
461           map_domain_t *d0;
462           u8 error0 = MAP_ERROR_NONE;
463           ip4_header_t *ip40;
464           u16 port0 = 0;
465           ip6_header_t *ip6h0;
466           u32 next0 = IP4_MAP_NEXT_IP6_LOOKUP;
467           u32 map_domain_index0 = ~0;
468
469           pi0 = to_next[0] = from[0];
470           from += 1;
471           n_left_from -= 1;
472           to_next += 1;
473           n_left_to_next -= 1;
474
475           p0 = vlib_get_buffer (vm, pi0);
476           ip40 = vlib_buffer_get_current (p0);
477           d0 =
478             ip4_map_get_domain (vnet_buffer (p0)->ip.adj_index[VLIB_TX],
479                                 &map_domain_index0);
480           ASSERT (d0);
481
482           /*
483            * Shared IPv4 address
484            */
485           port0 = ip4_map_port_and_security_check (d0, ip40, &next0, &error0);
486
487           /* Decrement IPv4 TTL */
488           ip4_map_decrement_ttl (ip40, &error0);
489           bool df0 =
490             ip40->
491             flags_and_fragment_offset &
492             clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT);
493
494           /* MAP calc */
495           u32 da40 = clib_net_to_host_u32 (ip40->dst_address.as_u32);
496           u16 dp40 = clib_net_to_host_u16 (port0);
497           u64 dal60 = map_get_pfx (d0, da40, dp40);
498           u64 dar60 = map_get_sfx (d0, da40, dp40);
499           if (dal60 == 0 && dar60 == 0 && error0 == MAP_ERROR_NONE
500               && next0 != IP4_MAP_NEXT_REASS)
501             error0 = MAP_ERROR_NO_BINDING;
502
503           /* construct ipv6 header */
504           vlib_buffer_advance (p0, -(sizeof (ip6_header_t)));
505           ip6h0 = vlib_buffer_get_current (p0);
506           vnet_buffer (p0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
507
508           ip6h0->ip_version_traffic_class_and_flow_label =
509             ip4_map_vtcfl (ip40, p0);
510           ip6h0->payload_length = ip40->length;
511           ip6h0->protocol = IP_PROTOCOL_IP_IN_IP;
512           ip6h0->hop_limit = 0x40;
513           ip6h0->src_address = d0->ip6_src;
514           ip6h0->dst_address.as_u64[0] = clib_host_to_net_u64 (dal60);
515           ip6h0->dst_address.as_u64[1] = clib_host_to_net_u64 (dar60);
516
517           /*
518            * Determine next node. Can be one of:
519            * ip6-lookup, ip6-rewrite, ip4-fragment, ip4-virtreass, error-drop
520            */
521           if (PREDICT_TRUE (error0 == MAP_ERROR_NONE))
522             {
523               if (PREDICT_FALSE
524                   (d0->mtu
525                    && (clib_net_to_host_u16 (ip6h0->payload_length) +
526                        sizeof (*ip6h0) > d0->mtu)))
527                 {
528                   next0 = ip4_map_fragment (p0, d0->mtu, df0, &error0);
529                 }
530               else
531                 {
532                   next0 =
533                     ip4_map_ip6_lookup_bypass (p0,
534                                                ip40) ?
535                     IP4_MAP_NEXT_IP6_REWRITE : next0;
536                   vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_TX,
537                                                    cpu_index,
538                                                    map_domain_index0, 1,
539                                                    clib_net_to_host_u16
540                                                    (ip6h0->payload_length) +
541                                                    40);
542                 }
543             }
544           else
545             {
546               next0 = IP4_MAP_NEXT_DROP;
547             }
548
549           if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
550             {
551               map_trace_t *tr = vlib_add_trace (vm, node, p0, sizeof (*tr));
552               tr->map_domain_index = map_domain_index0;
553               tr->port = port0;
554             }
555
556           p0->error = error_node->errors[error0];
557           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
558                                            n_left_to_next, pi0, next0);
559         }
560       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
561     }
562
563   return frame->n_vectors;
564 }
565
566 /*
567  * ip4_map_reass
568  */
569 static uword
570 ip4_map_reass (vlib_main_t * vm,
571                vlib_node_runtime_t * node, vlib_frame_t * frame)
572 {
573   u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
574   vlib_node_runtime_t *error_node =
575     vlib_node_get_runtime (vm, ip4_map_reass_node.index);
576   from = vlib_frame_vector_args (frame);
577   n_left_from = frame->n_vectors;
578   next_index = node->cached_next_index;
579   map_main_t *mm = &map_main;
580   vlib_combined_counter_main_t *cm = mm->domain_counters;
581   u32 cpu_index = os_get_cpu_number ();
582   u32 *fragments_to_drop = NULL;
583   u32 *fragments_to_loopback = NULL;
584
585   while (n_left_from > 0)
586     {
587       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
588
589       while (n_left_from > 0 && n_left_to_next > 0)
590         {
591           u32 pi0;
592           vlib_buffer_t *p0;
593           map_domain_t *d0;
594           u8 error0 = MAP_ERROR_NONE;
595           ip4_header_t *ip40;
596           i32 port0 = 0;
597           ip6_header_t *ip60;
598           u32 next0 = IP4_MAP_REASS_NEXT_IP6_LOOKUP;
599           u32 map_domain_index0;
600           u8 cached = 0;
601
602           pi0 = to_next[0] = from[0];
603           from += 1;
604           n_left_from -= 1;
605           to_next += 1;
606           n_left_to_next -= 1;
607
608           p0 = vlib_get_buffer (vm, pi0);
609           ip60 = vlib_buffer_get_current (p0);
610           ip40 = (ip4_header_t *) (ip60 + 1);
611           d0 =
612             ip4_map_get_domain (vnet_buffer (p0)->ip.adj_index[VLIB_TX],
613                                 &map_domain_index0);
614
615           map_ip4_reass_lock ();
616           map_ip4_reass_t *r =
617             map_ip4_reass_get (ip40->src_address.as_u32,
618                                ip40->dst_address.as_u32,
619                                ip40->fragment_id, ip40->protocol,
620                                &fragments_to_drop);
621           if (PREDICT_FALSE (!r))
622             {
623               // Could not create a caching entry
624               error0 = MAP_ERROR_FRAGMENT_MEMORY;
625             }
626           else if (PREDICT_TRUE (ip4_get_fragment_offset (ip40)))
627             {
628               if (r->port >= 0)
629                 {
630                   // We know the port already
631                   port0 = r->port;
632                 }
633               else if (map_ip4_reass_add_fragment (r, pi0))
634                 {
635                   // Not enough space for caching
636                   error0 = MAP_ERROR_FRAGMENT_MEMORY;
637                   map_ip4_reass_free (r, &fragments_to_drop);
638                 }
639               else
640                 {
641                   cached = 1;
642                 }
643             }
644           else
645             if ((port0 =
646                  ip4_get_port (ip40, MAP_RECEIVER, p0->current_length)) < 0)
647             {
648               // Could not find port. We'll free the reassembly.
649               error0 = MAP_ERROR_BAD_PROTOCOL;
650               port0 = 0;
651               map_ip4_reass_free (r, &fragments_to_drop);
652             }
653           else
654             {
655               r->port = port0;
656               map_ip4_reass_get_fragments (r, &fragments_to_loopback);
657             }
658
659 #ifdef MAP_IP4_REASS_COUNT_BYTES
660           if (!cached && r)
661             {
662               r->forwarded += clib_host_to_net_u16 (ip40->length) - 20;
663               if (!ip4_get_fragment_more (ip40))
664                 r->expected_total =
665                   ip4_get_fragment_offset (ip40) * 8 +
666                   clib_host_to_net_u16 (ip40->length) - 20;
667               if (r->forwarded >= r->expected_total)
668                 map_ip4_reass_free (r, &fragments_to_drop);
669             }
670 #endif
671
672           map_ip4_reass_unlock ();
673
674           // NOTE: Most operations have already been performed by ip4_map
675           // All we need is the right destination address
676           ip60->dst_address.as_u64[0] =
677             map_get_pfx_net (d0, ip40->dst_address.as_u32, port0);
678           ip60->dst_address.as_u64[1] =
679             map_get_sfx_net (d0, ip40->dst_address.as_u32, port0);
680
681           if (PREDICT_FALSE
682               (d0->mtu
683                && (clib_net_to_host_u16 (ip60->payload_length) +
684                    sizeof (*ip60) > d0->mtu)))
685             {
686               vnet_buffer (p0)->ip_frag.header_offset = sizeof (*ip60);
687               vnet_buffer (p0)->ip_frag.next_index = IP4_FRAG_NEXT_IP6_LOOKUP;
688               vnet_buffer (p0)->ip_frag.mtu = d0->mtu;
689               vnet_buffer (p0)->ip_frag.flags = IP_FRAG_FLAG_IP6_HEADER;
690               next0 = IP4_MAP_REASS_NEXT_IP4_FRAGMENT;
691             }
692
693           if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
694             {
695               map_ip4_map_reass_trace_t *tr =
696                 vlib_add_trace (vm, node, p0, sizeof (*tr));
697               tr->map_domain_index = map_domain_index0;
698               tr->port = port0;
699               tr->cached = cached;
700             }
701
702           if (cached)
703             {
704               //Dequeue the packet
705               n_left_to_next++;
706               to_next--;
707             }
708           else
709             {
710               if (error0 == MAP_ERROR_NONE)
711                 vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_TX,
712                                                  cpu_index, map_domain_index0,
713                                                  1,
714                                                  clib_net_to_host_u16 (ip60->
715                                                                        payload_length)
716                                                  + 40);
717               next0 =
718                 (error0 == MAP_ERROR_NONE) ? next0 : IP4_MAP_REASS_NEXT_DROP;
719               p0->error = error_node->errors[error0];
720               vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
721                                                n_left_to_next, pi0, next0);
722             }
723
724           //Loopback when we reach the end of the inpu vector
725           if (n_left_from == 0 && vec_len (fragments_to_loopback))
726             {
727               from = vlib_frame_vector_args (frame);
728               u32 len = vec_len (fragments_to_loopback);
729               if (len <= VLIB_FRAME_SIZE)
730                 {
731                   clib_memcpy (from, fragments_to_loopback,
732                                sizeof (u32) * len);
733                   n_left_from = len;
734                   vec_reset_length (fragments_to_loopback);
735                 }
736               else
737                 {
738                   clib_memcpy (from,
739                                fragments_to_loopback + (len -
740                                                         VLIB_FRAME_SIZE),
741                                sizeof (u32) * VLIB_FRAME_SIZE);
742                   n_left_from = VLIB_FRAME_SIZE;
743                   _vec_len (fragments_to_loopback) = len - VLIB_FRAME_SIZE;
744                 }
745             }
746         }
747       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
748     }
749
750   map_send_all_to_node (vm, fragments_to_drop, node,
751                         &error_node->errors[MAP_ERROR_FRAGMENT_DROPPED],
752                         IP4_MAP_REASS_NEXT_DROP);
753
754   vec_free (fragments_to_drop);
755   vec_free (fragments_to_loopback);
756   return frame->n_vectors;
757 }
758
759 static char *map_error_strings[] = {
760 #define _(sym,string) string,
761   foreach_map_error
762 #undef _
763 };
764
765 /* *INDENT-OFF* */
766 VLIB_REGISTER_NODE(ip4_map_node) = {
767   .function = ip4_map,
768   .name = "ip4-map",
769   .vector_size = sizeof(u32),
770   .format_trace = format_map_trace,
771   .type = VLIB_NODE_TYPE_INTERNAL,
772   
773   .n_errors = MAP_N_ERROR,
774   .error_strings = map_error_strings,
775
776   .n_next_nodes = IP4_MAP_N_NEXT,
777   .next_nodes = {
778     [IP4_MAP_NEXT_IP6_LOOKUP] = "ip6-lookup",
779 #ifdef MAP_SKIP_IP6_LOOKUP
780     [IP4_MAP_NEXT_IP6_REWRITE] = "ip6-rewrite",
781 #endif
782     [IP4_MAP_NEXT_IP4_FRAGMENT] = "ip4-frag",
783     [IP4_MAP_NEXT_IP6_FRAGMENT] = "ip6-frag",
784     [IP4_MAP_NEXT_REASS] = "ip4-map-reass",
785     [IP4_MAP_NEXT_ICMP_ERROR] = "ip4-icmp-error",
786     [IP4_MAP_NEXT_DROP] = "error-drop",
787   },
788 };
789 /* *INDENT-ON* */
790
791 /* *INDENT-OFF* */
792 VLIB_REGISTER_NODE(ip4_map_reass_node) = {
793   .function = ip4_map_reass,
794   .name = "ip4-map-reass",
795   .vector_size = sizeof(u32),
796   .format_trace = format_ip4_map_reass_trace,
797   .type = VLIB_NODE_TYPE_INTERNAL,
798   
799   .n_errors = MAP_N_ERROR,
800   .error_strings = map_error_strings,
801
802   .n_next_nodes = IP4_MAP_REASS_N_NEXT,
803   .next_nodes = {
804     [IP4_MAP_REASS_NEXT_IP6_LOOKUP] = "ip6-lookup",
805     [IP4_MAP_REASS_NEXT_IP4_FRAGMENT] = "ip4-frag",
806     [IP4_MAP_REASS_NEXT_DROP] = "error-drop",
807   },
808 };
809 /* *INDENT-ON* */
810
811 /*
812  * fd.io coding-style-patch-verification: ON
813  *
814  * Local Variables:
815  * eval: (c-set-style "gnu")
816  * End:
817  */