6958eb386bae25386f4905a00ec4b511735492e0
[vpp.git] / src / vpp-api / vom / bond_group_binding.cpp
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_group_binding.hpp"
17 #include "vom/bond_group_binding_cmds.hpp"
18
19 namespace VOM {
20
21 /**
22  * A DB of all bond interface binding
23  */
24 singular_db<bond_group_binding::key_t, bond_group_binding>
25   bond_group_binding::m_db;
26
27 bond_group_binding::event_handler bond_group_binding::m_evh;
28
29 bond_group_binding::bond_group_binding(const bond_interface& itf,
30                                        const enslaved_itf_t& itfs)
31   : m_itf(itf.singular())
32   , m_mem_itfs(itfs)
33   , m_binding(false)
34 {
35 }
36
37 bond_group_binding::bond_group_binding(const bond_group_binding& o)
38   : m_itf(o.m_itf)
39   , m_mem_itfs(o.m_mem_itfs)
40   , m_binding(o.m_binding)
41 {
42 }
43
44 bond_group_binding::~bond_group_binding()
45 {
46   sweep();
47
48   // not in the DB anymore.
49   m_db.release(key(), this);
50 }
51
52 const bond_group_binding::key_t
53 bond_group_binding::key() const
54 {
55   return (m_itf->key() + "-binding");
56 }
57
58 void
59 bond_group_binding::sweep()
60 {
61
62   auto it = m_mem_itfs.cbegin();
63   while (it != m_mem_itfs.cend()) {
64     if (m_binding) {
65       HW::enqueue(
66         new bond_group_binding_cmds::unbind_cmd(m_binding, it->hdl()));
67     }
68     HW::write();
69     ++it;
70   }
71 }
72
73 void
74 bond_group_binding::dump(std::ostream& os)
75 {
76   m_db.dump(os);
77 }
78
79 void
80 bond_group_binding::replay()
81 {
82   auto it = m_mem_itfs.cbegin();
83   while (it != m_mem_itfs.cend()) {
84     if (m_binding) {
85       HW::enqueue(
86         new bond_group_binding_cmds::bind_cmd(m_binding, m_itf->handle(), *it));
87     }
88     HW::write();
89     ++it;
90   }
91 }
92
93 std::string
94 bond_group_binding::to_string() const
95 {
96   auto it = m_mem_itfs.cbegin();
97   std::ostringstream s;
98   s << "bond-interface-binding: " << m_itf->to_string() << " slave-itfs: [";
99   while (it != m_mem_itfs.cend()) {
100     s << " " << it->to_string();
101     ++it;
102   }
103   s << "]";
104   return (s.str());
105 }
106
107 void
108 bond_group_binding::update(const bond_group_binding& desired)
109 {
110   /*
111    * the desired state is always that the interface should be created
112    */
113   auto it = m_mem_itfs.cbegin();
114   while (it != m_mem_itfs.cend()) {
115     if (!m_binding) {
116       HW::enqueue(
117         new bond_group_binding_cmds::bind_cmd(m_binding, m_itf->handle(), *it));
118     }
119     ++it;
120   }
121 }
122
123 std::shared_ptr<bond_group_binding>
124 bond_group_binding::find_or_add(const bond_group_binding& temp)
125 {
126   return (m_db.find_or_add(temp.key(), temp));
127 }
128
129 std::shared_ptr<bond_group_binding>
130 bond_group_binding::singular() const
131 {
132   return find_or_add(*this);
133 }
134
135 bond_group_binding::event_handler::event_handler()
136 {
137   OM::register_listener(this);
138   inspect::register_handler({ "bond-intf-binding" }, "Bond interface binding",
139                             this);
140 }
141
142 void
143 bond_group_binding::event_handler::handle_replay()
144 {
145   m_db.replay();
146 }
147
148 void
149 bond_group_binding::event_handler::handle_populate(const client_db::key_t& key)
150 {
151   /*
152    * handle it in interface class
153    */
154 }
155
156 dependency_t
157 bond_group_binding::event_handler::order() const
158 {
159   /*
160    * We want enslaved interfaces bind to bond after interface
161    * but before anything else.
162    */
163   return (dependency_t::BOND_BINDING);
164 }
165
166 void
167 bond_group_binding::event_handler::show(std::ostream& os)
168 {
169   m_db.dump(os);
170 }
171 }
172 /*
173  * fd.io coding-style-patch-verification: ON
174  *
175  * Local Variables:
176  * eval: (c-set-style "mozilla")
177  * End:
178  */