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