Dual Loop Load-Balance Nodes
[vpp.git] / vnet / vnet / ip / udp.h
1 /*
2  * ip/udp.h: udp protocol
3  *
4  * Copyright (c) 2013 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #ifndef included_udp_h
19 #define included_udp_h
20
21 #include <vnet/vnet.h>
22 #include <vnet/ip/udp_packet.h>
23 #include <vnet/ip/ip.h>
24 #include <vnet/ip/ip4.h>
25 #include <vnet/ip/ip4_packet.h>
26 #include <vnet/pg/pg.h>
27 #include <vnet/ip/format.h>
28
29 typedef enum {
30 #define udp_error(n,s) UDP_ERROR_##n,
31 #include <vnet/ip/udp_error.def>
32 #undef udp_error
33   UDP_N_ERROR,
34 } udp_error_t;
35
36 #define foreach_udp4_dst_port                   \
37 _ (67, dhcp_to_server)                          \
38 _ (68, dhcp_to_client)                          \
39 _ (500, ikev2)                                  \
40 _ (4341, lisp_gpe)                              \
41 _ (4342, lisp_cp)                               \
42 _ (4739, ipfix)                                 \
43 _ (4789, vxlan)                                 \
44 _ (4789, vxlan6)                                \
45 _ (4790, vxlan_gpe)                             \
46 _ (6633, vpath_3)
47
48
49 #define foreach_udp6_dst_port                   \
50 _ (547, dhcpv6_to_server)                       \
51 _ (546, dhcpv6_to_client)                       \
52 _ (4341, lisp_gpe6)                             \
53 _ (4342, lisp_cp6)                              \
54 _ (4790, vxlan6_gpe)      \
55 _ (6633, vpath6_3)
56
57 typedef enum {
58 #define _(n,f) UDP_DST_PORT_##f = n,
59   foreach_udp4_dst_port
60   foreach_udp6_dst_port
61 #undef _
62 } udp_dst_port_t;
63
64 typedef enum {
65 #define _(n,f) UDP6_DST_PORT_##f = n,
66   foreach_udp6_dst_port
67 #undef _
68 } udp6_dst_port_t;
69
70 typedef struct {
71   /* Name (a c string). */
72   char * name;
73
74   /* GRE protocol type in host byte order. */
75   udp_dst_port_t dst_port;
76
77   /* Node which handles this type. */
78   u32 node_index;
79
80   /* Next index for this type. */
81   u32 next_index;
82 } udp_dst_port_info_t;
83
84 typedef enum {
85   UDP_IP6 = 0,
86   UDP_IP4,                      /* the code is full of is_ip4... */
87   N_UDP_AF,
88 } udp_af_t;
89
90 typedef struct {
91   udp_dst_port_info_t * dst_port_infos [N_UDP_AF];
92
93   /* Hash tables mapping name/protocol to protocol info index. */
94   uword * dst_port_info_by_name[N_UDP_AF];
95   uword * dst_port_info_by_dst_port[N_UDP_AF];
96
97   /* convenience */
98   vlib_main_t * vlib_main;
99 } udp_main_t;
100
101 always_inline udp_dst_port_info_t *
102 udp_get_dst_port_info (udp_main_t * um, udp_dst_port_t dst_port, u8 is_ip4)
103 {
104   uword * p = hash_get (um->dst_port_info_by_dst_port[is_ip4], dst_port);
105   return p ? vec_elt_at_index (um->dst_port_infos[is_ip4], p[0]) : 0;
106 }
107
108 format_function_t format_udp_header;
109 format_function_t format_udp_rx_trace;
110
111 unformat_function_t unformat_udp_header;
112
113 void udp_register_dst_port (vlib_main_t * vm,
114                             udp_dst_port_t dst_port,
115                             u32 node_index, u8 is_ip4);
116
117 void udp_punt_unknown(vlib_main_t * vm, u8 is_ip4, u8 is_add);
118
119 always_inline void
120 ip_udp_fixup_one (vlib_main_t * vm,
121                   vlib_buffer_t * b0,
122                   u8 is_ip4)
123 {
124   u16 new_l0;
125   udp_header_t * udp0;
126
127   if (is_ip4)
128     {
129       ip4_header_t * ip0;
130       ip_csum_t sum0;
131       u16 old_l0 = 0;
132
133       ip0 = vlib_buffer_get_current(b0);
134
135       /* fix the <bleep>ing outer-IP checksum */
136       sum0 = ip0->checksum;
137       /* old_l0 always 0, see the rewrite setup */
138       new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
139
140       sum0 = ip_csum_update(sum0, old_l0, new_l0, ip4_header_t,
141                             length /* changed member */);
142       ip0->checksum = ip_csum_fold (sum0);
143       ip0->length = new_l0;
144
145       /* Fix UDP length */
146       udp0 = (udp_header_t *)(ip0+1);
147       new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
148                                      - sizeof (*ip0));
149       udp0->length = new_l0;
150     }
151   else
152     {
153       ip6_header_t * ip0;
154       int bogus0;
155
156       ip0 = vlib_buffer_get_current(b0);
157
158       new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
159                                      - sizeof (*ip0));
160       ip0->payload_length = new_l0;
161
162       /* Fix UDP length */
163       udp0 = (udp_header_t *)(ip0+1);
164       udp0->length = new_l0;
165
166       udp0->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
167       ASSERT(bogus0 == 0);
168
169       if (udp0->checksum == 0)
170           udp0->checksum = 0xffff;
171     }
172 }
173 always_inline void
174 ip_udp_encap_one (vlib_main_t * vm, vlib_buffer_t * b0, u8 * ec0, word ec_len,
175                   u8 is_ip4)
176 {
177   vlib_buffer_advance (b0, - ec_len);
178
179   if (is_ip4)
180     {
181       ip4_header_t * ip0;
182
183       ip0 = vlib_buffer_get_current(b0);
184
185       /* Apply the encap string. */
186       clib_memcpy(ip0, ec0, ec_len);
187       ip_udp_fixup_one(vm, b0, 1);
188     }
189   else
190     {
191       ip6_header_t * ip0;
192
193       ip0 = vlib_buffer_get_current(b0);
194
195       /* Apply the encap string. */
196       clib_memcpy(ip0, ec0, ec_len);
197       ip_udp_fixup_one(vm, b0, 0);
198     }
199 }
200
201 always_inline void
202 ip_udp_encap_two (vlib_main_t * vm, vlib_buffer_t * b0, vlib_buffer_t * b1,
203                   u8 * ec0, u8 * ec1, word ec_len, u8 is_v4)
204 {
205   u16 new_l0, new_l1;
206   udp_header_t * udp0, *udp1;
207
208   ASSERT(_vec_len(ec0) == _vec_len(ec1));
209
210   vlib_buffer_advance (b0, -ec_len);
211   vlib_buffer_advance (b1, -ec_len);
212
213   if (is_v4)
214     {
215       ip4_header_t * ip0, *ip1;
216       ip_csum_t sum0, sum1;
217       u16 old_l0 = 0, old_l1 = 0;
218
219       ip0 = vlib_buffer_get_current (b0);
220       ip1 = vlib_buffer_get_current (b1);
221
222       /* Apply the encap string */
223       clib_memcpy (ip0, ec0, ec_len);
224       clib_memcpy (ip1, ec1, ec_len);
225
226       /* fix the <bleep>ing outer-IP checksum */
227       sum0 = ip0->checksum;
228       sum1 = ip1->checksum;
229
230       /* old_l0 always 0, see the rewrite setup */
231       new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
232       new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1));
233
234       sum0 = ip_csum_update(sum0, old_l0, new_l0, ip4_header_t,
235                             length /* changed member */);
236       sum1 = ip_csum_update(sum1, old_l1, new_l1, ip4_header_t,
237                             length /* changed member */);
238
239       ip0->checksum = ip_csum_fold (sum0);
240       ip1->checksum = ip_csum_fold (sum1);
241
242       ip0->length = new_l0;
243       ip1->length = new_l1;
244
245       /* Fix UDP length */
246       udp0 = (udp_header_t *) (ip0 + 1);
247       udp1 = (udp_header_t *) (ip1 + 1);
248
249       new_l0 = clib_host_to_net_u16 (
250           vlib_buffer_length_in_chain (vm, b0) - sizeof(*ip0));
251       new_l1 = clib_host_to_net_u16 (
252           vlib_buffer_length_in_chain (vm, b1) - sizeof(*ip1));
253       udp0->length = new_l0;
254       udp1->length = new_l1;
255     }
256   else
257     {
258       ip6_header_t * ip0, * ip1;
259       int bogus0, bogus1;
260
261       ip0 = vlib_buffer_get_current(b0);
262       ip1 = vlib_buffer_get_current(b1);
263
264       /* Apply the encap string. */
265       clib_memcpy(ip0, ec0, ec_len);
266       clib_memcpy(ip1, ec1, ec_len);
267
268       new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
269                                      - sizeof (*ip0));
270       new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1)
271                                      - sizeof (*ip1));
272       ip0->payload_length = new_l0;
273       ip1->payload_length = new_l1;
274
275       /* Fix UDP length */
276       udp0 = (udp_header_t *)(ip0+1);
277       udp1 = (udp_header_t *)(ip1+1);
278
279       udp0->length = new_l0;
280       udp1->length = new_l1;
281
282       udp0->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0, &bogus0);
283       udp1->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b1, ip1, &bogus1);
284       ASSERT(bogus0 == 0);
285       ASSERT(bogus1 == 0);
286
287       if (udp0->checksum == 0)
288           udp0->checksum = 0xffff;
289       if (udp1->checksum == 0)
290           udp1->checksum = 0xffff;
291     }
292 }
293
294 #endif /* included_udp_h */