/
test_vnswif.cc
416 lines (344 loc) · 14.2 KB
/
test_vnswif.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include "base/os.h"
#include <cmn/agent_cmn.h>
#include "test/test_cmn_util.h"
void RouterIdDepInit(Agent *agent) {
}
struct PortInfo input[] = {
{"vnet1", 1, "1.1.1.1", "00:00:00:01:01:01", 1, 1},
{"vnet2", 2, "1.1.1.2", "00:00:00:01:01:02", 1, 2}
};
IpamInfo ipam_info[] = {
{"1.1.1.0", 24, "1.1.1.100"},
};
class TestVnswIf : public ::testing::Test {
public:
virtual void SetUp() {
agent_ = Agent::GetInstance();
vnswif_ = agent_->ksync()->vnsw_interface_listner();
CreateVmportEnv(input, 2, 1);
client->WaitForIdle();
AddIPAM("vn1", ipam_info, 1);
client->WaitForIdle();
EXPECT_TRUE(VmPortActive(1));
EXPECT_TRUE(VmPortActive(2));
vnet1_ = static_cast<VmInterface *>(VmPortGet(1));
vnet2_ = static_cast<VmInterface *>(VmPortGet(2));
}
virtual void TearDown() {
InterfaceEvent(false, "vnet1", 0);
InterfaceEvent(false, "vnet2", 0);
DeleteVmportEnv(input, 2, true, 1);
client->WaitForIdle();
DelIPAM("vn1");
client->WaitForIdle();
EXPECT_EQ(0, vnswif_->GetHostInterfaceCount());
WAIT_FOR(1000, 100, (VmPortFindRetDel(1) == false));
WAIT_FOR(1000, 100, (VmPortFindRetDel(2) == false));
}
void SetSeen(const string &ifname, bool oper, uint32_t id) {
vnswif_->SetSeen(agent_->vhost_interface_name(), true, id);
}
void ResetSeen(const string &ifname) {
vnswif_->ResetSeen(agent_->vhost_interface_name(), true);
}
void InterfaceEvent(bool add, const string &ifname, uint32_t flags) {
VnswInterfaceListener::Event::Type type;
if (add) {
type = VnswInterfaceListener::Event::ADD_INTERFACE;
} else {
type = VnswInterfaceListener::Event::DEL_INTERFACE;
}
vnswif_->Enqueue(new VnswInterfaceListener::Event(type, ifname, flags));
client->WaitForIdle();
}
void InterfaceAddressEvent(bool add, const string &ifname, const char *ip) {
VnswInterfaceListener::Event::Type type;
if (add) {
type = VnswInterfaceListener::Event::ADD_ADDR;
} else {
type = VnswInterfaceListener::Event::DEL_ADDR;
}
Ip4Address addr = Ip4Address::from_string(ip);
vnswif_->Enqueue(new VnswInterfaceListener::Event(type, ifname,
addr, 24, 0));
client->WaitForIdle();
}
void RouteEvent(bool add, const string &ifname, Ip4Address addr,
uint8_t plen, uint32_t protocol) {
VnswInterfaceListener::Event::Type type;
if (add) {
type = VnswInterfaceListener::Event::ADD_ROUTE;
} else {
type = VnswInterfaceListener::Event::DEL_ROUTE;
}
vnswif_->Enqueue(new VnswInterfaceListener::Event(type, addr, plen,
ifname, Ip4Address(0),
protocol, 0));
client->WaitForIdle();
}
Agent *agent_;
VnswInterfaceListener *vnswif_;
VmInterface *vnet1_;
VmInterface *vnet2_;
};
// Ensure LL routes added for active ports
TEST_F(TestVnswIf, basic_1) {
EXPECT_TRUE(vnswif_->IsValidLinkLocalAddress(vnet1_->mdata_ip_addr()));
EXPECT_TRUE(vnswif_->IsValidLinkLocalAddress(vnet2_->mdata_ip_addr()));
}
// Validate that link-local address is deleted for inactive interface
TEST_F(TestVnswIf, intf_inactive) {
DelLink("virtual-network", "vn1", "virtual-machine-interface", "vnet1");
client->WaitForIdle();
EXPECT_FALSE(vnswif_->IsValidLinkLocalAddress(vnet1_->mdata_ip_addr()));
EXPECT_TRUE(vnswif_->IsValidLinkLocalAddress(vnet2_->mdata_ip_addr()));
}
// Validate that link-local address is deleted when interface is deleted
TEST_F(TestVnswIf, intf_delete) {
DeleteVmportEnv(input, 1, false);
client->WaitForIdle();
EXPECT_FALSE(vnswif_->IsValidLinkLocalAddress(vnet1_->mdata_ip_addr()));
EXPECT_TRUE(vnswif_->IsValidLinkLocalAddress(vnet2_->mdata_ip_addr()));
}
// Validate that link-local address is added back if route deleted from kernel
TEST_F(TestVnswIf, host_route_del) {
uint32_t count = vnswif_->ll_add_count();
VnswInterfaceListener::Event *event =
new VnswInterfaceListener::Event(VnswInterfaceListener::Event::DEL_ROUTE,
vnet1_->mdata_ip_addr(), 32, "",
Ip4Address(0),
VnswInterfaceListener::kVnswRtmProto,
0);
vnswif_->Enqueue(event);
client->WaitForIdle();
WAIT_FOR(1000, 100, (vnswif_->ll_add_count() > count));
}
// On link flap of vhost, all link-local must be written again
TEST_F(TestVnswIf, vhost_link_flap) {
uint32_t id = Interface::kInvalidIndex;
const Interface *intf = agent_->vhost_interface();
if (intf) {
id = intf->id();
}
SetSeen(agent_->vhost_interface_name(), true, id);
client->WaitForIdle();
// Set link-state down
InterfaceEvent(true, agent_->vhost_interface_name(), 0);
// bring up link of vhost0
uint32_t count = vnswif_->ll_add_count();
// Set link-state up
InterfaceEvent(true, agent_->vhost_interface_name(),
(IFF_UP|IFF_RUNNING));
// Ensure routes are synced to host
WAIT_FOR(1000, 100, (vnswif_->ll_add_count() >= (count + 2)));
InterfaceEvent(false, agent_->vhost_interface_name(), 0);
ResetSeen(agent_->vhost_interface_name());
client->WaitForIdle();
}
// Interface inactive when seen by oper only
TEST_F(TestVnswIf, inactive_1) {
VnswInterfaceListener::HostInterfaceEntry *entry =
vnswif_->GetHostInterfaceEntry("vnet1");
EXPECT_TRUE(entry != NULL);
if (entry == NULL)
return;
EXPECT_TRUE(entry->oper_seen_);
EXPECT_FALSE(entry->host_seen_);
EXPECT_TRUE(entry->link_up_);
InterfaceEvent(true, "vnet1", 0);
EXPECT_TRUE(entry->host_seen_);
EXPECT_FALSE(entry->link_up_);
InterfaceEvent(true, "vnet1", (IFF_UP|IFF_RUNNING));
EXPECT_TRUE(entry->link_up_);
}
// vhost-ip address update
TEST_F(TestVnswIf, vhost_addr_1) {
uint32_t id = Interface::kInvalidIndex;
const Interface *intf = agent_->vhost_interface();
if (intf) {
id = intf->id();
}
SetSeen(agent_->vhost_interface_name(), true, id);
client->WaitForIdle();
VnswInterfaceListener::HostInterfaceEntry *entry =
vnswif_->GetHostInterfaceEntry(agent_->vhost_interface_name());
EXPECT_TRUE(entry != NULL);
if (entry == NULL)
return;
const VmInterface *vhost =
static_cast<const VmInterface *>(agent_->vhost_interface());
string vhost_ip = "1.1.1.1";
// vnswif module does not know IP address yet
EXPECT_EQ(entry->addr_.to_ulong(), 0);
InterfaceAddressEvent(true, agent_->vhost_interface_name(), "1.1.1.1");
// Validate address in entry
EXPECT_STREQ(entry->addr_.to_string().c_str(), "1.1.1.1");
// Message not sent to kernel since vhost already has IP
EXPECT_EQ(vnswif_->vhost_update_count(), 1);
InterfaceAddressEvent(true, agent_->vhost_interface_name(), "2.2.2.2");
// Validate address in local entry
EXPECT_STREQ(entry->addr_.to_string().c_str(), "2.2.2.2");
// Message not sent to kernel since vhost already has IP
EXPECT_EQ(vnswif_->vhost_update_count(), 1);
// Ensure there is no change vhost-ip
EXPECT_STREQ(vhost->primary_ip_addr().to_string().c_str(), vhost_ip.c_str());
InterfaceAddressEvent(false, agent_->vhost_interface_name(), "1.1.1.1");
// Validate address in local entry
EXPECT_STREQ(entry->addr_.to_string().c_str(), "0.0.0.0");
// Message not sent to kernel since vhost already has IP
EXPECT_EQ(vnswif_->vhost_update_count(), 1);
// Ensure there is no change vhost-ip
EXPECT_STREQ(vhost->primary_ip_addr().to_string().c_str(), vhost_ip.c_str());
InterfaceEvent(false, agent_->vhost_interface_name(), 0);
ResetSeen(agent_->vhost_interface_name());
client->WaitForIdle();
}
// vhost-ip address update
TEST_F(TestVnswIf, oper_state_1) {
VnswInterfaceListener::HostInterfaceEntry *entry =
vnswif_->GetHostInterfaceEntry("vnet1");
EXPECT_TRUE(entry != NULL);
if (entry == NULL)
return;
InterfaceEvent(true, "vnet1", (IFF_UP|IFF_RUNNING));
EXPECT_TRUE(entry->link_up_);
EXPECT_TRUE(entry->host_seen_);
EXPECT_TRUE(entry->oper_seen_);
InterfaceEvent(true, "vnet1", 0);
EXPECT_FALSE(entry->link_up_);
EXPECT_FALSE(vnet1_->ipv4_active());
EXPECT_FALSE(vnet1_->l2_active());
client->WaitForIdle();
EXPECT_TRUE(RouteGet(vnet1_->vrf()->GetName(), vnet1_->primary_ip_addr(), 32)
== NULL);
InterfaceEvent(true, "vnet1", (IFF_UP|IFF_RUNNING));
client->WaitForIdle();
EXPECT_TRUE(entry->link_up_);
EXPECT_TRUE(vnet1_->ipv4_active());
EXPECT_TRUE(vnet1_->l2_active());
EXPECT_TRUE(RouteGet(vnet1_->vrf()->GetName(), vnet1_->primary_ip_addr(), 32)
!= NULL);
}
// Delete the config node for interface, and verify interface NH are deleted
// Add the config node and verify the interface NH are readded
TEST_F(TestVnswIf, EcmpActivateDeactivate_1) {
struct PortInfo input[] = {
{"vnet3", 3, "1.1.1.10", "00:00:00:01:01:01", 1, 1},
{"vnet4", 4, "1.1.1.10", "00:00:00:01:01:02", 1, 2},
{"vnet5", 5, "1.1.1.10", "00:00:00:01:01:03", 1, 3},
};
client->Reset();
client->WaitForIdle();
// Create ports with ECMP
CreateVmportWithEcmp(input, 3);
client->WaitForIdle();
client->WaitForIdle();
// Ensure all interface are active
EXPECT_TRUE(VmPortActive(input, 0));
EXPECT_TRUE(VmPortActive(input, 1));
EXPECT_TRUE(VmPortActive(input, 2));
InterfaceEvent(true, "vnet3", 0);
InterfaceEvent(true, "vnet4", 0);
InterfaceEvent(true, "vnet5", 0);
client->WaitForIdle();
EXPECT_TRUE(VmPortInactive(input, 0));
EXPECT_TRUE(VmPortInactive(input, 1));
EXPECT_TRUE(VmPortInactive(input, 2));
InterfaceEvent(true, "vnet3", (IFF_UP|IFF_RUNNING));
InterfaceEvent(true, "vnet4", (IFF_UP|IFF_RUNNING));
InterfaceEvent(true, "vnet5", (IFF_UP|IFF_RUNNING));
client->WaitForIdle();
// Ensure ECMP is created
Ip4Address ip = Ip4Address::from_string("1.1.1.10");
InetUnicastRouteEntry *rt = RouteGet("vrf1", ip, 32);
EXPECT_TRUE(rt != NULL);
const CompositeNH *nh;
WAIT_FOR(100, 1000, ((nh = dynamic_cast<const CompositeNH *>(rt->GetActiveNextHop())) != NULL));
EXPECT_EQ(nh->ActiveComponentNHCount(), 3);
// Set oper-state of vnet3 down. We should have ECMP with 2 NH
InterfaceEvent(true, "vnet3", 0);
WAIT_FOR(100, 100, (VmPortActive(input, 0) == false));
client->WaitForIdle();
nh = dynamic_cast<const CompositeNH *>(rt->GetActiveNextHop());
EXPECT_EQ(nh->ActiveComponentNHCount(), 2);
// Set oper-state of vnet4 down. We should have non-ECMP route
InterfaceEvent(true, "vnet4", 0);
client->WaitForIdle();
WAIT_FOR(100, 100, (VmPortActive(input, 1) == false));
nh = dynamic_cast<const CompositeNH *>(rt->GetActiveNextHop());
EXPECT_TRUE(nh == NULL);
// Set oper-state of vnet5 down. Route should be deleted
InterfaceEvent(true, "vnet5", 0);
client->WaitForIdle();
WAIT_FOR(100, 100, (VmPortActive(input, 2) == false));
rt = RouteGet("vrf1", ip, 32);
EXPECT_TRUE(rt == NULL);
// Set oper-state of vnet3 and vnet4 up. We should have ECMP os 2 NH
InterfaceEvent(true, "vnet3", (IFF_UP|IFF_RUNNING));
InterfaceEvent(true, "vnet4", (IFF_UP|IFF_RUNNING));
client->WaitForIdle();
WAIT_FOR(100, 100, (VmPortActive(input, 0)));
WAIT_FOR(100, 100, (VmPortActive(input, 1)));
rt = RouteGet("vrf1", ip, 32);
EXPECT_TRUE(rt != NULL);
nh = dynamic_cast<const CompositeNH *>(rt->GetActiveNextHop());
EXPECT_TRUE(nh != NULL);
EXPECT_EQ(nh->ActiveComponentNHCount(), 2);
//Clean up
DeleteVmportEnv(input, 3, true);
InterfaceEvent(false, "vnet5", 0);
InterfaceEvent(false, "vnet4", 0);
InterfaceEvent(false, "vnet3", 0);
client->WaitForIdle();
}
// Agent adds a link-local route if its deleted in kernel
TEST_F(TestVnswIf, linklocal_1) {
EXPECT_TRUE(vnswif_->IsValidLinkLocalAddress(vnet1_->mdata_ip_addr()));
EXPECT_TRUE(vnswif_->IsValidLinkLocalAddress(vnet2_->mdata_ip_addr()));
uint32_t count = vnswif_->ll_add_count();
// Delete mdata-ip. agent should add it again
RouteEvent(false, "vnet1", vnet1_->mdata_ip_addr(), 32,
VnswInterfaceListener::kVnswRtmProto);
client->WaitForIdle();
WAIT_FOR(1000, 100, (vnswif_->ll_add_count() >= (count + 1)));
}
// Agent deletes a link-local route from kernel if its not valid address
TEST_F(TestVnswIf, linklocal_2) {
EXPECT_TRUE(vnswif_->IsValidLinkLocalAddress(vnet1_->mdata_ip_addr()));
EXPECT_TRUE(vnswif_->IsValidLinkLocalAddress(vnet2_->mdata_ip_addr()));
uint32_t count = vnswif_->ll_del_count();
// Add a dummy route with kVnswRtmProto as protocol. Agent should delete it
RouteEvent(true, "vnet1", Ip4Address::from_string("169.254.1.1"), 32,
VnswInterfaceListener::kVnswRtmProto);
client->WaitForIdle();
WAIT_FOR(1000, 100, (vnswif_->ll_del_count() >= (count + 1)));
}
class SetupTask : public Task {
public:
SetupTask() :
Task((TaskScheduler::GetInstance()->
GetTaskId("db::DBTable")), 0) {
}
virtual bool Run() {
Agent::GetInstance()->ksync()->vnsw_interface_listner()->Shutdown();
return true;
}
std::string Description() const { return "SetupTask"; }
};
int main(int argc, char *argv[]) {
GETUSERARGS();
strcpy(init_file, "controller/src/vnsw/agent/test/vnswa_no_ip_cfg.ini");
client = TestInit(init_file, ksync_init);
Agent::GetInstance()->ksync()->VnswInterfaceListenerInit();
Agent::GetInstance()->set_router_id(Ip4Address::from_string("10.1.1.1"));
int ret = RUN_ALL_TESTS();
TestShutdown();
SetupTask * task = new SetupTask();
TaskScheduler::GetInstance()->Enqueue(task);
client->WaitForIdle();
delete client;
return ret;
}