dhcp ip: DSCP settings for transmitted DHCP packets
[vpp.git] / extras / vom / vom / route_api_types.cpp
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 <vom/api_types.hpp>
17 #include <vom/route.hpp>
18 #include <vom/route_api_types.hpp>
19
20 namespace VOM {
21
22 const route::itf_flags_t&
23 from_api(vapi_enum_mfib_itf_flags val)
24 {
25   if (route::itf_flags_t::ACCEPT == val)
26     return route::itf_flags_t::ACCEPT;
27   else
28     return route::itf_flags_t::FORWARD;
29 }
30
31 vapi_enum_mfib_itf_flags
32 to_api(const route::itf_flags_t& in)
33 {
34   vapi_enum_mfib_itf_flags out = MFIB_API_ITF_FLAG_NONE;
35
36   if (route::itf_flags_t::ACCEPT & in)
37     out = static_cast<vapi_enum_mfib_itf_flags>(out | MFIB_API_ITF_FLAG_ACCEPT);
38   if (route::itf_flags_t::FORWARD & in)
39     out =
40       static_cast<vapi_enum_mfib_itf_flags>(out | MFIB_API_ITF_FLAG_FORWARD);
41
42   return (out);
43 }
44
45 void
46 to_api(const route::path& p, vapi_type_fib_path& payload)
47 {
48   payload.flags = FIB_API_PATH_FLAG_NONE;
49   payload.proto = to_api(p.nh_proto());
50   payload.sw_if_index = ~0;
51
52   if (route::path::flags_t::DVR & p.flags()) {
53     payload.type = FIB_API_PATH_TYPE_DVR;
54   } else if (route::path::special_t::STANDARD == p.type()) {
55     to_api(p.nh(), payload.nh.address);
56
57     if (p.rd()) {
58       payload.table_id = p.rd()->table_id();
59     }
60     if (p.itf()) {
61       payload.sw_if_index = p.itf()->handle().value();
62     }
63   } else if (route::path::special_t::DROP == p.type()) {
64     payload.type = FIB_API_PATH_TYPE_DROP;
65   } else if (route::path::special_t::UNREACH == p.type()) {
66     payload.type = FIB_API_PATH_TYPE_ICMP_UNREACH;
67   } else if (route::path::special_t::PROHIBIT == p.type()) {
68     payload.type = FIB_API_PATH_TYPE_ICMP_PROHIBIT;
69   } else if (route::path::special_t::LOCAL == p.type()) {
70     payload.type = FIB_API_PATH_TYPE_LOCAL;
71   }
72
73   payload.weight = p.weight();
74   payload.preference = p.preference();
75   payload.n_labels = 0;
76 }
77
78 route::path
79 from_api(const vapi_type_fib_path& p)
80 {
81   switch (p.type) {
82     case FIB_API_PATH_TYPE_DVR: {
83       std::shared_ptr<interface> itf = interface::find(p.sw_if_index);
84       if (!itf)
85         throw invalid_decode("fib-path deocde no interface:" +
86                              std::to_string(p.sw_if_index));
87
88       return (route::path(*itf, from_api(p.proto), route::path::flags_t::DVR,
89                           p.weight, p.preference));
90     }
91     case FIB_API_PATH_TYPE_NORMAL: {
92       boost::asio::ip::address address = from_api(p.nh.address, p.proto);
93       std::shared_ptr<interface> itf = interface::find(p.sw_if_index);
94       if (itf) {
95         return (route::path(address, *itf, p.weight, p.preference));
96       } else {
97         std::shared_ptr<route_domain> rd = route_domain::find(p.table_id);
98
99         if (!rd)
100           throw invalid_decode("fib-path deocde no route-domain:" +
101                                std::to_string(p.table_id));
102
103         return (route::path(*rd, address, p.weight, p.preference));
104       }
105     }
106     case FIB_API_PATH_TYPE_LOCAL:
107       return (route::path(route::path::special_t::LOCAL));
108     case FIB_API_PATH_TYPE_DROP:
109       return (route::path(route::path::special_t::DROP));
110     case FIB_API_PATH_TYPE_ICMP_UNREACH:
111       return (route::path(route::path::special_t::PROHIBIT));
112     case FIB_API_PATH_TYPE_ICMP_PROHIBIT:
113       return (route::path(route::path::special_t::UNREACH));
114
115     case FIB_API_PATH_TYPE_UDP_ENCAP:
116     case FIB_API_PATH_TYPE_BIER_IMP:
117     case FIB_API_PATH_TYPE_SOURCE_LOOKUP:
118     case FIB_API_PATH_TYPE_INTERFACE_RX:
119     case FIB_API_PATH_TYPE_CLASSIFY:
120       // not done yet
121       break;
122   }
123   return (route::path(route::path::special_t::DROP));
124 };
125
126 vapi_enum_ip_dscp
127 to_api(const ip_dscp_t& d)
128 {
129   return static_cast<vapi_enum_ip_dscp>((int)d);
130 }
131 const ip_dscp_t&
132 from_api(vapi_enum_ip_dscp d)
133 {
134   switch (d) {
135     case IP_API_DSCP_CS0:
136       return ip_dscp_t::DSCP_CS0;
137     case IP_API_DSCP_CS1:
138       return ip_dscp_t::DSCP_CS1;
139     case IP_API_DSCP_CS2:
140       return ip_dscp_t::DSCP_CS2;
141     case IP_API_DSCP_CS3:
142       return ip_dscp_t::DSCP_CS3;
143     case IP_API_DSCP_CS4:
144       return ip_dscp_t::DSCP_CS4;
145     case IP_API_DSCP_CS5:
146       return ip_dscp_t::DSCP_CS5;
147     case IP_API_DSCP_CS6:
148       return ip_dscp_t::DSCP_CS6;
149     case IP_API_DSCP_CS7:
150       return ip_dscp_t::DSCP_CS7;
151     case IP_API_DSCP_EF:
152       return ip_dscp_t::DSCP_EF;
153     case IP_API_DSCP_AF11:
154       return ip_dscp_t::DSCP_AF11;
155     case IP_API_DSCP_AF12:
156       return ip_dscp_t::DSCP_AF12;
157     case IP_API_DSCP_AF13:
158       return ip_dscp_t::DSCP_AF13;
159     case IP_API_DSCP_AF21:
160       return ip_dscp_t::DSCP_AF21;
161     case IP_API_DSCP_AF22:
162       return ip_dscp_t::DSCP_AF22;
163     case IP_API_DSCP_AF23:
164       return ip_dscp_t::DSCP_AF23;
165     case IP_API_DSCP_AF31:
166       return ip_dscp_t::DSCP_AF31;
167     case IP_API_DSCP_AF32:
168       return ip_dscp_t::DSCP_AF32;
169     case IP_API_DSCP_AF33:
170       return ip_dscp_t::DSCP_AF33;
171     case IP_API_DSCP_AF41:
172       return ip_dscp_t::DSCP_AF41;
173     case IP_API_DSCP_AF42:
174       return ip_dscp_t::DSCP_AF42;
175     case IP_API_DSCP_AF43:
176       return ip_dscp_t::DSCP_AF43;
177   }
178
179   return ip_dscp_t::DSCP_CS0;
180 }
181
182 }; // namespace VOM
183
184 /*
185  * fd.io coding-style-patch-verification: ON
186  *
187  * Local Variables:
188  * eval: (c-set-style "mozilla")
189  * End:
190  */