-
Notifications
You must be signed in to change notification settings - Fork 390
/
vnswif_listener.cc
382 lines (325 loc) · 11.7 KB
/
vnswif_listener.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include <assert.h>
#include <bits/sockaddr.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <ifaddrs.h>
#include <base/logging.h>
#include <base/util.h>
#include <cmn/agent_cmn.h>
#include <init/agent_param.h>
#include <cfg/cfg_init.h>
#include <oper/route_common.h>
#include <oper/mirror_table.h>
#include <ksync/ksync_index.h>
#include <vrouter/ksync/interface_ksync.h>
#include "vnswif_listener.h"
VnswInterfaceListenerLinux::VnswInterfaceListenerLinux(Agent *agent) :
VnswInterfaceListenerBase(agent) {
}
VnswInterfaceListenerLinux::~VnswInterfaceListenerLinux() {
}
int VnswInterfaceListenerLinux::CreateSocket() {
int s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
if (s < 0) {
LOG(ERROR, "Error <" << errno << ": " << strerror(errno) <<
"> creating socket");
assert(0);
}
/* Bind to netlink socket */
struct sockaddr_nl addr;
memset (&addr, 0, sizeof(addr));
addr.nl_family = AF_NETLINK;
addr.nl_groups = (RTMGRP_IPV4_ROUTE | RTMGRP_LINK | RTMGRP_IPV4_IFADDR);
if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
LOG(ERROR, "Error <" << errno << ": " << strerror(errno) <<
"> binding to netlink address family");
assert(0);
}
return s;
}
void VnswInterfaceListenerLinux::SyncCurrentState() {
/* Fetch Links from kernel syncronously, to allow dump request for routes
* to go through fine
*/
InitNetlinkScan(RTM_GETLINK, ++seqno_);
/* Fetch routes from kernel asyncronously and update the gateway-id */
InitNetlinkScan(RTM_GETADDR, ++seqno_);
/* Fetch routes from kernel asyncronously and update the gateway-id */
InitNetlinkScan(RTM_GETROUTE, ++seqno_);
}
// Initiate netlink scan based on type and flags
void
VnswInterfaceListenerLinux::InitNetlinkScan(uint32_t type, uint32_t seqno) {
struct nlmsghdr *nlh;
const uint32_t buf_size = VnswInterfaceListenerLinux::kMaxBufferSize;
memset(tx_buf_, 0, buf_size);
nlh = (struct nlmsghdr *)tx_buf_;
/* Fill in the nlmsg header */
nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
nlh->nlmsg_type = type;
// The message is a request for dump.
nlh->nlmsg_flags = (NLM_F_DUMP | NLM_F_REQUEST);
nlh->nlmsg_seq = seqno;
struct rtgenmsg *rt_gen = (struct rtgenmsg *) NLMSG_DATA (nlh);
rt_gen->rtgen_family = AF_PACKET;
boost::system::error_code ec;
sock_.send(boost::asio::buffer(nlh,nlh->nlmsg_len), 0, ec);
assert(ec.value() == 0);
uint8_t read_buf[buf_size];
/*
* Wait/Read the response for dump request, linux kernel doesn't handle
* dump request if response for previous dump request is not complete.
*/
int end = 0;
while (end == 0) {
memset(read_buf, 0, buf_size);
std::size_t len = sock_.receive(boost::asio::buffer(read_buf,
buf_size), 0, ec);
assert(ec.value() == 0);
struct nlmsghdr *nl = (struct nlmsghdr *)read_buf;
end = NlMsgDecode(nl, len, seqno);
}
}
void VnswInterfaceListenerLinux::RegisterAsyncReadHandler() {
read_buf_ = new uint8_t[kMaxBufferSize];
sock_.async_receive(boost::asio::buffer(read_buf_, kMaxBufferSize),
boost::bind(&VnswInterfaceListenerLinux::ReadHandler, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
void VnswInterfaceListenerLinux::ReadHandler(
const boost::system::error_code &error, std::size_t len) {
struct nlmsghdr *nlh;
if (error == 0) {
nlh = (struct nlmsghdr *)read_buf_;
NlMsgDecode(nlh, len, -1);
} else {
LOG(ERROR, "Error < : " << error.message() <<
"> reading packet on netlink sock");
}
if (read_buf_) {
delete [] read_buf_;
read_buf_ = NULL;
}
RegisterAsyncReadHandler();
}
/****************************************************************************
* Link Local route event handler
****************************************************************************/
int VnswInterfaceListenerLinux::AddAttr(uint8_t *buff, int type, void *data, int alen) {
struct nlmsghdr *n = (struct nlmsghdr *)buff;
int len = RTA_LENGTH(alen);
if (NLMSG_ALIGN(n->nlmsg_len) + len > VnswInterfaceListenerLinux::kMaxBufferSize)
return -1;
struct rtattr *rta = (struct rtattr*)(((char*)n)+NLMSG_ALIGN(n->nlmsg_len));
rta->rta_type = type;
rta->rta_len = len;
memcpy(RTA_DATA(rta), data, alen);
n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
return 0;
}
void VnswInterfaceListenerLinux::UpdateLinkLocalRoute(const Ip4Address &addr,
bool del_rt) {
struct nlmsghdr *nlh;
struct rtmsg *rtm;
uint32_t ipaddr;
memset(tx_buf_, 0, kMaxBufferSize);
nlh = (struct nlmsghdr *) tx_buf_;
rtm = (struct rtmsg *) NLMSG_DATA (nlh);
/* Fill in the nlmsg header*/
nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
if (del_rt) {
nlh->nlmsg_type = RTM_DELROUTE;
nlh->nlmsg_flags = NLM_F_REQUEST;
} else {
nlh->nlmsg_type = RTM_NEWROUTE;
nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE;
}
nlh->nlmsg_seq = ++seqno_;
rtm = (struct rtmsg *) NLMSG_DATA (nlh);
rtm->rtm_table = RT_TABLE_MAIN;
rtm->rtm_family = AF_INET;
rtm->rtm_type = RTN_UNICAST;
rtm->rtm_protocol = kVnswRtmProto;
rtm->rtm_scope = RT_SCOPE_LINK;
rtm->rtm_dst_len = 32;
ipaddr = RT_TABLE_MAIN;
AddAttr(tx_buf_, RTA_TABLE, (void *) &ipaddr, 4);
ipaddr = htonl(addr.to_ulong());
AddAttr(tx_buf_, RTA_DST, (void *) &ipaddr, 4);
int if_index = if_nametoindex(agent_->vhost_interface_name().c_str());
AddAttr(tx_buf_, RTA_OIF, (void *) &if_index, 4);
boost::system::error_code ec;
sock_.send(boost::asio::buffer(nlh,nlh->nlmsg_len), 0, ec);
assert(ec.value() == 0);
}
/****************************************************************************
* Netlink message handlers
* Decodes netlink messages and enqueues events to revent_queue_
****************************************************************************/
string VnswInterfaceListenerLinux::NetlinkTypeToString(uint32_t type) {
switch (type) {
case NLMSG_DONE:
return "NLMSG_DONE";
case RTM_NEWADDR:
return "RTM_NEWADDR";
case RTM_DELADDR:
return "RTM_DELADDR";
case RTM_NEWROUTE:
return "RTM_NEWROUTE";
case RTM_DELROUTE:
return "RTM_DELROUTE";
case RTM_NEWLINK:
return "RTM_NEWLINK";
case RTM_DELLINK:
return "RTM_DELLINK";
default:
break;
}
std::stringstream str;
str << "UNHANDLED <" << type << ">";
return str.str();
}
VnswInterfaceListenerBase::Event *
VnswInterfaceListenerLinux::HandleNetlinkRouteMsg(struct nlmsghdr *nlh) {
struct rtmsg *rtm = (struct rtmsg *) NLMSG_DATA (nlh);
if (rtm->rtm_family != AF_INET || rtm->rtm_table != RT_TABLE_MAIN
|| rtm->rtm_type != RTN_UNICAST || rtm->rtm_scope != RT_SCOPE_LINK) {
LOG(DEBUG, "Ignoring Netlink route with family "
<< (uint32_t)rtm->rtm_family
<< " table " << (uint32_t)rtm->rtm_table
<< " type " << (uint32_t)rtm->rtm_type
<< " scope " << (uint32_t)rtm->rtm_family);
return NULL;
}
int oif = -1;
uint32_t dst_ip = 0;
uint32_t gw_ip = 0;
/* Get the route atttibutes len */
int rtl = RTM_PAYLOAD(nlh);
/* Loop through all attributes */
for (struct rtattr *rth = (struct rtattr *) RTM_RTA(rtm);
RTA_OK(rth, rtl); rth = RTA_NEXT(rth, rtl)) {
/* Get the gateway (Next hop) */
if (rth->rta_type == RTA_DST) {
dst_ip = *((int *)RTA_DATA(rth));
}
if (rth->rta_type == RTA_GATEWAY) {
gw_ip = *((int *)RTA_DATA(rth));
}
if (rth->rta_type == RTA_OIF) {
oif = *((int *)RTA_DATA(rth));
}
}
if (oif == -1) {
return NULL;
}
char name[IFNAMSIZ];
if_indextoname(oif, name);
Ip4Address dst_addr((unsigned long)ntohl(dst_ip));
Ip4Address gw_addr((unsigned long)ntohl(gw_ip));
LOG(DEBUG, "Handle netlink route message "
<< NetlinkTypeToString(nlh->nlmsg_type)
<< " : " << dst_addr.to_string() << "/" << rtm->rtm_dst_len
<< " Interface " << name << " GW " << gw_addr.to_string());
Event::Type type;
if (nlh->nlmsg_type == RTM_DELROUTE) {
type = Event::DEL_ROUTE;
} else {
type = Event::ADD_ROUTE;
}
return new Event(type, dst_addr, rtm->rtm_dst_len, name, gw_addr,
rtm->rtm_protocol, rtm->rtm_flags);
}
VnswInterfaceListenerBase::Event *
VnswInterfaceListenerLinux::HandleNetlinkIntfMsg(struct nlmsghdr *nlh) {
/* Get the atttibutes len */
int rtl = RTM_PAYLOAD(nlh);
const char *port_name = NULL;
struct ifinfomsg *ifi = (struct ifinfomsg *) NLMSG_DATA (nlh);
/* Loop through all attributes */
for (struct rtattr *rth = IFLA_RTA(ifi); RTA_OK(rth, rtl);
rth = RTA_NEXT(rth, rtl)) {
/* Get the interface name */
if (rth->rta_type == IFLA_IFNAME) {
port_name = (char *) RTA_DATA(rth);
}
}
assert(port_name != NULL);
LOG(DEBUG, "Handle netlink interface message "
<< NetlinkTypeToString(nlh->nlmsg_type)
<< " for interface " << port_name << " flags " << ifi->ifi_flags);
Event::Type type;
if (nlh->nlmsg_type == RTM_DELLINK) {
type = Event::DEL_INTERFACE;
} else {
type = Event::ADD_INTERFACE;
}
return new Event(type, port_name, ifi->ifi_flags);
}
VnswInterfaceListenerBase::Event *
VnswInterfaceListenerLinux::HandleNetlinkAddrMsg(struct nlmsghdr *nlh) {
struct ifaddrmsg *ifa = (struct ifaddrmsg *) NLMSG_DATA (nlh);
// Get interface name from os-index
char name[IFNAMSIZ];
if_indextoname(ifa->ifa_index, name);
LOG(DEBUG, "Handle netlink address message "
<< NetlinkTypeToString(nlh->nlmsg_type) << " for interface " << name);
uint32_t ipaddr = 0;
int rtl = IFA_PAYLOAD(nlh);
for (struct rtattr *rth = IFA_RTA(ifa); rtl && RTA_OK(rth, rtl);
rth = RTA_NEXT(rth,rtl)) {
if (rth->rta_type != IFA_LOCAL) {
continue;
}
ipaddr = ntohl(* ((uint32_t *)RTA_DATA(rth)));
}
if (ipaddr == 0)
return NULL;
assert(ipaddr != 0);
Event::Type type;
if (nlh->nlmsg_type == RTM_DELADDR) {
type = Event::DEL_ADDR;
} else {
type = Event::ADD_ADDR;
}
return new Event(type, name, Ip4Address(ipaddr), ifa->ifa_prefixlen,
ifa->ifa_flags);
}
int VnswInterfaceListenerLinux::NlMsgDecode(struct nlmsghdr *nl,
std::size_t len, uint32_t seq_no) {
Event *event = NULL;
struct nlmsghdr *nlh = nl;
for (; (NLMSG_OK(nlh, len)); nlh = NLMSG_NEXT(nlh, len)) {
switch (nlh->nlmsg_type) {
case NLMSG_DONE:
if (nlh->nlmsg_seq == seq_no) {
return 1;
}
return 0;
case RTM_NEWADDR:
case RTM_DELADDR:
event = HandleNetlinkAddrMsg(nlh);
break;
case RTM_NEWROUTE:
case RTM_DELROUTE:
event = HandleNetlinkRouteMsg(nlh);
break;
case RTM_NEWLINK:
case RTM_DELLINK:
event = HandleNetlinkIntfMsg(nlh);
break;
default:
LOG(DEBUG, "VnswInterfaceListenerLinux got message : "
<< NetlinkTypeToString(nlh->nlmsg_type));
break;
}
if (event) {
revent_queue_->Enqueue(event);
}
}
return 0;
}