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