vlib: prevent some signals from being executed on workers
[vpp.git] / src / vnet / tunnel / tunnel_types_api.c
1 /*
2  * tunnel_api.c - tunnel api
3  *
4  * Copyright (c) 2018 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 #include <vnet/api_errno.h>
19 #include <vnet/tunnel/tunnel_types_api.h>
20 #include <vnet/ip/ip_types_api.h>
21 #include <vnet/fib/fib_table.h>
22
23 #include <vnet/tunnel/tunnel_types.api_enum.h>
24 #include <vnet/tunnel/tunnel_types.api_types.h>
25
26
27 STATIC_ASSERT (sizeof (vl_api_tunnel_encap_decap_flags_t) ==
28                sizeof (tunnel_encap_decap_flags_t),
29                "tunnel API and internal flags enum size differ");
30 STATIC_ASSERT (sizeof (vl_api_tunnel_flags_t) == sizeof (tunnel_flags_t),
31                "tunnel API and internal flags enum size differ");
32
33 int
34 tunnel_encap_decap_flags_decode (vl_api_tunnel_encap_decap_flags_t f,
35                                  tunnel_encap_decap_flags_t * o)
36 {
37   if (f & ~TUNNEL_ENCAP_DECAP_FLAG_MASK)
38     /* unknown flags set */
39     return (VNET_API_ERROR_INVALID_VALUE_2);
40
41   *o = (tunnel_encap_decap_flags_t) f;
42   return (0);
43 }
44
45 vl_api_tunnel_encap_decap_flags_t
46 tunnel_encap_decap_flags_encode (tunnel_encap_decap_flags_t f)
47 {
48   return ((vl_api_tunnel_encap_decap_flags_t) f);
49 }
50
51 int
52 tunnel_flags_decode (vl_api_tunnel_flags_t f, tunnel_flags_t *o)
53 {
54   if (f & ~TUNNEL_FLAG_MASK)
55     /* unknown flags set */
56     return (VNET_API_ERROR_INVALID_VALUE_2);
57
58   *o = (tunnel_flags_t) f;
59   return (0);
60 }
61
62 vl_api_tunnel_flags_t
63 tunnel_flags_encode (tunnel_flags_t in)
64 {
65   vl_api_tunnel_flags_t out = 0;
66
67   if (in & TUNNEL_FLAG_TRACK_MTU)
68     out |= TUNNEL_API_FLAG_TRACK_MTU;
69
70   return (out);
71 }
72
73 int
74 tunnel_mode_decode (vl_api_tunnel_mode_t in, tunnel_mode_t * out)
75 {
76   switch (in)
77     {
78 #define _(n, v)                                       \
79       case TUNNEL_API_MODE_##n:                       \
80         *out = TUNNEL_MODE_##n;                       \
81         return (0);
82       foreach_tunnel_mode
83 #undef _
84     }
85
86   return (VNET_API_ERROR_INVALID_VALUE_2);
87 }
88
89 vl_api_tunnel_mode_t
90 tunnel_mode_encode (tunnel_mode_t in)
91 {
92   vl_api_tunnel_mode_t out = TUNNEL_API_MODE_P2P;
93
94   switch (in)
95     {
96 #define _(n, v)                                       \
97       case TUNNEL_MODE_##n:                           \
98         out = TUNNEL_API_MODE_##n;                    \
99         break;
100       foreach_tunnel_mode
101 #undef _
102     }
103
104   return (out);
105 }
106
107 int
108 tunnel_decode (const vl_api_tunnel_t *in, tunnel_t *out)
109 {
110   int rv;
111
112   ip_address_decode2 (&in->src, &out->t_src);
113   ip_address_decode2 (&in->dst, &out->t_dst);
114
115   if (ip_addr_version (&out->t_src) != ip_addr_version (&out->t_dst))
116     return (VNET_API_ERROR_INVALID_PROTOCOL);
117
118   if (0 == ip_address_cmp (&out->t_src, &out->t_dst))
119     return (VNET_API_ERROR_SAME_SRC_DST);
120
121   rv = tunnel_encap_decap_flags_decode (in->encap_decap_flags,
122                                         &out->t_encap_decap_flags);
123
124   if (rv)
125     return (rv);
126
127   rv = tunnel_mode_decode (in->mode, &out->t_mode);
128
129   if (rv)
130     return (rv);
131
132   rv = tunnel_flags_decode (in->flags, &out->t_flags);
133
134   if (rv)
135     return (rv);
136
137   out->t_table_id = clib_net_to_host_u32 (in->table_id);
138   out->t_fib_index = fib_table_find (
139     ip_address_family_to_fib_proto (ip_addr_version (&out->t_dst)),
140     out->t_table_id);
141
142   if (~0 == out->t_fib_index)
143     return (VNET_API_ERROR_NO_SUCH_FIB);
144
145   out->t_dscp = ip_dscp_decode (in->dscp);
146   out->t_hop_limit = in->hop_limit;
147
148   return (0);
149 }
150
151 void
152 tunnel_encode (const tunnel_t *in, vl_api_tunnel_t *out)
153 {
154   ip_address_encode2 (&in->t_src, &out->src);
155   ip_address_encode2 (&in->t_dst, &out->dst);
156
157   out->encap_decap_flags =
158     tunnel_encap_decap_flags_encode (in->t_encap_decap_flags);
159   out->mode = tunnel_mode_encode (in->t_mode);
160   out->flags = tunnel_flags_encode (in->t_flags);
161   out->table_id = clib_host_to_net_u32 (in->t_table_id);
162   out->dscp = ip_dscp_encode (in->t_dscp);
163   out->hop_limit = in->t_hop_limit;
164 }
165
166 /*
167  * fd.io coding-style-patch-verification: ON
168  *
169  * Local Variables:
170  * eval: (c-set-style "gnu")
171  * End:
172  */