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 "vom/route_domain.hpp" |
| 17 | #include "vom/cmd.hpp" |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 18 | #include "vom/route_domain_cmds.hpp" |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 19 | |
| 20 | namespace VOM { |
Neale Ranns | 10e7a9f | 2017-11-14 08:40:43 -0800 | [diff] [blame] | 21 | |
| 22 | route_domain::event_handler route_domain::m_evh; |
| 23 | |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 24 | /** |
| 25 | * A DB of al the interfaces, key on the name |
| 26 | */ |
| 27 | singular_db<route::table_id_t, route_domain> route_domain::m_db; |
| 28 | |
| 29 | /** |
| 30 | * Construct a new object matching the desried state |
| 31 | */ |
| 32 | route_domain::route_domain(route::table_id_t id) |
| 33 | : m_hw_v4(true) |
| 34 | , m_hw_v6(true) |
| 35 | , m_table_id(id) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | route_domain::route_domain(const route_domain& o) |
| 40 | : m_hw_v4(o.m_hw_v4) |
| 41 | , m_hw_v6(o.m_hw_v6) |
| 42 | , m_table_id(o.m_table_id) |
| 43 | { |
| 44 | } |
| 45 | |
Neale Ranns | fd92060 | 2017-11-23 12:15:00 -0800 | [diff] [blame] | 46 | bool |
| 47 | route_domain::operator==(const route_domain& r) const |
| 48 | { |
| 49 | return (m_table_id == r.m_table_id); |
| 50 | } |
| 51 | |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 52 | route::table_id_t |
| 53 | route_domain::table_id() const |
| 54 | { |
| 55 | return (m_table_id); |
| 56 | } |
| 57 | |
| 58 | route_domain::key_t |
| 59 | route_domain::key() const |
| 60 | { |
| 61 | return (table_id()); |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | route_domain::sweep() |
| 66 | { |
| 67 | if (m_hw_v4) { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 68 | HW::enqueue( |
| 69 | new route_domain_cmds::delete_cmd(m_hw_v4, l3_proto_t::IPV4, m_table_id)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 70 | } |
| 71 | if (m_hw_v6) { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 72 | HW::enqueue( |
| 73 | new route_domain_cmds::delete_cmd(m_hw_v6, l3_proto_t::IPV6, m_table_id)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 74 | } |
| 75 | HW::write(); |
| 76 | } |
| 77 | |
| 78 | void |
| 79 | route_domain::replay() |
| 80 | { |
| 81 | if (m_hw_v4) { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 82 | HW::enqueue( |
| 83 | new route_domain_cmds::create_cmd(m_hw_v4, l3_proto_t::IPV4, m_table_id)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 84 | } |
| 85 | if (m_hw_v6) { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 86 | HW::enqueue( |
| 87 | new route_domain_cmds::create_cmd(m_hw_v6, l3_proto_t::IPV6, m_table_id)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
| 91 | route_domain::~route_domain() |
| 92 | { |
| 93 | sweep(); |
| 94 | |
| 95 | // not in the DB anymore. |
| 96 | m_db.release(m_table_id, this); |
| 97 | } |
| 98 | |
| 99 | std::string |
| 100 | route_domain::to_string() const |
| 101 | { |
| 102 | std::ostringstream s; |
| 103 | s << "route-domain:[" |
Neale Ranns | d3464b5 | 2017-12-07 08:48:02 -0800 | [diff] [blame] | 104 | << "table-id:" << m_table_id << " v4:" << m_hw_v4.to_string() |
| 105 | << " v6:" << m_hw_v6.to_string() << "]"; |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 106 | |
| 107 | return (s.str()); |
| 108 | } |
| 109 | |
| 110 | std::shared_ptr<route_domain> |
Neale Ranns | 7016b00 | 2018-01-25 07:28:19 -0800 | [diff] [blame] | 111 | route_domain::find(const key_t& k) |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 112 | { |
Neale Ranns | 7016b00 | 2018-01-25 07:28:19 -0800 | [diff] [blame] | 113 | return (m_db.find(k)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void |
| 117 | route_domain::update(const route_domain& desired) |
| 118 | { |
| 119 | /* |
| 120 | * create the table if it is not yet created |
| 121 | */ |
| 122 | if (rc_t::OK != m_hw_v4.rc()) { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 123 | HW::enqueue( |
| 124 | new route_domain_cmds::create_cmd(m_hw_v4, l3_proto_t::IPV4, m_table_id)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 125 | } |
| 126 | if (rc_t::OK != m_hw_v6.rc()) { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 127 | HW::enqueue( |
| 128 | new route_domain_cmds::create_cmd(m_hw_v6, l3_proto_t::IPV6, m_table_id)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
| 132 | std::shared_ptr<route_domain> |
| 133 | route_domain::get_default() |
| 134 | { |
| 135 | route_domain rd(route::DEFAULT_TABLE); |
| 136 | |
| 137 | return (find_or_add(rd)); |
| 138 | } |
| 139 | |
| 140 | std::shared_ptr<route_domain> |
| 141 | route_domain::find_or_add(const route_domain& temp) |
| 142 | { |
| 143 | return (m_db.find_or_add(temp.m_table_id, temp)); |
| 144 | } |
| 145 | |
| 146 | std::shared_ptr<route_domain> |
| 147 | route_domain::singular() const |
| 148 | { |
| 149 | return find_or_add(*this); |
| 150 | } |
| 151 | |
| 152 | void |
| 153 | route_domain::dump(std::ostream& os) |
| 154 | { |
| 155 | m_db.dump(os); |
| 156 | } |
Neale Ranns | 10e7a9f | 2017-11-14 08:40:43 -0800 | [diff] [blame] | 157 | |
| 158 | void |
| 159 | route_domain::event_handler::handle_populate(const client_db::key_t& key) |
| 160 | { |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 161 | } |
Neale Ranns | 10e7a9f | 2017-11-14 08:40:43 -0800 | [diff] [blame] | 162 | |
| 163 | route_domain::event_handler::event_handler() |
| 164 | { |
| 165 | OM::register_listener(this); |
| 166 | inspect::register_handler({ "rd", "route-domain" }, "Route Domains", this); |
| 167 | } |
| 168 | |
| 169 | void |
| 170 | route_domain::event_handler::handle_replay() |
| 171 | { |
| 172 | m_db.replay(); |
| 173 | } |
| 174 | |
| 175 | dependency_t |
| 176 | route_domain::event_handler::order() const |
| 177 | { |
| 178 | return (dependency_t::TABLE); |
| 179 | } |
| 180 | |
| 181 | void |
| 182 | route_domain::event_handler::show(std::ostream& os) |
| 183 | { |
| 184 | m_db.dump(os); |
| 185 | } |
| 186 | |
| 187 | }; // namespace VOPM |
| 188 | |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 189 | /* |
| 190 | * fd.io coding-style-patch-verification: ON |
| 191 | * |
| 192 | * Local Variables: |
| 193 | * eval: (c-set-style "mozilla") |
| 194 | * End: |
| 195 | */ |