feature: convert all feature nodes to new feature infra
[vpp.git] / vnet / vnet / ip / ip.h
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  * ip/ip.h: ip generic (4 or 6) main
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39
40 #ifndef included_ip_main_h
41 #define included_ip_main_h
42
43 #include <vppinfra/hash.h>
44 #include <vppinfra/heap.h>              /* adjacency heap */
45 #include <vppinfra/ptclosure.h>
46
47 #include <vnet/vnet.h>
48
49 #include <vnet/ip/format.h>
50 #include <vnet/ip/ip_packet.h>
51 #include <vnet/ip/lookup.h>
52
53 #include <vnet/ip/tcp_packet.h>
54 #include <vnet/ip/udp_packet.h>
55 #include <vnet/ip/icmp46_packet.h>
56
57 #include <vnet/ip/ip4.h>
58 #include <vnet/ip/ip4_error.h>
59 #include <vnet/ip/ip4_packet.h>
60 #include <vnet/ip/icmp4.h>
61
62 #include <vnet/ip/ip6.h>
63 #include <vnet/ip/ip6_packet.h>
64 #include <vnet/ip/ip6_error.h>
65 #include <vnet/ip/icmp6.h>
66
67 #if DPDK > 0
68 #include <vnet/devices/dpdk/dpdk.h>
69 #endif
70
71 #include <vnet/classify/vnet_classify.h>
72
73 /* Per protocol info. */
74 typedef struct {
75   /* Protocol name (also used as hash key). */
76   u8 * name;
77
78   /* Protocol number. */
79   ip_protocol_t protocol;
80
81   /* Format function for this IP protocol. */
82   format_function_t * format_header;
83
84   /* Parser for header. */
85   unformat_function_t * unformat_header;
86
87   /* Parser for per-protocol matches. */
88   unformat_function_t * unformat_match;
89
90   /* Parser for packet generator edits for this protocol. */
91   unformat_function_t * unformat_pg_edit;
92 } ip_protocol_info_t;
93
94 /* Per TCP/UDP port info. */
95 typedef struct {
96   /* Port name (used as hash key). */
97   u8 * name;
98
99   /* UDP/TCP port number in network byte order. */
100   u16 port;
101
102   /* Port specific format function. */
103   format_function_t * format_header;
104
105   /* Parser for packet generator edits for this protocol. */
106   unformat_function_t * unformat_pg_edit;
107 } tcp_udp_port_info_t;
108
109 typedef struct {
110   /* Per IP protocol info. */
111   ip_protocol_info_t * protocol_infos;
112
113   /* Protocol info index hashed by 8 bit IP protocol. */
114   uword * protocol_info_by_protocol;
115
116   /* Hash table mapping IP protocol name (see protocols.def)
117      to protocol number. */
118   uword * protocol_info_by_name;
119
120   /* Per TCP/UDP port info. */
121   tcp_udp_port_info_t * port_infos;
122
123   /* Hash table from network-byte-order port to port info index. */
124   uword * port_info_by_port;
125
126   /* Hash table mapping TCP/UDP name to port info index. */
127   uword * port_info_by_name;
128 } ip_main_t;
129
130 extern ip_main_t ip_main;
131
132 clib_error_t *
133 ip_main_init (vlib_main_t * vm);
134
135 static inline ip_protocol_info_t *
136 ip_get_protocol_info (ip_main_t * im, u32 protocol)
137 {
138   uword * p;
139
140   p = hash_get (im->protocol_info_by_protocol, protocol);
141   return p ? vec_elt_at_index (im->protocol_infos, p[0]) : 0;
142 }
143
144 static inline tcp_udp_port_info_t *
145 ip_get_tcp_udp_port_info (ip_main_t * im, u32 port)
146 {
147   uword * p;
148
149   p = hash_get (im->port_info_by_port, port);
150   return p ? vec_elt_at_index (im->port_infos, p[0]) : 0;
151 }
152       
153 always_inline ip_csum_t
154 ip_incremental_checksum_buffer (vlib_main_t * vm, vlib_buffer_t * first_buffer,
155                                 u32 first_buffer_offset,
156                                 u32 n_bytes_to_checksum,
157                                 ip_csum_t sum)
158 #if DPDK > 0
159 {
160   u32 n_bytes_left = n_bytes_to_checksum;
161   struct rte_mbuf * mb = rte_mbuf_from_vlib_buffer(first_buffer);
162   u8 nb_segs = mb->nb_segs;
163   ASSERT(mb->data_len >= first_buffer_offset);
164   void * h;
165   u32 n;
166   
167   n = clib_min (n_bytes_left, mb->data_len);
168   h = vlib_buffer_get_current (first_buffer) + first_buffer_offset;
169   while (n_bytes_left)
170     {
171       sum = ip_incremental_checksum (sum, h, n);
172       n_bytes_left -= n;
173       nb_segs--;
174       mb = mb->next;
175       if ((nb_segs == 0) || (mb == 0))
176         break;
177
178       n = clib_min (n_bytes_left, mb->data_len);
179       h = rte_ctrlmbuf_data(mb);
180     }
181  
182   ASSERT(n_bytes_left == 0);
183   ASSERT(nb_segs == 0);
184   return sum;
185 }
186 #else
187 {
188   vlib_buffer_t * b = first_buffer;
189   u32 n_bytes_left = n_bytes_to_checksum;
190   ASSERT (b->current_length >= first_buffer_offset);
191   void * h;
192   u32 n;
193
194   n = clib_min (n_bytes_left, b->current_length);
195   h = vlib_buffer_get_current (b) + first_buffer_offset;
196   sum = ip_incremental_checksum (sum, h, n);
197   if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT))
198     {
199       while (1)
200         {
201           n_bytes_left -= n;
202           if (n_bytes_left == 0)
203             break;
204           b = vlib_get_buffer (vm, b->next_buffer);
205           n = clib_min (n_bytes_left, b->current_length);
206           h = vlib_buffer_get_current (b);
207           sum = ip_incremental_checksum (sum, h, n);
208         }
209     }
210
211   return sum;
212 }
213 #endif /* DPDK */
214
215 void ip_del_all_interface_addresses (vlib_main_t *vm, u32 sw_if_index);
216
217 extern vlib_node_registration_t ip4_inacl_node;
218 extern vlib_node_registration_t ip6_inacl_node;
219
220 #endif /* included_ip_main_h */