Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 <boost/algorithm/string.hpp> |
| 17 | |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 18 | #include "vom/interface_factory.hpp" |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 19 | #include "vom/sub_interface.hpp" |
| 20 | #include "vom/tap_interface.hpp" |
| 21 | |
| 22 | namespace VOM { |
Neale Ranns | a2ee029 | 2017-11-28 22:29:13 -0800 | [diff] [blame] | 23 | std::shared_ptr<interface> |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 24 | interface_factory::new_interface(const vapi_payload_sw_interface_details& vd) |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 25 | { |
Neale Ranns | a2ee029 | 2017-11-28 22:29:13 -0800 | [diff] [blame] | 26 | std::shared_ptr<interface> sp; |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 27 | |
| 28 | /** |
Neale Ranns | a2ee029 | 2017-11-28 22:29:13 -0800 | [diff] [blame] | 29 | * Determine the interface type from the name and VLAN attributes |
| 30 | */ |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 31 | std::string name = reinterpret_cast<const char*>(vd.interface_name); |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 32 | interface::type_t type = interface::type_t::from_string(name); |
| 33 | interface::admin_state_t state = |
| 34 | interface::admin_state_t::from_int(vd.link_up_down); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 35 | handle_t hdl(vd.sw_if_index); |
| 36 | l2_address_t l2_address(vd.l2_address, vd.l2_address_length); |
Neale Ranns | 4ef4226 | 2018-02-20 08:10:44 -0800 | [diff] [blame] | 37 | std::string tag = ""; |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 38 | |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 39 | if (interface::type_t::AFPACKET == type) { |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 40 | /* |
Neale Ranns | a2ee029 | 2017-11-28 22:29:13 -0800 | [diff] [blame] | 41 | * need to strip VPP's "host-" prefix from the interface name |
| 42 | */ |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 43 | name = name.substr(5); |
| 44 | } |
| 45 | /** |
Neale Ranns | a2ee029 | 2017-11-28 22:29:13 -0800 | [diff] [blame] | 46 | * if the tag is set, then we wrote that to specify a name to make |
| 47 | * the interface type more specific |
| 48 | */ |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 49 | if (vd.tag[0] != 0) { |
Neale Ranns | 4ef4226 | 2018-02-20 08:10:44 -0800 | [diff] [blame] | 50 | tag = std::string(reinterpret_cast<const char*>(vd.tag)); |
| 51 | } |
| 52 | |
| 53 | if (!tag.empty() && interface::type_t::LOOPBACK == type) { |
| 54 | name = tag; |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 55 | type = interface::type_t::from_string(name); |
| 56 | } |
| 57 | |
| 58 | /* |
Neale Ranns | a2ee029 | 2017-11-28 22:29:13 -0800 | [diff] [blame] | 59 | * pull out the other special cases |
| 60 | */ |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 61 | if (interface::type_t::TAP == type) { |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 62 | /* |
Neale Ranns | a2ee029 | 2017-11-28 22:29:13 -0800 | [diff] [blame] | 63 | * TAP interface |
| 64 | */ |
| 65 | sp = tap_interface(name, state, route::prefix_t()).singular(); |
Neale Ranns | 4ef4226 | 2018-02-20 08:10:44 -0800 | [diff] [blame] | 66 | if (sp && !tag.empty()) |
| 67 | sp->set(tag); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 68 | } else if ((name.find(".") != std::string::npos) && (0 != vd.sub_id)) { |
| 69 | /* |
Neale Ranns | a2ee029 | 2017-11-28 22:29:13 -0800 | [diff] [blame] | 70 | * Sub-interface |
| 71 | * split the name into the parent and VLAN |
| 72 | */ |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 73 | std::vector<std::string> parts; |
| 74 | boost::split(parts, name, boost::is_any_of(".")); |
| 75 | |
Neale Ranns | 4ef4226 | 2018-02-20 08:10:44 -0800 | [diff] [blame] | 76 | interface parent(parts[0], type, state, tag); |
Neale Ranns | a2ee029 | 2017-11-28 22:29:13 -0800 | [diff] [blame] | 77 | sp = sub_interface(parent, state, vd.sub_id).singular(); |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 78 | } else if (interface::type_t::VXLAN == type) { |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 79 | /* |
Neale Ranns | a2ee029 | 2017-11-28 22:29:13 -0800 | [diff] [blame] | 80 | * there's not enough information in a SW interface record to |
Neale Ranns | 4ef4226 | 2018-02-20 08:10:44 -0800 | [diff] [blame] | 81 | * construct a VXLAN tunnel. so skip it. They have |
| 82 | * their own dump routines |
Neale Ranns | a2ee029 | 2017-11-28 22:29:13 -0800 | [diff] [blame] | 83 | */ |
Neale Ranns | 4ef4226 | 2018-02-20 08:10:44 -0800 | [diff] [blame] | 84 | } else if (interface::type_t::VHOST == type) { |
| 85 | /* |
| 86 | * vhost interfaces already exist in db, look for it using |
| 87 | * sw_if_index |
| 88 | */ |
| 89 | sp = interface::find(hdl); |
| 90 | if (sp) { |
| 91 | sp->set(state); |
| 92 | sp->set(l2_address); |
| 93 | if (!tag.empty()) |
| 94 | sp->set(tag); |
| 95 | } |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 96 | } else { |
Neale Ranns | 4ef4226 | 2018-02-20 08:10:44 -0800 | [diff] [blame] | 97 | sp = interface(name, type, state, tag).singular(); |
Neale Ranns | a2ee029 | 2017-11-28 22:29:13 -0800 | [diff] [blame] | 98 | sp->set(l2_address); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Neale Ranns | a2ee029 | 2017-11-28 22:29:13 -0800 | [diff] [blame] | 101 | /* |
| 102 | * set the handle on the intterface - N.B. this is the sigluar instance |
| 103 | * not a stack local. |
| 104 | */ |
Neale Ranns | 4ef4226 | 2018-02-20 08:10:44 -0800 | [diff] [blame] | 105 | if (sp) |
| 106 | sp->set(hdl); |
Neale Ranns | a2ee029 | 2017-11-28 22:29:13 -0800 | [diff] [blame] | 107 | |
| 108 | return (sp); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 109 | } |
Neale Ranns | 4ef4226 | 2018-02-20 08:10:44 -0800 | [diff] [blame] | 110 | |
| 111 | std::shared_ptr<interface> |
| 112 | interface_factory::new_vhost_user_interface( |
| 113 | const vapi_payload_sw_interface_vhost_user_details& vd) |
| 114 | { |
| 115 | std::shared_ptr<interface> sp; |
| 116 | std::string name = reinterpret_cast<const char*>(vd.sock_filename); |
| 117 | interface::type_t type = interface::type_t::from_string(name); |
| 118 | handle_t hdl(vd.sw_if_index); |
| 119 | |
| 120 | sp = interface(name, type, interface::admin_state_t::DOWN).singular(); |
| 121 | sp->set(hdl); |
| 122 | return (sp); |
| 123 | } |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 124 | }; // namespace VOM |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 125 | |
| 126 | /* |
| 127 | * fd.io coding-style-patch-verification: ON |
| 128 | * |
| 129 | * Local Variables: |
| 130 | * eval: (c-set-style "mozilla") |
| 131 | * End: |
| 132 | */ |