fib: fib api updates
[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 }; // namespace VOM
127    /*
128     * fd.io coding-style-patch-verification: ON
129     *
130     * Local Variables:
131     * eval: (c-set-style "mozilla")
132     * End:
133     */