Mohsin Kazmi | ed76ee2 | 2018-03-02 12:31:37 +0100 | [diff] [blame] | 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/bond_interface.hpp" |
| 17 | #include "vom/bond_group_binding.hpp" |
| 18 | #include "vom/bond_group_binding_cmds.hpp" |
| 19 | #include "vom/bond_interface_cmds.hpp" |
| 20 | |
| 21 | namespace VOM { |
| 22 | /** |
| 23 | * Construct a new object matching the desried state |
| 24 | */ |
| 25 | bond_interface::bond_interface(const std::string& name, |
| 26 | admin_state_t state, |
| 27 | mode_t mode, |
| 28 | lb_t lb) |
| 29 | : interface(name, type_t::BOND, state) |
| 30 | , m_l2_address(l2_address_t::ZERO) |
| 31 | , m_mode(mode) |
| 32 | , m_lb(lb) |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | bond_interface::bond_interface(const std::string& name, |
| 37 | admin_state_t state, |
| 38 | const l2_address_t& l2_address, |
| 39 | mode_t mode, |
| 40 | lb_t lb) |
| 41 | : interface(name, type_t::BOND, state) |
| 42 | , m_l2_address(l2_address) |
| 43 | , m_mode(mode) |
| 44 | , m_lb(lb) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | bond_interface::~bond_interface() |
| 49 | { |
| 50 | sweep(); |
| 51 | release(); |
| 52 | } |
| 53 | |
| 54 | bond_interface::bond_interface(const bond_interface& o) |
| 55 | : interface(o) |
| 56 | , m_l2_address(o.m_l2_address) |
| 57 | , m_mode(o.m_mode) |
| 58 | , m_lb(o.m_lb) |
| 59 | { |
| 60 | } |
| 61 | |
| 62 | std::shared_ptr<bond_interface> |
| 63 | bond_interface::find(const handle_t& hdl) |
| 64 | { |
| 65 | return std::dynamic_pointer_cast<bond_interface>(interface::find(hdl)); |
| 66 | } |
| 67 | |
| 68 | void |
| 69 | bond_interface::set(bond_interface::mode_t mode) |
| 70 | { |
| 71 | m_mode = mode; |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | bond_interface::set(bond_interface::lb_t lb) |
| 76 | { |
| 77 | m_lb = lb; |
| 78 | } |
| 79 | |
| 80 | std::string |
| 81 | bond_interface::to_string() const |
| 82 | { |
| 83 | std::ostringstream s; |
| 84 | |
| 85 | s << this->interface::to_string() << " mode:" << m_mode.to_string() |
| 86 | << " lb:" << m_lb.to_string(); |
| 87 | |
| 88 | return (s.str()); |
| 89 | } |
| 90 | |
| 91 | std::queue<cmd*>& |
| 92 | bond_interface::mk_create_cmd(std::queue<cmd*>& q) |
| 93 | { |
| 94 | q.push(new bond_interface_cmds::create_cmd(m_hdl, name(), m_mode, m_lb, |
| 95 | m_l2_address)); |
| 96 | |
| 97 | return (q); |
| 98 | } |
| 99 | |
| 100 | std::queue<cmd*>& |
| 101 | bond_interface::mk_delete_cmd(std::queue<cmd*>& q) |
| 102 | { |
| 103 | q.push(new bond_interface_cmds::delete_cmd(m_hdl)); |
| 104 | |
| 105 | return (q); |
| 106 | } |
| 107 | |
| 108 | std::shared_ptr<bond_interface> |
| 109 | bond_interface::singular() const |
| 110 | { |
| 111 | return std::dynamic_pointer_cast<bond_interface>(singular_i()); |
| 112 | } |
| 113 | |
| 114 | std::shared_ptr<interface> |
| 115 | bond_interface::singular_i() const |
| 116 | { |
| 117 | return m_db.find_or_add(name(), *this); |
| 118 | } |
| 119 | |
| 120 | void |
| 121 | bond_interface::set(handle_t& handle) |
| 122 | { |
| 123 | this->interface::set(handle); |
| 124 | } |
| 125 | |
| 126 | const bond_interface::mode_t bond_interface::mode_t::ROUND_ROBIN(1, |
| 127 | "round-robin"); |
| 128 | const bond_interface::mode_t bond_interface::mode_t::ACTIVE_BACKUP( |
| 129 | 2, |
| 130 | "active-backup"); |
| 131 | const bond_interface::mode_t bond_interface::mode_t::XOR(3, "xor"); |
| 132 | const bond_interface::mode_t bond_interface::mode_t::BROADCAST(4, "broadcast"); |
| 133 | const bond_interface::mode_t bond_interface::mode_t::LACP(5, "lacp"); |
| 134 | const bond_interface::mode_t bond_interface::mode_t::UNSPECIFIED(0, |
| 135 | "unspecified"); |
| 136 | |
| 137 | const bond_interface::mode_t |
| 138 | bond_interface::mode_t::from_numeric_val(uint8_t numeric) |
| 139 | { |
| 140 | if (1 == numeric) { |
| 141 | return (bond_interface::mode_t::ROUND_ROBIN); |
| 142 | } |
| 143 | if (2 == numeric) { |
| 144 | return (bond_interface::mode_t::ACTIVE_BACKUP); |
| 145 | } |
| 146 | if (3 == numeric) { |
| 147 | return (bond_interface::mode_t::XOR); |
| 148 | } |
| 149 | if (4 == numeric) { |
| 150 | return (bond_interface::mode_t::BROADCAST); |
| 151 | } |
| 152 | if (5 == numeric) { |
| 153 | return (bond_interface::mode_t::LACP); |
| 154 | } |
| 155 | |
| 156 | return (bond_interface::mode_t::UNSPECIFIED); |
| 157 | } |
| 158 | |
| 159 | bond_interface::mode_t::mode_t(int v, const std::string& s) |
| 160 | : enum_base<bond_interface::mode_t>(v, s) |
| 161 | { |
| 162 | } |
| 163 | |
| 164 | const bond_interface::lb_t bond_interface::lb_t::L2(0, "l2"); |
| 165 | const bond_interface::lb_t bond_interface::lb_t::L34(1, "l34"); |
| 166 | const bond_interface::lb_t bond_interface::lb_t::L23(2, "l23"); |
| 167 | const bond_interface::lb_t bond_interface::lb_t::UNSPECIFIED(~0, "unspecified"); |
| 168 | |
| 169 | const bond_interface::lb_t |
| 170 | bond_interface::lb_t::from_numeric_val(uint8_t numeric) |
| 171 | { |
| 172 | if (0 == numeric) { |
| 173 | return (bond_interface::lb_t::L2); |
| 174 | } |
| 175 | if (1 == numeric) { |
| 176 | return (bond_interface::lb_t::L34); |
| 177 | } |
| 178 | if (2 == numeric) { |
| 179 | return (bond_interface::lb_t::L23); |
| 180 | } |
| 181 | |
| 182 | return (bond_interface::lb_t::UNSPECIFIED); |
| 183 | } |
| 184 | |
| 185 | bond_interface::lb_t::lb_t(int v, const std::string& s) |
| 186 | : enum_base<bond_interface::lb_t>(v, s) |
| 187 | { |
| 188 | } |
| 189 | }; // namespace VOM |
| 190 | |
| 191 | /* |
| 192 | * fd.io coding-style-patch-verification: ON |
| 193 | * |
| 194 | * Local Variables: |
| 195 | * eval: (c-set-style "mozilla") |
| 196 | * End: |
| 197 | */ |