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/l3_binding.hpp" |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 17 | #include "vom/l3_binding_cmds.hpp" |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 18 | |
| 19 | namespace VOM { |
Neale Ranns | fd92060 | 2017-11-23 12:15:00 -0800 | [diff] [blame] | 20 | singular_db<l3_binding::key_t, l3_binding> l3_binding::m_db; |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 21 | |
| 22 | l3_binding::event_handler l3_binding::m_evh; |
| 23 | |
| 24 | /** |
| 25 | * Construct a new object matching the desried state |
| 26 | */ |
| 27 | l3_binding::l3_binding(const interface& itf, const route::prefix_t& pfx) |
| 28 | : m_itf(itf.singular()) |
| 29 | , m_pfx(pfx) |
Neale Ranns | 8ac4ce8 | 2017-11-17 05:08:55 -0800 | [diff] [blame] | 30 | , m_binding(true, rc_t::NOOP) |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 31 | { |
| 32 | } |
| 33 | |
| 34 | l3_binding::l3_binding(const l3_binding& o) |
| 35 | : m_itf(o.m_itf) |
| 36 | , m_pfx(o.m_pfx) |
Neale Ranns | 8ac4ce8 | 2017-11-17 05:08:55 -0800 | [diff] [blame] | 37 | , m_binding(o.m_binding) |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 38 | { |
| 39 | } |
| 40 | |
| 41 | l3_binding::~l3_binding() |
| 42 | { |
| 43 | sweep(); |
| 44 | |
| 45 | // not in the DB anymore. |
Neale Ranns | fd92060 | 2017-11-23 12:15:00 -0800 | [diff] [blame] | 46 | m_db.release(key(), this); |
| 47 | } |
| 48 | |
| 49 | bool |
| 50 | l3_binding::operator==(const l3_binding& l) const |
| 51 | { |
| 52 | return ((m_pfx == l.m_pfx) && (*m_itf == *l.m_itf)); |
| 53 | } |
| 54 | |
| 55 | const l3_binding::key_t |
| 56 | l3_binding::key() const |
| 57 | { |
| 58 | return (make_pair(m_itf->key(), m_pfx)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void |
| 62 | l3_binding::sweep() |
| 63 | { |
| 64 | if (m_binding) { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 65 | HW::enqueue( |
| 66 | new l3_binding_cmds::unbind_cmd(m_binding, m_itf->handle(), m_pfx)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 67 | } |
| 68 | HW::write(); |
| 69 | } |
| 70 | |
| 71 | void |
| 72 | l3_binding::replay() |
| 73 | { |
| 74 | if (m_binding) { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 75 | HW::enqueue( |
| 76 | new l3_binding_cmds::bind_cmd(m_binding, m_itf->handle(), m_pfx)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
| 80 | const route::prefix_t& |
| 81 | l3_binding::prefix() const |
| 82 | { |
| 83 | return (m_pfx); |
| 84 | } |
| 85 | |
Neale Ranns | 352ea0c | 2017-11-14 11:04:28 -0800 | [diff] [blame] | 86 | const interface& |
| 87 | l3_binding::itf() const |
| 88 | { |
| 89 | return (*m_itf); |
| 90 | } |
| 91 | |
| 92 | l3_binding::const_iterator_t |
| 93 | l3_binding::cbegin() |
| 94 | { |
| 95 | return m_db.cbegin(); |
| 96 | } |
| 97 | |
| 98 | l3_binding::const_iterator_t |
| 99 | l3_binding::cend() |
| 100 | { |
| 101 | return m_db.cend(); |
| 102 | } |
| 103 | |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 104 | std::string |
| 105 | l3_binding::to_string() const |
| 106 | { |
| 107 | std::ostringstream s; |
Neale Ranns | fd92060 | 2017-11-23 12:15:00 -0800 | [diff] [blame] | 108 | s << "L3-binding:[" << m_itf->to_string() << " prefix:" << m_pfx.to_string() |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 109 | << " " << m_binding.to_string() << "]"; |
| 110 | |
| 111 | return (s.str()); |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | l3_binding::update(const l3_binding& desired) |
| 116 | { |
| 117 | /* |
Neale Ranns | 352ea0c | 2017-11-14 11:04:28 -0800 | [diff] [blame] | 118 | * no updates for the binding. chaning the interface or the prefix is a change |
| 119 | * to the |
| 120 | * key, hence a new object |
| 121 | */ |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 122 | if (!m_binding) { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 123 | HW::enqueue( |
| 124 | new l3_binding_cmds::bind_cmd(m_binding, m_itf->handle(), m_pfx)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
| 128 | std::shared_ptr<l3_binding> |
| 129 | l3_binding::find_or_add(const l3_binding& temp) |
| 130 | { |
Neale Ranns | fd92060 | 2017-11-23 12:15:00 -0800 | [diff] [blame] | 131 | return (m_db.find_or_add(temp.key(), temp)); |
| 132 | } |
| 133 | |
| 134 | std::shared_ptr<l3_binding> |
| 135 | l3_binding::find(const key_t& k) |
| 136 | { |
| 137 | return (m_db.find(k)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | std::shared_ptr<l3_binding> |
| 141 | l3_binding::singular() const |
| 142 | { |
| 143 | return find_or_add(*this); |
| 144 | } |
| 145 | |
| 146 | void |
| 147 | l3_binding::dump(std::ostream& os) |
| 148 | { |
| 149 | m_db.dump(os); |
| 150 | } |
| 151 | |
| 152 | std::ostream& |
Neale Ranns | fd92060 | 2017-11-23 12:15:00 -0800 | [diff] [blame] | 153 | operator<<(std::ostream& os, const l3_binding::key_t& key) |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 154 | { |
| 155 | os << "[" << key.first << ", " << key.second << "]"; |
| 156 | |
| 157 | return (os); |
| 158 | } |
| 159 | |
| 160 | std::deque<std::shared_ptr<l3_binding>> |
| 161 | l3_binding::find(const interface& i) |
| 162 | { |
| 163 | /* |
| 164 | * Loop throught the entire map looking for matching interface. |
| 165 | * not the most efficient algorithm, but it will do for now. The |
| 166 | * number of L3 configs is low and this is only called during bootup |
| 167 | */ |
| 168 | std::deque<std::shared_ptr<l3_binding>> l3s; |
| 169 | |
| 170 | auto it = m_db.cbegin(); |
| 171 | |
| 172 | while (it != m_db.cend()) { |
| 173 | /* |
Neale Ranns | 1d78155 | 2017-11-27 04:52:35 -0800 | [diff] [blame] | 174 | * The key in the DB is a pair of the interface's name and prefix. |
| 175 | * If the keys match, save the L3-config |
| 176 | */ |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 177 | auto key = it->first; |
| 178 | |
| 179 | if (i.key() == key.first) { |
| 180 | l3s.push_back(it->second.lock()); |
| 181 | } |
| 182 | |
| 183 | ++it; |
| 184 | } |
| 185 | |
| 186 | return (l3s); |
| 187 | } |
| 188 | |
| 189 | l3_binding::event_handler::event_handler() |
| 190 | { |
| 191 | OM::register_listener(this); |
| 192 | inspect::register_handler({ "l3" }, "L3 bindings", this); |
| 193 | } |
| 194 | |
| 195 | void |
| 196 | l3_binding::event_handler::handle_replay() |
| 197 | { |
| 198 | m_db.replay(); |
| 199 | } |
| 200 | |
| 201 | void |
| 202 | l3_binding::event_handler::handle_populate(const client_db::key_t& key) |
| 203 | { |
| 204 | /** |
Neale Ranns | 1d78155 | 2017-11-27 04:52:35 -0800 | [diff] [blame] | 205 | * This is done while populating the interfaces |
| 206 | */ |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | dependency_t |
| 210 | l3_binding::event_handler::order() const |
| 211 | { |
| 212 | return (dependency_t::BINDING); |
| 213 | } |
| 214 | |
| 215 | void |
| 216 | l3_binding::event_handler::show(std::ostream& os) |
| 217 | { |
| 218 | m_db.dump(os); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | /* |
| 223 | * fd.io coding-style-patch-verification: ON |
| 224 | * |
| 225 | * Local Variables: |
| 226 | * eval: (c-set-style "mozilla") |
| 227 | * End: |
| 228 | */ |