Punt: socket register for exception dispatched/punted packets based on reason
[vpp.git] / src / vnet / ip / ip_types_api.c
1 /*
2  * Copyright (c) 2018 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 <vlibapi/api_types.h>
17 #include <vnet/ip/ip_types_api.h>
18
19 #define vl_typedefs             /* define message structures */
20 #include <vnet/vnet_all_api_h.h>
21 #undef vl_typedefs
22
23 #define vl_endianfun            /* define message structures */
24 #include <vnet/vnet_all_api_h.h>
25 #undef vl_endianfun
26
27 /* instantiate all the print functions we know about */
28 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
29 #define vl_printfun
30 #include <vnet/vnet_all_api_h.h>
31 #undef vl_printfun
32
33 int
34 ip_address_family_decode (int _af, ip_address_family_t * out)
35 {
36   vl_api_address_family_t af = clib_host_to_net_u32 (_af);
37
38   switch (af)
39     {
40     case ADDRESS_IP4:
41       *out = AF_IP4;
42       return (0);
43     case ADDRESS_IP6:
44       *out = AF_IP6;
45       return (0);
46     }
47   return (-1);
48 }
49
50 int
51 ip_address_family_encode (ip_address_family_t af)
52 {
53   switch (af)
54     {
55     case AF_IP4:
56       return (clib_host_to_net_u32 (ADDRESS_IP4));
57     case AF_IP6:
58       return (clib_host_to_net_u32 (ADDRESS_IP6));
59     }
60
61   ASSERT (0);
62   return (clib_host_to_net_u32 (ADDRESS_IP4));
63 }
64
65 int
66 ip_proto_decode (int _ipp, ip_protocol_t * out)
67 {
68   vl_api_ip_proto_t ipp = clib_host_to_net_u32 (_ipp);
69
70   switch (ipp)
71     {
72     case IP_API_PROTO_TCP:
73       *out = IP_PROTOCOL_TCP;
74       return (0);
75     case IP_API_PROTO_UDP:
76       *out = IP_PROTOCOL_UDP;
77       return (0);
78     }
79   return (-1);
80 }
81
82 int
83 ip_proto_encode (ip_protocol_t ipp)
84 {
85   switch (ipp)
86     {
87     case IP_PROTOCOL_UDP:
88       return (clib_host_to_net_u32 (IP_API_PROTO_UDP));
89     case IP_PROTOCOL_TCP:
90       return (clib_host_to_net_u32 (IP_API_PROTO_TCP));
91     default:
92       break;
93     }
94
95   ASSERT (0);
96   return (clib_host_to_net_u32 (IP_API_PROTO_TCP));
97 }
98
99 void
100 ip6_address_encode (const ip6_address_t * in, vl_api_ip6_address_t out)
101 {
102   clib_memcpy (out, in, sizeof (*in));
103 }
104
105 void
106 ip6_address_decode (const vl_api_ip6_address_t in, ip6_address_t * out)
107 {
108   clib_memcpy (out, in, sizeof (*out));
109 }
110
111 void
112 ip4_address_encode (const ip4_address_t * in, vl_api_ip4_address_t out)
113 {
114   clib_memcpy (out, in, sizeof (*in));
115 }
116
117 void
118 ip4_address_decode (const vl_api_ip4_address_t in, ip4_address_t * out)
119 {
120   clib_memcpy (out, in, sizeof (*out));
121 }
122
123 static ip46_type_t
124 ip_address_union_decode (const vl_api_address_union_t * in,
125                          vl_api_address_family_t af, ip46_address_t * out)
126 {
127   ip46_type_t type;
128
129   switch (clib_net_to_host_u32 (af))
130     {
131     case ADDRESS_IP4:
132       clib_memset (out, 0, sizeof (*out));
133       clib_memcpy (&out->ip4, &in->ip4, sizeof (out->ip4));
134       type = IP46_TYPE_IP4;
135       break;
136     case ADDRESS_IP6:
137       clib_memcpy (&out->ip6, &in->ip6, sizeof (out->ip6));
138       type = IP46_TYPE_IP6;
139       break;
140     default:
141       ASSERT (!"Unkown address family in API address type");
142       type = IP46_TYPE_ANY;
143       break;
144     }
145
146   return type;
147 }
148
149 ip46_type_t
150 ip_address_decode (const vl_api_address_t * in, ip46_address_t * out)
151 {
152   return (ip_address_union_decode (&in->un, in->af, out));
153 }
154
155 static void
156 ip_address_union_encode (const ip46_address_t * in,
157                          vl_api_address_family_t af,
158                          vl_api_address_union_t * out)
159 {
160   if (ADDRESS_IP6 == clib_net_to_host_u32 (af))
161     ip6_address_encode (&in->ip6, out->ip6);
162   else
163     ip4_address_encode (&in->ip4, out->ip4);
164 }
165
166 void
167 ip_address_encode (const ip46_address_t * in,
168                    ip46_type_t type, vl_api_address_t * out)
169 {
170   switch (type)
171     {
172     case IP46_TYPE_IP4:
173       out->af = clib_net_to_host_u32 (ADDRESS_IP4);
174       break;
175     case IP46_TYPE_IP6:
176       out->af = clib_net_to_host_u32 (ADDRESS_IP6);
177       break;
178     case IP46_TYPE_ANY:
179       if (ip46_address_is_ip4 (in))
180         out->af = clib_net_to_host_u32 (ADDRESS_IP4);
181       else
182         out->af = clib_net_to_host_u32 (ADDRESS_IP6);
183       break;
184     }
185   ip_address_union_encode (in, out->af, &out->un);
186 }
187
188 void
189 ip_prefix_decode (const vl_api_prefix_t * in, fib_prefix_t * out)
190 {
191   switch (clib_net_to_host_u32 (in->address.af))
192     {
193     case ADDRESS_IP4:
194       out->fp_proto = FIB_PROTOCOL_IP4;
195       break;
196     case ADDRESS_IP6:
197       out->fp_proto = FIB_PROTOCOL_IP6;
198       break;
199     }
200   out->fp_len = in->address_length;
201   out->___fp___pad = 0;
202   ip_address_decode (&in->address, &out->fp_addr);
203 }
204
205 void
206 ip_prefix_encode (const fib_prefix_t * in, vl_api_prefix_t * out)
207 {
208   out->address_length = in->fp_len;
209   ip_address_encode (&in->fp_addr,
210                      fib_proto_to_ip46 (in->fp_proto), &out->address);
211 }
212
213 void
214 ip_mprefix_encode (const mfib_prefix_t * in, vl_api_mprefix_t * out)
215 {
216   out->af = (FIB_PROTOCOL_IP6 == in->fp_proto ? ADDRESS_IP6 : ADDRESS_IP4);
217   out->af = clib_host_to_net_u32 (out->af);
218   out->grp_address_length = clib_host_to_net_u16 (in->fp_len);
219
220   ip_address_union_encode (&in->fp_grp_addr, out->af, &out->grp_address);
221   ip_address_union_encode (&in->fp_src_addr, out->af, &out->src_address);
222 }
223
224 void
225 ip_mprefix_decode (const vl_api_mprefix_t * in, mfib_prefix_t * out)
226 {
227   out->fp_proto = (ADDRESS_IP6 == clib_net_to_host_u32 (in->af) ?
228                    FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
229   out->fp_len = clib_net_to_host_u16 (in->grp_address_length);
230
231   ip_address_union_decode (&in->grp_address, in->af, &out->fp_grp_addr);
232   ip_address_union_decode (&in->src_address, in->af, &out->fp_src_addr);
233 }
234
235 /*
236  * fd.io coding-style-patch-verification: ON
237  *
238  * Local Variables:
239  * eval: (c-set-style "gnu")
240  * End:
241  */