blob: 32a00ad7f437ec1c32ad922493c098eb0888afad [file] [log] [blame]
Mohsin Kazmied76ee22018-03-02 12:31:37 +01001/*
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
21namespace VOM {
22/**
23 * Construct a new object matching the desried state
24 */
25bond_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
36bond_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
48bond_interface::~bond_interface()
49{
50 sweep();
51 release();
52}
53
54bond_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
62std::shared_ptr<bond_interface>
63bond_interface::find(const handle_t& hdl)
64{
65 return std::dynamic_pointer_cast<bond_interface>(interface::find(hdl));
66}
67
68void
69bond_interface::set(bond_interface::mode_t mode)
70{
71 m_mode = mode;
72}
73
74void
75bond_interface::set(bond_interface::lb_t lb)
76{
77 m_lb = lb;
78}
79
80std::string
81bond_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
91std::queue<cmd*>&
92bond_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
100std::queue<cmd*>&
101bond_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
108std::shared_ptr<bond_interface>
109bond_interface::singular() const
110{
111 return std::dynamic_pointer_cast<bond_interface>(singular_i());
112}
113
114std::shared_ptr<interface>
115bond_interface::singular_i() const
116{
117 return m_db.find_or_add(name(), *this);
118}
119
120void
121bond_interface::set(handle_t& handle)
122{
123 this->interface::set(handle);
124}
125
126const bond_interface::mode_t bond_interface::mode_t::ROUND_ROBIN(1,
127 "round-robin");
128const bond_interface::mode_t bond_interface::mode_t::ACTIVE_BACKUP(
129 2,
130 "active-backup");
131const bond_interface::mode_t bond_interface::mode_t::XOR(3, "xor");
132const bond_interface::mode_t bond_interface::mode_t::BROADCAST(4, "broadcast");
133const bond_interface::mode_t bond_interface::mode_t::LACP(5, "lacp");
134const bond_interface::mode_t bond_interface::mode_t::UNSPECIFIED(0,
135 "unspecified");
136
137const bond_interface::mode_t
138bond_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
159bond_interface::mode_t::mode_t(int v, const std::string& s)
160 : enum_base<bond_interface::mode_t>(v, s)
161{
162}
163
164const bond_interface::lb_t bond_interface::lb_t::L2(0, "l2");
165const bond_interface::lb_t bond_interface::lb_t::L34(1, "l34");
166const bond_interface::lb_t bond_interface::lb_t::L23(2, "l23");
167const bond_interface::lb_t bond_interface::lb_t::UNSPECIFIED(~0, "unspecified");
168
169const bond_interface::lb_t
170bond_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
185bond_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 */