-
Notifications
You must be signed in to change notification settings - Fork 390
/
test_inet_interface.cc
297 lines (237 loc) · 9.98 KB
/
test_inet_interface.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
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include "base/os.h"
#include "testing/gunit.h"
#include <boost/uuid/string_generator.hpp>
#include <base/logging.h>
#include <io/event_manager.h>
#include <tbb/task.h>
#include <base/task.h>
#include <cmn/agent_cmn.h>
#include "cfg/cfg_init.h"
#include "cfg/cfg_interface.h"
#include "oper/operdb_init.h"
#include "controller/controller_init.h"
#include "pkt/pkt_init.h"
#include "services/services_init.h"
#include "ksync/ksync_init.h"
#include "oper/interface_common.h"
#include "oper/nexthop.h"
#include "route/route.h"
#include "oper/vrf.h"
#include "oper/mpls.h"
#include "oper/vm.h"
#include "oper/vn.h"
#include "filter/acl.h"
#include "openstack/instance_service_server.h"
#include "test_cmn_util.h"
#include "vr_types.h"
#include <controller/controller_export.h>
#include <ksync/ksync_sock_user.h>
#define VRF_VHOST "vrf-vhost"
#define VRF_LL "vrf-ll"
#define VRF_GW "vrf-gw"
void RouterIdDepInit(Agent *agent) {
}
class InetInterfaceTest : public ::testing::Test {
public:
virtual void SetUp() {
agent_ = Agent::GetInstance();
interface_table_ = agent_->interface_table();
nh_table_ = agent_->nexthop_table();
vrf_table_ = agent_->vrf_table();
intf_count_ = agent_->interface_table()->Size();
nh_count_ = agent_->nexthop_table()->Size();
vrf_count_ = agent_->vrf_table()->Size();
peer_ = agent_->local_peer();
vrf_table_->CreateVrfReq(VRF_VHOST);
vrf_table_->CreateVrfReq(VRF_LL);
vrf_table_->CreateVrfReq(VRF_GW);
client->WaitForIdle();
vhost_rt_table_ = static_cast<InetUnicastAgentRouteTable *>
(vrf_table_->GetInet4UnicastRouteTable(VRF_VHOST));
ll_rt_table_ = static_cast<InetUnicastAgentRouteTable *>
(vrf_table_->GetInet4UnicastRouteTable(VRF_LL));
gw_rt_table_ = static_cast<InetUnicastAgentRouteTable *>
(vrf_table_->GetInet4UnicastRouteTable(VRF_GW));
}
virtual void TearDown() {
vrf_table_->DeleteVrfReq(VRF_VHOST);
vrf_table_->DeleteVrfReq(VRF_LL);
vrf_table_->DeleteVrfReq(VRF_GW);
client->WaitForIdle();
WAIT_FOR(100, 1000, (interface_table_->Size() == intf_count_));
WAIT_FOR(100, 1000, (agent_->vrf_table()->Size() == vrf_count_));
WAIT_FOR(100, 1000, (agent_->nexthop_table()->Size() == nh_count_));
WAIT_FOR(100, 1000, (agent_->vm_table()->Size() == 0U));
WAIT_FOR(100, 1000, (agent_->vn_table()->Size() == 0U));
}
unsigned int intf_count_;
unsigned int nh_count_;
unsigned int vrf_count_;
Agent *agent_;
InterfaceTable *interface_table_;
NextHopTable *nh_table_;
InetUnicastAgentRouteTable *vhost_rt_table_;
InetUnicastAgentRouteTable *ll_rt_table_;
InetUnicastAgentRouteTable *gw_rt_table_;
VrfTable *vrf_table_;
const Peer *peer_;
};
static void DelInterface(InetInterfaceTest *t, const char *ifname) {
InetInterface::DeleteReq(t->interface_table_, ifname);
}
static void AddInterface(InetInterfaceTest *t, const char *ifname,
InetInterface::SubType sub_type, const char *vrf,
const char *ip, int plen, const char *gw) {
InetInterface::CreateReq(t->interface_table_, ifname, sub_type, vrf,
Ip4Address::from_string(ip), plen,
Ip4Address::from_string(gw),
client->param()->eth_port(), "TEST");
}
static void DelInterface(InetInterfaceTest *t, const char *ifname,
const char *vrf, const char *gw) {
InetInterface::DeleteReq(t->interface_table_, ifname);
if (strcmp(vrf, VRF_VHOST) == 0){
t->vhost_rt_table_->DeleteReq(t->peer_, vrf,
Ip4Address::from_string(gw), 32, NULL);
}
if (strcmp(vrf, VRF_LL) == 0){
t->ll_rt_table_->DeleteReq(t->peer_, vrf, Ip4Address::from_string(gw),
32, NULL);
}
if (strcmp(vrf, VRF_GW) == 0){
t->gw_rt_table_->DeleteReq(t->peer_, vrf, Ip4Address::from_string(gw),
32, NULL);
}
}
// Create and delete VHOST port
TEST_F(InetInterfaceTest, vhost_basic_1) {
AddInterface(this, "vhost1", InetInterface::VHOST, VRF_VHOST,
"1.1.1.1", 24, "1.1.1.254");
client->WaitForIdle();
NextHop *nh;
nh = ReceiveNHGet(nh_table_, "vhost1", false);
EXPECT_TRUE(nh != NULL);
nh = ReceiveNHGet(nh_table_, "vhost1", true);
EXPECT_TRUE(nh != NULL);
EXPECT_TRUE(RouteFind(VRF_VHOST, Ip4Address::from_string("1.1.1.1"), 32));
EXPECT_TRUE(RouteFind(VRF_VHOST, Ip4Address::from_string("1.1.1.0"), 24));
EXPECT_TRUE(RouteFind(VRF_VHOST, Ip4Address::from_string("1.1.1.255"), 32));
EXPECT_TRUE(RouteFind(VRF_VHOST, Ip4Address::from_string("0.0.0.0"), 0));
EXPECT_TRUE(RouteFind(VRF_VHOST, Ip4Address::from_string("1.1.1.254"), 32));
DelInterface(this, "vhost1", VRF_VHOST, "1.1.1.254");
client->WaitForIdle();
}
TEST_F(InetInterfaceTest, vhost_key_manipulations) {
AddInterface(this, "vhost1", InetInterface::VHOST, VRF_VHOST,
"1.1.1.1", 24, "1.1.1.254");
client->WaitForIdle();
ReceiveNH *nh = static_cast<ReceiveNH *>(ReceiveNHGet(nh_table_, "vhost1",
false));
EXPECT_TRUE(nh != NULL);
DBEntryBase::KeyPtr key = nh->GetDBRequestKey();
nh->SetKey(key.get());
DelInterface(this, "vhost1", VRF_VHOST, "1.1.1.254");
client->WaitForIdle();
}
TEST_F(InetInterfaceTest, vhost_no_ip_1) {
AddInterface(this, "vhost1", InetInterface::VHOST, VRF_VHOST,
"0.0.0.0", 0, "1.1.1.254");
client->WaitForIdle();
NextHop *nh;
nh = ReceiveNHGet(nh_table_, "vhost1", false);
EXPECT_TRUE(nh != NULL);
nh = ReceiveNHGet(nh_table_, "vhost1", true);
EXPECT_TRUE(nh != NULL);
// Gateway route is added added
EXPECT_TRUE(RouteFind(VRF_VHOST, Ip4Address::from_string("0.0.0.0"), 0));
// Resolve route is not added for route since 1.1.1.0/24 route is missing
EXPECT_FALSE(RouteFind(VRF_VHOST, Ip4Address::from_string("1.1.1.254"),
32));
DelInterface(this, "vhost1", VRF_VHOST, "1.1.1.254");
client->WaitForIdle();
}
// Test interface add without-ip and set ip later
TEST_F(InetInterfaceTest, vhost_change_1) {
AddInterface(this, "vhost1", InetInterface::VHOST, VRF_VHOST,
"0.0.0.0", 0, "1.1.1.254");
client->WaitForIdle();
NextHop *nh;
nh = ReceiveNHGet(nh_table_, "vhost1", false);
EXPECT_TRUE(nh != NULL);
nh = ReceiveNHGet(nh_table_, "vhost1", true);
EXPECT_TRUE(nh != NULL);
// Gateway route is added added
EXPECT_TRUE(RouteFind(VRF_VHOST, Ip4Address::from_string("0.0.0.0"), 0));
// Resolve route is not added for route since 1.1.1.0/24 route is missing
EXPECT_FALSE(RouteFind(VRF_VHOST, Ip4Address::from_string("1.1.1.254"),
32));
AddInterface(this, "vhost1", InetInterface::VHOST, VRF_VHOST,
"1.1.1.1", 24, "1.1.1.254");
client->WaitForIdle();
EXPECT_TRUE(RouteFind(VRF_VHOST, Ip4Address::from_string("1.1.1.1"), 32));
EXPECT_TRUE(RouteFind(VRF_VHOST, Ip4Address::from_string("1.1.1.0"), 24));
EXPECT_TRUE(RouteFind(VRF_VHOST, Ip4Address::from_string("1.1.1.255"), 32));
EXPECT_TRUE(RouteFind(VRF_VHOST, Ip4Address::from_string("0.0.0.0"), 0));
EXPECT_TRUE(RouteFind(VRF_VHOST, Ip4Address::from_string("1.1.1.254"), 32));
DelInterface(this, "vhost1", VRF_VHOST, "1.1.1.254");
client->WaitForIdle();
}
TEST_F(InetInterfaceTest, del_add_1) {
AddInterface(this, "vhost1", InetInterface::VHOST, VRF_VHOST,
"0.0.0.0", 0, "1.1.1.254");
client->WaitForIdle();
// Add reference to interface so that its not freed on delete
InterfaceRef ref(InetInterfaceGet("vhost1"));
DelInterface(this, "vhost1");
client->WaitForIdle();
NextHop *nh;
nh = ReceiveNHGet(nh_table_, "vhost1", false);
EXPECT_TRUE(nh == NULL);
nh = ReceiveNHGet(nh_table_, "vhost1", true);
EXPECT_TRUE(nh == NULL);
EXPECT_TRUE(InetInterfaceGet("vhost1") == NULL);
AddInterface(this, "vhost1", InetInterface::VHOST, VRF_VHOST,
"1.1.1.1", 24, "1.1.1.254");
client->WaitForIdle();
nh = ReceiveNHGet(nh_table_, "vhost1", false);
EXPECT_TRUE(nh != NULL);
nh = ReceiveNHGet(nh_table_, "vhost1", true);
EXPECT_TRUE(nh != NULL);
EXPECT_TRUE(RouteFind(VRF_VHOST, Ip4Address::from_string("1.1.1.1"), 32));
EXPECT_TRUE(RouteFind(VRF_VHOST, Ip4Address::from_string("1.1.1.0"), 24));
EXPECT_TRUE(RouteFind(VRF_VHOST, Ip4Address::from_string("1.1.1.255"), 32));
EXPECT_TRUE(RouteFind(VRF_VHOST, Ip4Address::from_string("0.0.0.0"), 0));
EXPECT_TRUE(RouteFind(VRF_VHOST, Ip4Address::from_string("1.1.1.254"), 32));
DelInterface(this, "vhost1", VRF_VHOST, "1.1.1.254");
client->WaitForIdle();
}
// Create and delete LL port
TEST_F(InetInterfaceTest, ll_basic_1) {
AddInterface(this, "ll1", InetInterface::LINK_LOCAL, VRF_LL,
"1.1.1.1", 24, "1.1.1.254");
client->WaitForIdle();
NextHop *nh;
nh = ReceiveNHGet(nh_table_, "ll1", false);
EXPECT_TRUE(nh != NULL);
nh = ReceiveNHGet(nh_table_, "ll1", true);
EXPECT_TRUE(nh != NULL);
EXPECT_TRUE(RouteFind(VRF_LL, Ip4Address::from_string("1.1.1.1"), 32));
EXPECT_TRUE(RouteFind(VRF_LL, Ip4Address::from_string("1.1.1.0"), 24));
EXPECT_TRUE(RouteFind(VRF_LL, Ip4Address::from_string("1.1.1.255"), 32));
EXPECT_TRUE(RouteFind(VRF_LL, Ip4Address::from_string("0.0.0.0"), 0));
EXPECT_TRUE(RouteFind(VRF_LL, Ip4Address::from_string("1.1.1.254"), 32));
DelInterface(this, "ll1", VRF_LL, "1.1.1.254");
client->WaitForIdle();
}
int main(int argc, char **argv) {
GETUSERARGS();
client = TestInit(init_file, ksync_init, false, false, false);
int ret = RUN_ALL_TESTS();
client->WaitForIdle();
TestShutdown();
delete client;
return ret;
}