Reorganize source tree to use single autotools instance
[vpp.git] / src / plugins / sixrd / ip6_sixrd.c
1 /*---------------------------------------------------------------------------
2  * Copyright (c) 2009-2014 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 /*
17  * Defines used for testing various optimisation schemes
18  */
19 #define SIXRD_ENCAP_DUAL 0
20
21 #include "sixrd.h"
22
23 static vlib_node_registration_t ip6_sixrd_node;
24
25 typedef enum {
26   IP6_SIXRD_NEXT_IP4_LOOKUP,
27   IP6_SIXRD_NEXT_DROP,
28   IP6_SIXRD_N_NEXT,
29 } ip6_sixrd_next_t;
30
31 /*
32  * ip6_sixrd
33  */
34 static uword
35 ip6_sixrd (vlib_main_t *vm,
36            vlib_node_runtime_t *node,
37            vlib_frame_t *frame)
38 {
39   u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
40   vlib_node_runtime_t *error_node = vlib_node_get_runtime(vm, ip6_sixrd_node.index);
41   u32 encap = 0;
42   from = vlib_frame_vector_args(frame);
43   n_left_from = frame->n_vectors;
44   next_index = node->cached_next_index;
45
46   while (n_left_from > 0) {
47     vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
48
49     while (n_left_from > 0 && n_left_to_next > 0) {
50       u32 pi0;
51       vlib_buffer_t *p0;
52       sixrd_domain_t *d0;
53       u8 error0 = SIXRD_ERROR_NONE;
54       ip6_header_t *ip60;
55       ip4_header_t *ip4h0;
56       u32 next0 = IP6_SIXRD_NEXT_IP4_LOOKUP;
57       u32 sixrd_domain_index0 = ~0;
58
59       pi0 = to_next[0] = from[0];
60       from += 1;
61       n_left_from -= 1;
62       to_next +=1;
63       n_left_to_next -= 1;
64
65       p0 = vlib_get_buffer(vm, pi0);
66       ip60 = vlib_buffer_get_current(p0);
67       //      p0->current_length = clib_net_to_host_u16(ip40->length);
68       d0 = ip6_sixrd_get_domain(vnet_buffer(p0)->ip.adj_index[VLIB_TX], &sixrd_domain_index0);
69       ASSERT(d0);
70
71       /* SIXRD calc */
72       u64 dal60 = clib_net_to_host_u64(ip60->dst_address.as_u64[0]);
73       u32 da40 = sixrd_get_addr(d0, dal60);
74       u16 len = clib_net_to_host_u16(ip60->payload_length) + 60;
75       if (da40 == 0) error0 = SIXRD_ERROR_UNKNOWN;
76
77       /* construct ipv4 header */
78       vlib_buffer_advance(p0, - (sizeof(ip4_header_t)));
79       ip4h0 = vlib_buffer_get_current(p0);
80       vnet_buffer(p0)->sw_if_index[VLIB_TX] = (u32)~0;
81       ip4h0->ip_version_and_header_length = 0x45;
82       ip4h0->tos = 0;
83       ip4h0->length = clib_host_to_net_u16(len);
84       ip4h0->fragment_id = 0;
85       ip4h0->flags_and_fragment_offset = 0;
86       ip4h0->ttl = 0x40;
87       ip4h0->protocol = IP_PROTOCOL_IPV6;
88       ip4h0->src_address = d0->ip4_src;
89       ip4h0->dst_address.as_u32 = clib_host_to_net_u32(da40);
90       ip4h0->checksum = ip4_header_checksum(ip4h0);
91
92       next0 = error0 == SIXRD_ERROR_NONE ? IP6_SIXRD_NEXT_IP4_LOOKUP : IP6_SIXRD_NEXT_DROP;
93
94       if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED)) {
95         sixrd_trace_t *tr = vlib_add_trace(vm, node, p0, sizeof(*tr));
96         tr->sixrd_domain_index = sixrd_domain_index0;
97       }
98
99       p0->error = error_node->errors[error0];
100       if (PREDICT_TRUE(error0 == SIXRD_ERROR_NONE)) encap++;
101
102       vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, pi0, next0);
103     }
104     vlib_put_next_frame(vm, node, next_index, n_left_to_next);  
105   }
106   vlib_node_increment_counter(vm, ip6_sixrd_node.index, SIXRD_ERROR_ENCAPSULATED, encap);
107
108   return frame->n_vectors;
109 }
110
111 static char *sixrd_error_strings[] = {
112 #define _(sym,string) string,
113   foreach_sixrd_error
114 #undef _
115 };
116
117 VLIB_REGISTER_NODE(ip6_sixrd_node,static) = {
118   .function = ip6_sixrd,
119   .name = "ip6-sixrd",
120   .vector_size = sizeof(u32),
121   .format_trace = format_sixrd_trace,
122   .n_errors = SIXRD_N_ERROR,
123   .error_strings = sixrd_error_strings,
124   .n_next_nodes = IP6_SIXRD_N_NEXT,
125   .next_nodes = {
126     [IP6_SIXRD_NEXT_IP4_LOOKUP] = "ip4-lookup",
127     [IP6_SIXRD_NEXT_DROP] = "error-drop",
128   },
129 };