cdf0850ebc540a780ef08353d17657114f819082
[vpp.git] / extras / vom / vom / igmp_listen_cmds.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/igmp_listen_cmds.hpp"
17
18 DEFINE_VAPI_MSG_IDS_IGMP_API_JSON;
19
20 namespace VOM {
21 namespace igmp_listen_cmds {
22 listen_cmd::listen_cmd(HW::item<bool>& item,
23                        const handle_t& itf,
24                        const boost::asio::ip::address& gaddr,
25                        const igmp_listen::src_addrs_t& saddrs)
26   : rpc_cmd(item)
27   , m_itf(itf)
28   , m_gaddr(gaddr)
29   , m_saddrs(saddrs)
30 {
31 }
32
33 bool
34 listen_cmd::operator==(const listen_cmd& other) const
35 {
36   return ((m_itf == other.m_itf) && (m_gaddr == other.m_gaddr));
37 }
38
39 rc_t
40 listen_cmd::issue(connection& con)
41 {
42   u8 size = m_saddrs.size();
43   msg_t req(con.ctx(), sizeof(vapi_type_ip4_address) * size, std::ref(*this));
44
45   auto& payload = req.get_request().get_payload();
46   payload.group.sw_if_index = m_itf.value();
47   payload.group.filter = EXCLUDE;
48   to_bytes(m_gaddr.to_v4(), (u8*)&payload.group.gaddr);
49   auto addr = m_saddrs.cbegin();
50   u8 i = 0;
51   while (addr != m_saddrs.cend()) {
52     to_bytes(addr->to_v4(), (u8*)&payload.group.saddrs[i]);
53     addr++;
54     i++;
55   }
56
57   payload.group.n_srcs = i;
58   VAPI_CALL(req.execute());
59
60   return (wait());
61 }
62
63 std::string
64 listen_cmd::to_string() const
65 {
66   auto addr = m_saddrs.cbegin();
67   std::ostringstream s;
68   s << "igmp-listen: " << m_hw_item.to_string() << " itf:" << m_itf.to_string()
69     << " group:" << m_gaddr << " src-addrs:[";
70   while (addr != m_saddrs.cend()) {
71     s << " " << *addr;
72     addr++;
73   }
74   s << " ]";
75   return (s.str());
76 }
77
78 unlisten_cmd::unlisten_cmd(HW::item<bool>& item,
79                            const handle_t& itf,
80                            const boost::asio::ip::address& gaddr)
81   : rpc_cmd(item)
82   , m_itf(itf)
83   , m_gaddr(gaddr)
84 {
85 }
86
87 bool
88 unlisten_cmd::operator==(const unlisten_cmd& other) const
89 {
90   return ((m_itf == other.m_itf) && (m_gaddr == other.m_gaddr));
91 }
92
93 rc_t
94 unlisten_cmd::issue(connection& con)
95 {
96   msg_t req(con.ctx(), 0, std::ref(*this));
97
98   auto& payload = req.get_request().get_payload();
99   payload.group.sw_if_index = m_itf.value();
100   payload.group.n_srcs = 0;
101   payload.group.filter = INCLUDE;
102   to_bytes(m_gaddr.to_v4(), (u8*)&payload.group.gaddr);
103
104   VAPI_CALL(req.execute());
105
106   wait();
107   m_hw_item.set(rc_t::NOOP);
108
109   return rc_t::OK;
110 }
111
112 std::string
113 unlisten_cmd::to_string() const
114 {
115   std::ostringstream s;
116   s << "igmp-unlisten: " << m_hw_item.to_string()
117     << " itf:" << m_itf.to_string() << " group:" << m_gaddr;
118
119   return (s.str());
120 }
121
122 dump_cmd::dump_cmd(const handle_t& hdl)
123   : m_itf(hdl)
124 {
125 }
126
127 dump_cmd::dump_cmd(const dump_cmd& d)
128   : m_itf(d.m_itf)
129 {
130 }
131
132 bool
133 dump_cmd::operator==(const dump_cmd& other) const
134 {
135   return (true);
136 }
137
138 rc_t
139 dump_cmd::issue(connection& con)
140 {
141   m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
142
143   auto& payload = m_dump->get_request().get_payload();
144   payload.sw_if_index = m_itf.value();
145
146   VAPI_CALL(m_dump->execute());
147
148   wait();
149
150   return rc_t::OK;
151 }
152
153 std::string
154 dump_cmd::to_string() const
155 {
156   return ("igmp-listen-dump");
157 }
158
159 }; // namespace igmp_listen_cmds
160 }; // namespace VOM
161
162 /*
163  * fd.io coding-style-patch-verification: ON
164  *
165  * Local Variables:
166  * eval: (c-set-style "mozilla")
167  * End:
168  */