blob: 4c72fbc1aaa453fe73f28385d458983d5676ff6e [file] [log] [blame]
Neale Ranns812ed392017-10-16 04:20:13 -07001/*
2 * Test suite for class VppOM
3 *
4 * Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved.
5 *
6 * This program and the accompanying materials are made available under the
7 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
8 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 */
10#define BOOST_TEST_MODULE "VPP OBJECT MODEL"
11#define BOOST_TEST_DYN_LINK
12
13#include <boost/test/unit_test.hpp>
14#include <boost/assign/list_inserter.hpp>
15
16
17#include <iostream>
18#include <deque>
19
20#include "vom/om.hpp"
21#include "vom/interface.hpp"
22#include "vom/l2_binding.hpp"
23#include "vom/l3_binding.hpp"
24#include "vom/bridge_domain.hpp"
25#include "vom/bridge_domain_entry.hpp"
26#include "vom/bridge_domain_arp_entry.hpp"
27#include "vom/prefix.hpp"
28#include "vom/route.hpp"
29#include "vom/route_domain.hpp"
30#include "vom/vxlan_tunnel.hpp"
31#include "vom/sub_interface.hpp"
32#include "vom/acl_list.hpp"
33#include "vom/acl_binding.hpp"
34#include "vom/acl_l3_rule.hpp"
35#include "vom/acl_l2_rule.hpp"
36#include "vom/arp_proxy_config.hpp"
37#include "vom/arp_proxy_binding.hpp"
38#include "vom/ip_unnumbered.hpp"
39#include "vom/interface_ip6_nd.hpp"
40#include "vom/interface_span.hpp"
41#include "vom/neighbour.hpp"
42#include "vom/nat_static.hpp"
43#include "vom/nat_binding.hpp"
44
45using namespace boost;
46using namespace VOM;
47
48/**
49 * An expectation exception
50 */
51class ExpException
52{
53public:
54 ExpException(unsigned int number)
55 {
56 // a neat place to add a break point
57 std::cout << " ExpException here: " << number << std::endl;
58 }
59};
60
61class MockListener : public interface::event_listener,
62 public interface::stat_listener
63{
64 void handle_interface_stat(interface::stats_cmd *cmd)
65 {
66 }
67 void handle_interface_event(interface::events_cmd *cmd)
68 {
69 }
70};
71
72class MockCmdQ : public HW::cmd_q
73{
74public:
75 MockCmdQ():
76 m_strict_order(true)
77 {
78 }
79 virtual ~MockCmdQ()
80 {
81 }
82 void expect(cmd *f)
83 {
84 m_exp_queue.push_back(f);
85 }
86 void enqueue(cmd *f)
87 {
88 m_act_queue.push_back(f);
89 }
90 void enqueue(std::queue<cmd*> &cmds)
91 {
92 while (cmds.size())
93 {
94 m_act_queue.push_back(cmds.front());
95 cmds.pop();
96 }
97 }
98 void enqueue(std::shared_ptr<cmd> f)
99 {
100 m_act_queue.push_back(f.get());
101 }
102
103 void dequeue(cmd *f)
104 {
105 }
106
107 void dequeue(std::shared_ptr<cmd> cmd)
108 {
109 }
110
111 void strict_order(bool on)
112 {
113 m_strict_order = on;
114 }
115
116 bool is_empty()
117 {
118 return ((0 == m_exp_queue.size()) &&
119 (0 == m_act_queue.size()));
120 }
121
122 rc_t write()
123 {
124 cmd *f_exp, *f_act;
125 rc_t rc = rc_t::OK;
126
127 while (m_act_queue.size())
128 {
129 bool matched = false;
130 auto it_exp = m_exp_queue.begin();
131 auto it_act = m_act_queue.begin();
132
133 f_act = *it_act;
134
135 std::cout << " Act: " << f_act->to_string() << std::endl;
136 while (it_exp != m_exp_queue.end())
137 {
138 f_exp = *it_exp;
139 try
140 {
141 std::cout << " Exp: " << f_exp->to_string() << std::endl;
142
143 if (typeid(*f_exp) != typeid(*f_act))
144 {
145 throw ExpException(1);
146 }
147
148 if (typeid(*f_exp) == typeid(interface::af_packet_create_cmd))
149 {
150 rc = handle_derived<interface::af_packet_create_cmd>(f_exp, f_act);
151 }
152 else if (typeid(*f_exp) == typeid(interface::loopback_create_cmd))
153 {
154 rc = handle_derived<interface::loopback_create_cmd>(f_exp, f_act);
155 }
156 else if (typeid(*f_exp) == typeid(interface::loopback_delete_cmd))
157 {
158 rc = handle_derived<interface::loopback_delete_cmd>(f_exp, f_act);
159 }
160 else if (typeid(*f_exp) == typeid(interface::af_packet_delete_cmd))
161 {
162 rc = handle_derived<interface::af_packet_delete_cmd>(f_exp, f_act);
163 }
164 else if (typeid(*f_exp) == typeid(interface::state_change_cmd))
165 {
166 rc = handle_derived<interface::state_change_cmd>(f_exp, f_act);
167 }
168 else if (typeid(*f_exp) == typeid(interface::set_table_cmd))
169 {
170 rc = handle_derived<interface::set_table_cmd>(f_exp, f_act);
171 }
172 else if (typeid(*f_exp) == typeid(interface::set_mac_cmd))
173 {
174 rc = handle_derived<interface::set_mac_cmd>(f_exp, f_act);
175 }
176 else if (typeid(*f_exp) == typeid(interface::set_tag))
177 {
178 rc = handle_derived<interface::set_tag>(f_exp, f_act);
179 }
180 else if (typeid(*f_exp) == typeid(route_domain::create_cmd))
181 {
182 rc = handle_derived<route_domain::create_cmd>(f_exp, f_act);
183 }
184 else if (typeid(*f_exp) == typeid(route_domain::delete_cmd))
185 {
186 rc = handle_derived<route_domain::delete_cmd>(f_exp, f_act);
187 }
188 else if (typeid(*f_exp) == typeid(route::ip_route::update_cmd))
189 {
190 rc = handle_derived<route::ip_route::update_cmd>(f_exp, f_act);
191 }
192 else if (typeid(*f_exp) == typeid(route::ip_route::delete_cmd))
193 {
194 rc = handle_derived<route::ip_route::delete_cmd>(f_exp, f_act);
195 }
196 else if (typeid(*f_exp) == typeid(neighbour::create_cmd))
197 {
198 rc = handle_derived<neighbour::create_cmd>(f_exp, f_act);
199 }
200 else if (typeid(*f_exp) == typeid(neighbour::delete_cmd))
201 {
202 rc = handle_derived<neighbour::delete_cmd>(f_exp, f_act);
203 }
204 else if (typeid(*f_exp) == typeid(l3_binding::bind_cmd))
205 {
206 rc = handle_derived<l3_binding::bind_cmd>(f_exp, f_act);
207 }
208 else if (typeid(*f_exp) == typeid(l3_binding::unbind_cmd))
209 {
210 rc = handle_derived<l3_binding::unbind_cmd>(f_exp, f_act);
211 }
212 else if (typeid(*f_exp) == typeid(bridge_domain::create_cmd))
213 {
214 rc = handle_derived<bridge_domain::create_cmd>(f_exp, f_act);
215 }
216 else if (typeid(*f_exp) == typeid(bridge_domain::delete_cmd))
217 {
218 rc = handle_derived<bridge_domain::delete_cmd>(f_exp, f_act);
219 }
220 else if (typeid(*f_exp) == typeid(bridge_domain_entry::create_cmd))
221 {
222 rc = handle_derived<bridge_domain_entry::create_cmd>(f_exp, f_act);
223 }
224 else if (typeid(*f_exp) == typeid(bridge_domain_entry::delete_cmd))
225 {
226 rc = handle_derived<bridge_domain_entry::delete_cmd>(f_exp, f_act);
227 }
228 else if (typeid(*f_exp) == typeid(bridge_domain_arp_entry::create_cmd))
229 {
230 rc = handle_derived<bridge_domain_arp_entry::create_cmd>(f_exp, f_act);
231 }
232 else if (typeid(*f_exp) == typeid(bridge_domain_arp_entry::delete_cmd))
233 {
234 rc = handle_derived<bridge_domain_arp_entry::delete_cmd>(f_exp, f_act);
235 }
236 else if (typeid(*f_exp) == typeid(l2_binding::bind_cmd))
237 {
238 rc = handle_derived<l2_binding::bind_cmd>(f_exp, f_act);
239 }
240 else if (typeid(*f_exp) == typeid(l2_binding::unbind_cmd))
241 {
242 rc = handle_derived<l2_binding::unbind_cmd>(f_exp, f_act);
243 }
244 else if (typeid(*f_exp) == typeid(l2_binding::set_vtr_op_cmd))
245 {
246 rc = handle_derived<l2_binding::set_vtr_op_cmd>(f_exp, f_act);
247 }
248 else if (typeid(*f_exp) == typeid(vxlan_tunnel::create_cmd))
249 {
250 rc = handle_derived<vxlan_tunnel::create_cmd>(f_exp, f_act);
251 }
252 else if (typeid(*f_exp) == typeid(vxlan_tunnel::delete_cmd))
253 {
254 rc = handle_derived<vxlan_tunnel::delete_cmd>(f_exp, f_act);
255 }
256 else if (typeid(*f_exp) == typeid(sub_interface::create_cmd))
257 {
258 rc = handle_derived<sub_interface::create_cmd>(f_exp, f_act);
259 }
260 else if (typeid(*f_exp) == typeid(sub_interface::delete_cmd))
261 {
262 rc = handle_derived<sub_interface::delete_cmd>(f_exp, f_act);
263 }
264 else if (typeid(*f_exp) == typeid(ACL::l3_list::update_cmd))
265 {
266 rc = handle_derived<ACL::l3_list::update_cmd>(f_exp, f_act);
267 }
268 else if (typeid(*f_exp) == typeid(ACL::l3_list::delete_cmd))
269 {
270 rc = handle_derived<ACL::l3_list::delete_cmd>(f_exp, f_act);
271 }
272 else if (typeid(*f_exp) == typeid(ACL::l3_binding::bind_cmd))
273 {
274 rc = handle_derived<ACL::l3_binding::bind_cmd>(f_exp, f_act);
275 }
276 else if (typeid(*f_exp) == typeid(ACL::l3_binding::unbind_cmd))
277 {
278 rc = handle_derived<ACL::l3_binding::unbind_cmd>(f_exp, f_act);
279 }
280 else if (typeid(*f_exp) == typeid(ACL::l2_list::update_cmd))
281 {
282 rc = handle_derived<ACL::l2_list::update_cmd>(f_exp, f_act);
283 }
284 else if (typeid(*f_exp) == typeid(ACL::l2_list::delete_cmd))
285 {
286 rc = handle_derived<ACL::l2_list::delete_cmd>(f_exp, f_act);
287 }
288 else if (typeid(*f_exp) == typeid(ACL::l2_binding::bind_cmd))
289 {
290 rc = handle_derived<ACL::l2_binding::bind_cmd>(f_exp, f_act);
291 }
292 else if (typeid(*f_exp) == typeid(ACL::l2_binding::unbind_cmd))
293 {
294 rc = handle_derived<ACL::l2_binding::unbind_cmd>(f_exp, f_act);
295 }
296 else if (typeid(*f_exp) == typeid(arp_proxy_binding::bind_cmd))
297 {
298 rc = handle_derived<arp_proxy_binding::bind_cmd>(f_exp, f_act);
299 }
300 else if (typeid(*f_exp) == typeid(arp_proxy_binding::unbind_cmd))
301 {
302 rc = handle_derived<arp_proxy_binding::unbind_cmd>(f_exp, f_act);
303 }
304 else if (typeid(*f_exp) == typeid(arp_proxy_config::config_cmd))
305 {
306 rc = handle_derived<arp_proxy_config::config_cmd>(f_exp, f_act);
307 }
308 else if (typeid(*f_exp) == typeid(arp_proxy_config::unconfig_cmd))
309 {
310 rc = handle_derived<arp_proxy_config::unconfig_cmd>(f_exp, f_act);
311 }
312 else if (typeid(*f_exp) == typeid(ip_unnumbered::config_cmd))
313 {
314 rc = handle_derived<ip_unnumbered::config_cmd>(f_exp, f_act);
315 }
316 else if (typeid(*f_exp) == typeid(ip_unnumbered::unconfig_cmd))
317 {
318 rc = handle_derived<ip_unnumbered::unconfig_cmd>(f_exp, f_act);
319 }
320 else if (typeid(*f_exp) == typeid(ip6nd_ra_config::config_cmd))
321 {
322 rc = handle_derived<ip6nd_ra_config::config_cmd>(f_exp, f_act);
323 }
324 else if (typeid(*f_exp) == typeid(ip6nd_ra_config::unconfig_cmd))
325 {
326 rc = handle_derived<ip6nd_ra_config::unconfig_cmd>(f_exp, f_act);
327 }
328 else if (typeid(*f_exp) == typeid(ip6nd_ra_prefix::config_cmd))
329 {
330 rc = handle_derived<ip6nd_ra_prefix::config_cmd>(f_exp, f_act);
331 }
332 else if (typeid(*f_exp) == typeid(ip6nd_ra_prefix::unconfig_cmd))
333 {
334 rc = handle_derived<ip6nd_ra_prefix::unconfig_cmd>(f_exp, f_act);
335 }
336 else if (typeid(*f_exp) == typeid(interface_span::config_cmd))
337 {
338 rc = handle_derived<interface_span::config_cmd>(f_exp, f_act);
339 }
340 else if (typeid(*f_exp) == typeid(interface_span::unconfig_cmd))
341 {
342 rc = handle_derived<interface_span::unconfig_cmd>(f_exp, f_act);
343 }
344 else if (typeid(*f_exp) == typeid(nat_static::create_44_cmd))
345 {
346 rc = handle_derived<nat_static::create_44_cmd>(f_exp, f_act);
347 }
348 else if (typeid(*f_exp) == typeid(nat_static::delete_44_cmd))
349 {
350 rc = handle_derived<nat_static::delete_44_cmd>(f_exp, f_act);
351 }
352 else if (typeid(*f_exp) == typeid(nat_binding::bind_44_input_cmd))
353 {
354 rc = handle_derived<nat_binding::bind_44_input_cmd>(f_exp, f_act);
355 }
356 else if (typeid(*f_exp) == typeid(nat_binding::unbind_44_input_cmd))
357 {
358 rc = handle_derived<nat_binding::unbind_44_input_cmd>(f_exp, f_act);
359 }
360 else if (typeid(*f_exp) == typeid(interface::events_cmd))
361 {
362 rc = handle_derived<interface::events_cmd>(f_exp, f_act);
363 }
364 else
365 {
366 throw ExpException(2);
367 }
368
369 // if we get here then we found the match.
370 m_exp_queue.erase(it_exp);
371 m_act_queue.erase(it_act);
372 delete f_exp;
373 delete f_act;
374
375 // return any injected failures to the agent
376 if (rc_t::OK != rc && rc_t::NOOP != rc)
377 {
378 return (rc);
379 }
380
381 matched = true;
382 break;
383 }
384 catch (ExpException &e)
385 {
386 // The expected and actual do not match
387 if (m_strict_order)
388 {
389 // in strict ordering mode this is fatal, so rethrow
390 throw e;
391 }
392 else
393 {
394 // move the iterator onto the next in the expected list and
395 // check for a match
396 ++it_exp;
397 }
398 }
399 }
400
401 if (!matched)
402 throw ExpException(3);
403 }
404
405 return (rc);
406 }
407private:
408
409 template <typename T>
410 rc_t handle_derived(const cmd *f_exp, cmd *f_act)
411 {
412 const T *i_exp;
413 T *i_act;
414
415 i_exp = dynamic_cast<const T*>(f_exp);
416 i_act = dynamic_cast<T*>(f_act);
417 if (!(*i_exp == *i_act))
418 {
419 throw ExpException(4);
420 }
421 // pass the data and return code to the agent
422 i_act->item() = i_exp->item();
423
424 return (i_act->item().rc());
425 }
426
427 // The Q to push the expectations on
428 std::deque<cmd*> m_exp_queue;
429
430 // the queue to push the actual events on
431 std::deque<cmd*> m_act_queue;
432
433 // control whether the expected queue is strictly ordered.
434 bool m_strict_order;
435};
436
437class VppInit {
438public:
439 std::string name;
440 MockCmdQ *f;
441
442 VppInit()
443 : name("vpp-ut"),
444 f(new MockCmdQ())
445 {
446 HW::init(f);
447 OM::init();
448 logger().set(log_level_t::DEBUG);
449 }
450
451 ~VppInit() {
452 delete f;
453 }
454};
455
456BOOST_AUTO_TEST_SUITE(VppOM_test)
457
458#define TRY_CHECK_RC(stmt) \
459{ \
460 try { \
461 BOOST_CHECK(rc_t::OK == stmt); \
462 } \
463 catch (ExpException &e) \
464 { \
465 BOOST_CHECK(false); \
466 } \
467 BOOST_CHECK(vi.f->is_empty()); \
468}
469
470#define TRY_CHECK(stmt) \
471{ \
472 try { \
473 stmt; \
474 } \
475 catch (ExpException &e) \
476 { \
477 BOOST_CHECK(false); \
478 } \
479 BOOST_CHECK(vi.f->is_empty()); \
480}
481
482#define ADD_EXPECT(stmt) \
483 vi.f->expect(new stmt)
484
485#define STRICT_ORDER_OFF() \
486 vi.f->strict_order(false)
487
488BOOST_AUTO_TEST_CASE(test_interface) {
489 VppInit vi;
490 const std::string go = "GeorgeOrwell";
491 const std::string js = "JohnSteinbeck";
492 rc_t rc = rc_t::OK;
493
494 /*
495 * George creates and deletes the interface
496 */
497 std::string itf1_name = "afpacket1";
498 interface itf1(itf1_name,
499 interface::type_t::AFPACKET,
500 interface::admin_state_t::UP);
501
502 /*
503 * set the expectation for a afpacket interface create.
504 * 2 is the interface handle VPP [mock] assigns
505 */
506 HW::item<handle_t> hw_ifh(2, rc_t::OK);
507 ADD_EXPECT(interface::af_packet_create_cmd(hw_ifh, itf1_name));
508
509 HW::item<interface::admin_state_t> hw_as_up(interface::admin_state_t::UP, rc_t::OK);
510 ADD_EXPECT(interface::state_change_cmd(hw_as_up, hw_ifh));
511
512 TRY_CHECK_RC(OM::write(go, itf1));
513
514 HW::item<interface::admin_state_t> hw_as_down(interface::admin_state_t::DOWN, rc_t::OK);
515 ADD_EXPECT(interface::state_change_cmd(hw_as_down, hw_ifh));
516 ADD_EXPECT(interface::af_packet_delete_cmd(hw_ifh, itf1_name));
517
518 TRY_CHECK(OM::remove(go));
519
520 /*
521 * George creates the interface, then John brings it down.
522 * George's remove is a no-op, sice John also owns the interface
523 */
524 interface itf1b(itf1_name,
525 interface::type_t::AFPACKET,
526 interface::admin_state_t::DOWN);
527
528 ADD_EXPECT(interface::af_packet_create_cmd(hw_ifh, itf1_name));
529 ADD_EXPECT(interface::state_change_cmd(hw_as_up, hw_ifh));
530 TRY_CHECK_RC(OM::write(go, itf1));
531
532 ADD_EXPECT(interface::state_change_cmd(hw_as_down, hw_ifh));
533 TRY_CHECK_RC(OM::write(js, itf1b));
534
535 TRY_CHECK(OM::remove(go));
536
537 ADD_EXPECT(interface::af_packet_delete_cmd(hw_ifh, itf1_name));
538 TRY_CHECK(OM::remove(js));
539
540 /*
541 * George adds an interface, then we flush all of Geroge's state
542 */
543 ADD_EXPECT(interface::af_packet_create_cmd(hw_ifh, itf1_name));
544 ADD_EXPECT(interface::state_change_cmd(hw_as_up, hw_ifh));
545 TRY_CHECK_RC(OM::write(go, itf1));
546
547 TRY_CHECK(OM::mark(go));
548
549 ADD_EXPECT(interface::state_change_cmd(hw_as_down, hw_ifh));
550 ADD_EXPECT(interface::af_packet_delete_cmd(hw_ifh, itf1_name));
551 TRY_CHECK(OM::sweep(go));
552
553 /*
554 * George adds an interface. mark stale. update the same interface. sweep
555 * and expect no delete
556 */
557 ADD_EXPECT(interface::af_packet_create_cmd(hw_ifh, itf1_name));
558 ADD_EXPECT(interface::state_change_cmd(hw_as_down, hw_ifh));
559 TRY_CHECK_RC(OM::write(go, itf1b));
560
561 TRY_CHECK(OM::mark(go));
562
563 ADD_EXPECT(interface::state_change_cmd(hw_as_up, hw_ifh));
564 TRY_CHECK_RC(OM::write(go, itf1));
565
566 TRY_CHECK(OM::sweep(go));
567
568 ADD_EXPECT(interface::state_change_cmd(hw_as_down, hw_ifh));
569 ADD_EXPECT(interface::af_packet_delete_cmd(hw_ifh, itf1_name));
570 TRY_CHECK(OM::remove(go));
571
572 /*
573 * George adds an insterface, then we mark that state. Add a second interface
574 * an flush the first that is now stale.
575 */
576 ADD_EXPECT(interface::af_packet_create_cmd(hw_ifh, itf1_name));
577 ADD_EXPECT(interface::state_change_cmd(hw_as_up, hw_ifh));
578 TRY_CHECK_RC(OM::write(go, itf1));
579
580 TRY_CHECK(OM::mark(go));
581
582 std::string itf2_name = "afpacket2";
583 interface itf2(itf2_name,
584 interface::type_t::AFPACKET,
585 interface::admin_state_t::UP);
586 HW::item<handle_t> hw_ifh2(3, rc_t::OK);
587
588 ADD_EXPECT(interface::af_packet_create_cmd(hw_ifh2, itf2_name));
589 ADD_EXPECT(interface::state_change_cmd(hw_as_up, hw_ifh2));
590 TRY_CHECK_RC(OM::write(go, itf2));
591
592 ADD_EXPECT(interface::state_change_cmd(hw_as_down, hw_ifh));
593 ADD_EXPECT(interface::af_packet_delete_cmd(hw_ifh, itf1_name));
594 TRY_CHECK(OM::sweep(go));
595
596 TRY_CHECK(OM::mark(go));
597
598 ADD_EXPECT(interface::state_change_cmd(hw_as_down, hw_ifh2));
599 ADD_EXPECT(interface::af_packet_delete_cmd(hw_ifh2, itf2_name));
600 TRY_CHECK(OM::sweep(go));
601}
602
603BOOST_AUTO_TEST_CASE(test_bvi) {
604 VppInit vi;
605 const std::string ernest = "ErnestHemmingway";
606 const std::string graham = "GrahamGreene";
607 rc_t rc = rc_t::OK;
608 l3_binding *l3;
609
610 HW::item<interface::admin_state_t> hw_as_up(interface::admin_state_t::UP,
611 rc_t::OK);
612 HW::item<interface::admin_state_t> hw_as_down(interface::admin_state_t::DOWN,
613 rc_t::OK);
614
615 /*
616 * Enrest creates a BVI with address 10.10.10.10/24
617 */
618 route::prefix_t pfx_10("10.10.10.10", 24);
619
620 const std::string bvi_name = "bvi1";
621 interface itf(bvi_name,
622 interface::type_t::BVI,
623 interface::admin_state_t::UP);
624 HW::item<handle_t> hw_ifh(4, rc_t::OK);
625 HW::item<route::prefix_t> hw_pfx_10(pfx_10, rc_t::OK);
626
627 ADD_EXPECT(interface::loopback_create_cmd(hw_ifh, bvi_name));
628 ADD_EXPECT(interface::set_tag(hw_ifh, bvi_name));
629 ADD_EXPECT(interface::state_change_cmd(hw_as_up, hw_ifh));
630 TRY_CHECK_RC(OM::write(ernest, itf));
631
632 l3 = new l3_binding(itf, pfx_10);
633 HW::item<bool> hw_l3_bind(true, rc_t::OK);
634 HW::item<bool> hw_l3_unbind(false, rc_t::OK);
635 ADD_EXPECT(l3_binding::bind_cmd(hw_l3_bind, hw_ifh.data(), pfx_10));
636 TRY_CHECK_RC(OM::write(ernest, *l3));
637
638 // change the MAC address on the BVI
639 interface itf_new_mac(bvi_name,
640 interface::type_t::BVI,
641 interface::admin_state_t::UP);
642 l2_address_t l2_addr({0,1,2,3,4,5});
643 HW::item<l2_address_t> hw_mac(l2_addr, rc_t::OK);
644 itf_new_mac.set(l2_addr);
645 ADD_EXPECT(interface::set_mac_cmd(hw_mac, hw_ifh));
646 TRY_CHECK_RC(OM::write(ernest, itf_new_mac));
647
648 // create/write the interface to the OM again but with an unset MAC
649 // this should not generate a MAC address update
650 TRY_CHECK_RC(OM::write(ernest, itf));
651
652 // change the MAC address on the BVI - again
653 interface itf_new_mac2(bvi_name,
654 interface::type_t::BVI,
655 interface::admin_state_t::UP);
656 l2_address_t l2_addr2({0,1,2,3,4,6});
657 HW::item<l2_address_t> hw_mac2(l2_addr2, rc_t::OK);
658 itf_new_mac2.set(l2_addr2);
659 ADD_EXPECT(interface::set_mac_cmd(hw_mac2, hw_ifh));
660 TRY_CHECK_RC(OM::write(ernest, itf_new_mac2));
661
662 delete l3;
663 ADD_EXPECT(l3_binding::unbind_cmd(hw_l3_unbind, hw_ifh.data(), pfx_10));
664 ADD_EXPECT(interface::state_change_cmd(hw_as_down, hw_ifh));
665 ADD_EXPECT(interface::loopback_delete_cmd(hw_ifh));
666 TRY_CHECK(OM::remove(ernest));
667
668 /*
669 * Graham creates a BVI with address 10.10.10.10/24 in Routing Domain
670 */
671 route_domain rd(1);
672 HW::item<bool> hw_rd4_create(true, rc_t::OK);
673 HW::item<bool> hw_rd4_delete(false, rc_t::OK);
674 HW::item<bool> hw_rd6_create(true, rc_t::OK);
675 HW::item<bool> hw_rd6_delete(false, rc_t::OK);
676 HW::item<route::table_id_t> hw_rd4_bind(1, rc_t::OK);
677 HW::item<route::table_id_t> hw_rd4_unbind(route::DEFAULT_TABLE, rc_t::OK);
678 HW::item<route::table_id_t> hw_rd6_bind(1, rc_t::OK);
679 HW::item<route::table_id_t> hw_rd6_unbind(route::DEFAULT_TABLE, rc_t::OK);
680 ADD_EXPECT(route_domain::create_cmd(hw_rd4_create, l3_proto_t::IPV4, 1));
681 ADD_EXPECT(route_domain::create_cmd(hw_rd6_create, l3_proto_t::IPV6, 1));
682 TRY_CHECK_RC(OM::write(graham, rd));
683
684 const std::string bvi2_name = "bvi2";
685 interface *itf2 = new interface(bvi2_name,
686 interface::type_t::BVI,
687 interface::admin_state_t::UP,
688 rd);
689 HW::item<handle_t> hw_ifh2(5, rc_t::OK);
690
691 ADD_EXPECT(interface::loopback_create_cmd(hw_ifh2, bvi2_name));
692 ADD_EXPECT(interface::set_tag(hw_ifh2, bvi2_name));
693 ADD_EXPECT(interface::state_change_cmd(hw_as_up, hw_ifh2));
694 ADD_EXPECT(interface::set_table_cmd(hw_rd4_bind, l3_proto_t::IPV4, hw_ifh2));
695 ADD_EXPECT(interface::set_table_cmd(hw_rd6_bind, l3_proto_t::IPV6, hw_ifh2));
696
697 TRY_CHECK_RC(OM::write(graham, *itf2));
698
699 l3 = new l3_binding(*itf2, pfx_10);
700 ADD_EXPECT(l3_binding::bind_cmd(hw_l3_bind, hw_ifh2.data(), pfx_10));
701 TRY_CHECK_RC(OM::write(graham, *l3));
702
703 delete l3;
704 delete itf2;
705
706 ADD_EXPECT(l3_binding::unbind_cmd(hw_l3_unbind, hw_ifh2.data(), pfx_10));
707 ADD_EXPECT(interface::set_table_cmd(hw_rd4_unbind, l3_proto_t::IPV4, hw_ifh2));
708 ADD_EXPECT(interface::set_table_cmd(hw_rd6_unbind, l3_proto_t::IPV6, hw_ifh2));
709 ADD_EXPECT(interface::state_change_cmd(hw_as_down, hw_ifh2));
710 ADD_EXPECT(interface::loopback_delete_cmd(hw_ifh2));
711 ADD_EXPECT(route_domain::delete_cmd(hw_rd4_delete, l3_proto_t::IPV4, 1));
712 ADD_EXPECT(route_domain::delete_cmd(hw_rd6_delete, l3_proto_t::IPV6, 1));
713 TRY_CHECK(OM::remove(graham));
714}
715
716BOOST_AUTO_TEST_CASE(test_bridge) {
717 VppInit vi;
718 const std::string franz = "FranzKafka";
719 const std::string dante = "Dante";
720 rc_t rc = rc_t::OK;
721
722 /*
723 * Franz creates an interface, Bridge-domain, then binds the two
724 */
725
726 // interface create
727 std::string itf1_name = "afpacket1";
728 interface itf1(itf1_name,
729 interface::type_t::AFPACKET,
730 interface::admin_state_t::UP);
731
732 HW::item<handle_t> hw_ifh(3, rc_t::OK);
733 HW::item<interface::admin_state_t> hw_as_up(interface::admin_state_t::UP,
734 rc_t::OK);
735 ADD_EXPECT(interface::af_packet_create_cmd(hw_ifh, itf1_name));
736 ADD_EXPECT(interface::state_change_cmd(hw_as_up, hw_ifh));
737
738 TRY_CHECK_RC(OM::write(franz, itf1));
739
740 // bridge-domain create
741 bridge_domain bd1(33);
742
743 HW::item<uint32_t> hw_bd(33, rc_t::OK);
744 ADD_EXPECT(bridge_domain::create_cmd(hw_bd));
745
746 TRY_CHECK_RC(OM::write(franz, bd1));
747
748 // L2-interface create and bind
749 // this needs to be delete'd before the flush below, since it too maintains
750 // references to the BD and Interface
751 l2_binding *l2itf = new l2_binding(itf1, bd1);
752 HW::item<bool> hw_l2_bind(true, rc_t::OK);
753
754 ADD_EXPECT(l2_binding::bind_cmd(hw_l2_bind, hw_ifh.data(), hw_bd.data(), false));
755 TRY_CHECK_RC(OM::write(franz, *l2itf));
756
757 /*
758 * Dante adds an interface to the same BD
759 */
760 std::string itf2_name = "afpacket2";
761 interface itf2(itf2_name,
762 interface::type_t::AFPACKET,
763 interface::admin_state_t::UP);
764
765 HW::item<handle_t> hw_ifh2(4, rc_t::OK);
766 ADD_EXPECT(interface::af_packet_create_cmd(hw_ifh2, itf2_name));
767 ADD_EXPECT(interface::state_change_cmd(hw_as_up, hw_ifh2));
768 TRY_CHECK_RC(OM::write(dante, itf2));
769
770 // BD add is a no-op since it exists
771 TRY_CHECK_RC(OM::write(dante, bd1));
772
773 l2_binding *l2itf2 = new l2_binding(itf2, bd1);
774 HW::item<l2_binding::l2_vtr_op_t> hw_set_vtr(l2_binding::l2_vtr_op_t::L2_VTR_POP_1, rc_t::OK);
775 l2itf2->set(l2_binding::l2_vtr_op_t::L2_VTR_POP_1, 68);
776
777 ADD_EXPECT(l2_binding::bind_cmd(hw_l2_bind, hw_ifh2.data(), hw_bd.data(), false));
778 ADD_EXPECT(l2_binding::set_vtr_op_cmd(hw_set_vtr, hw_ifh2.data(), 68));
779 TRY_CHECK_RC(OM::write(dante, *l2itf2));
780
781 // Add some static entries to the bridge-domain
782 HW::item<bool> hw_be1(true, rc_t::OK);
783 mac_address_t mac1({0,1,2,3,4,5});
784 bridge_domain_entry *be1 = new bridge_domain_entry(bd1, mac1, itf2);
785 ADD_EXPECT(bridge_domain_entry::create_cmd(hw_be1, mac1, bd1.id(), hw_ifh2.data()));
786 TRY_CHECK_RC(OM::write(dante, *be1));
787
788 // Add some entries to the bridge-domain ARP termination table
789 HW::item<bool> hw_bea1(true, rc_t::OK);
790 boost::asio::ip::address ip1 = boost::asio::ip::address::from_string("10.10.10.10");
791
792 bridge_domain_arp_entry *bea1 = new bridge_domain_arp_entry(bd1, mac1, ip1);
793 ADD_EXPECT(bridge_domain_arp_entry::create_cmd(hw_be1, bd1.id(), mac1, ip1));
794 TRY_CHECK_RC(OM::write(dante, *bea1));
795
796 // flush Franz's state
797 delete l2itf;
798 HW::item<interface::admin_state_t> hw_as_down(interface::admin_state_t::DOWN,
799 rc_t::OK);
800 ADD_EXPECT(l2_binding::unbind_cmd(hw_l2_bind, hw_ifh.data(), hw_bd.data(), false));
801 ADD_EXPECT(interface::state_change_cmd(hw_as_down, hw_ifh));
802 ADD_EXPECT(interface::af_packet_delete_cmd(hw_ifh, itf1_name));
803 TRY_CHECK(OM::remove(franz));
804
805 // flush Dante's state - the order the interface and BD are deleted
806 // is an uncontrollable artifact of the C++ object destruction.
807 delete l2itf2;
808 delete be1;
809 delete bea1;
810 STRICT_ORDER_OFF();
811 ADD_EXPECT(l2_binding::unbind_cmd(hw_l2_bind, hw_ifh2.data(), hw_bd.data(), false));
812 ADD_EXPECT(interface::state_change_cmd(hw_as_down, hw_ifh2));
813 ADD_EXPECT(interface::af_packet_delete_cmd(hw_ifh2, itf2_name));
814 ADD_EXPECT(bridge_domain_entry::delete_cmd(hw_be1, mac1, bd1.id()));
815 ADD_EXPECT(bridge_domain_arp_entry::delete_cmd(hw_be1, bd1.id(), mac1, ip1));
816 ADD_EXPECT(bridge_domain::delete_cmd(hw_bd));
817 TRY_CHECK(OM::remove(dante));
818}
819
820BOOST_AUTO_TEST_CASE(test_vxlan) {
821 VppInit vi;
822 const std::string franz = "FranzKafka";
823 rc_t rc = rc_t::OK;
824
825 /*
826 * Franz creates an interface, Bridge-domain, then binds the two
827 */
828
829 // VXLAN create
830 vxlan_tunnel::endpoint_t ep(boost::asio::ip::address::from_string("10.10.10.10"),
831 boost::asio::ip::address::from_string("10.10.10.11"),
832 322);
833
834 vxlan_tunnel vxt(ep.src, ep.dst, ep.vni);
835
836 HW::item<handle_t> hw_vxt(3, rc_t::OK);
837 ADD_EXPECT(vxlan_tunnel::create_cmd(hw_vxt, "don't-care", ep));
838
839 TRY_CHECK_RC(OM::write(franz, vxt));
840
841 // bridge-domain create
842 bridge_domain bd1(33);
843
844 HW::item<uint32_t> hw_bd(33, rc_t::OK);
845 ADD_EXPECT(bridge_domain::create_cmd(hw_bd));
846
847 TRY_CHECK_RC(OM::write(franz, bd1));
848
849 // L2-interface create and bind
850 // this needs to be delete'd before the flush below, since it too maintains
851 // references to the BD and Interface
852 l2_binding *l2itf = new l2_binding(vxt, bd1);
853 HW::item<bool> hw_l2_bind(true, rc_t::OK);
854
855 ADD_EXPECT(l2_binding::bind_cmd(hw_l2_bind, hw_vxt.data(), hw_bd.data(), false));
856 TRY_CHECK_RC(OM::write(franz, *l2itf));
857
858 // flush Franz's state
859 delete l2itf;
860 HW::item<handle_t> hw_vxtdel(3, rc_t::NOOP);
861 STRICT_ORDER_OFF();
862 ADD_EXPECT(l2_binding::unbind_cmd(hw_l2_bind, hw_vxt.data(), hw_bd.data(), false));
863 ADD_EXPECT(bridge_domain::delete_cmd(hw_bd));
864 ADD_EXPECT(vxlan_tunnel::delete_cmd(hw_vxtdel, ep));
865 TRY_CHECK(OM::remove(franz));
866}
867
868BOOST_AUTO_TEST_CASE(test_vlan) {
869 VppInit vi;
870 const std::string noam = "NoamChomsky";
871 rc_t rc = rc_t::OK;
872
873 std::string itf1_name = "host1";
874 interface itf1(itf1_name,
875 interface::type_t::AFPACKET,
876 interface::admin_state_t::UP);
877
878 HW::item<handle_t> hw_ifh(2, rc_t::OK);
879 ADD_EXPECT(interface::af_packet_create_cmd(hw_ifh, itf1_name));
880
881 HW::item<interface::admin_state_t> hw_as_up(interface::admin_state_t::UP, rc_t::OK);
882 ADD_EXPECT(interface::state_change_cmd(hw_as_up, hw_ifh));
883
884 TRY_CHECK_RC(OM::write(noam, itf1));
885
886 sub_interface *vl33 = new sub_interface(itf1,
887 interface::admin_state_t::UP,
888 33);
889
890 HW::item<handle_t> hw_vl33(3, rc_t::OK);
891 ADD_EXPECT(sub_interface::create_cmd(hw_vl33, itf1_name+".33", hw_ifh.data(), 33));
892 ADD_EXPECT(interface::state_change_cmd(hw_as_up, hw_vl33));
893
894 TRY_CHECK_RC(OM::write(noam, *vl33));
895
896 delete vl33;
897 HW::item<interface::admin_state_t> hw_as_down(interface::admin_state_t::DOWN, rc_t::OK);
898 HW::item<handle_t> hw_vl33_down(3, rc_t::NOOP);
899 ADD_EXPECT(interface::state_change_cmd(hw_as_down, hw_vl33));
900 ADD_EXPECT(sub_interface::delete_cmd(hw_vl33_down));
901 ADD_EXPECT(interface::state_change_cmd(hw_as_down, hw_ifh));
902 ADD_EXPECT(interface::af_packet_delete_cmd(hw_ifh, itf1_name));
903
904 TRY_CHECK(OM::remove(noam));
905}
906
907BOOST_AUTO_TEST_CASE(test_acl) {
908 VppInit vi;
909 const std::string fyodor = "FyodorDostoyevsky";
910 const std::string leo = "LeoTolstoy";
911 rc_t rc = rc_t::OK;
912
913 /*
914 * Fyodor adds an ACL in the input direction
915 */
916 std::string itf1_name = "host1";
917 interface itf1(itf1_name,
918 interface::type_t::AFPACKET,
919 interface::admin_state_t::UP);
920 HW::item<handle_t> hw_ifh(2, rc_t::OK);
921 HW::item<interface::admin_state_t> hw_as_up(interface::admin_state_t::UP, rc_t::OK);
922 ADD_EXPECT(interface::af_packet_create_cmd(hw_ifh, itf1_name));
923 ADD_EXPECT(interface::state_change_cmd(hw_as_up, hw_ifh));
924 TRY_CHECK_RC(OM::write(fyodor, itf1));
925
926 route::prefix_t src("10.10.10.10", 32);
927 ACL::l3_rule r1(10, ACL::action_t::PERMIT, src, route::prefix_t::ZERO);
928 ACL::l3_rule r2(20, ACL::action_t::DENY, route::prefix_t::ZERO, route::prefix_t::ZERO);
929
930 std::string acl_name = "acl1";
931 ACL::l3_list acl1(acl_name);
932 acl1.insert(r2);
933 acl1.insert(r1);
934 ACL::l3_list::rules_t rules = {r1, r2};
935
936 HW::item<handle_t> hw_acl(2, rc_t::OK);
937 ADD_EXPECT(ACL::l3_list::update_cmd(hw_acl, acl_name, rules));
938 TRY_CHECK_RC(OM::write(fyodor, acl1));
939
940 ACL::l3_binding *l3b = new ACL::l3_binding(direction_t::INPUT, itf1, acl1);
941 HW::item<bool> hw_binding(true, rc_t::OK);
942 ADD_EXPECT(ACL::l3_binding::bind_cmd(hw_binding, direction_t::INPUT,
943 hw_ifh.data(), hw_acl.data()));
944 TRY_CHECK_RC(OM::write(fyodor, *l3b));
945
946 /**
947 * Leo adds an L2 ACL in the output direction
948 */
949 TRY_CHECK_RC(OM::write(leo, itf1));
950
951 std::string l2_acl_name = "l2_acl1";
952 mac_address_t mac({0x0, 0x0, 0x1, 0x2, 0x3, 0x4});
953 mac_address_t mac_mask({0xff, 0xff, 0xff, 0x0, 0x0, 0x0});
954 ACL::l2_rule l2_r1(10, ACL::action_t::PERMIT, src, mac, mac_mask);
955 ACL::l2_rule l2_r2(20, ACL::action_t::DENY, src, {}, {});
956
957 ACL::l2_list l2_acl(l2_acl_name);
958 l2_acl.insert(l2_r2);
959 l2_acl.insert(l2_r1);
960
961 ACL::l2_list::rules_t l2_rules = {l2_r1, l2_r2};
962
963 HW::item<handle_t> l2_hw_acl(3, rc_t::OK);
964 ADD_EXPECT(ACL::l2_list::update_cmd(l2_hw_acl, l2_acl_name, l2_rules));
965 TRY_CHECK_RC(OM::write(leo, l2_acl));
966
967 ACL::l2_binding *l2b = new ACL::l2_binding(direction_t::OUTPUT, itf1, l2_acl);
968 HW::item<bool> l2_hw_binding(true, rc_t::OK);
969 ADD_EXPECT(ACL::l2_binding::bind_cmd(l2_hw_binding, direction_t::OUTPUT,
970 hw_ifh.data(), l2_hw_acl.data()));
971 TRY_CHECK_RC(OM::write(leo, *l2b));
972
973 delete l2b;
974 ADD_EXPECT(ACL::l2_binding::unbind_cmd(l2_hw_binding, direction_t::OUTPUT,
975 hw_ifh.data(), l2_hw_acl.data()));
976 ADD_EXPECT(ACL::l2_list::delete_cmd(l2_hw_acl));
977 TRY_CHECK(OM::remove(leo));
978
979 delete l3b;
980 HW::item<interface::admin_state_t> hw_as_down(interface::admin_state_t::DOWN,
981 rc_t::OK);
982 STRICT_ORDER_OFF();
983 ADD_EXPECT(ACL::l3_binding::unbind_cmd(hw_binding, direction_t::INPUT,
984 hw_ifh.data(), hw_acl.data()));
985 ADD_EXPECT(ACL::l3_list::delete_cmd(hw_acl));
986 ADD_EXPECT(interface::state_change_cmd(hw_as_down, hw_ifh));
987 ADD_EXPECT(interface::af_packet_delete_cmd(hw_ifh, itf1_name));
988
989 TRY_CHECK(OM::remove(fyodor));
990}
991
992BOOST_AUTO_TEST_CASE(test_arp_proxy) {
993 VppInit vi;
994 const std::string kurt = "KurtVonnegut";
995 rc_t rc = rc_t::OK;
996
997 asio::ip::address_v4 low = asio::ip::address_v4::from_string("10.0.0.0");
998 asio::ip::address_v4 high = asio::ip::address_v4::from_string("10.0.0.255");
999
1000 arp_proxy_config ap(low, high);
1001 HW::item<bool> hw_ap_cfg(true, rc_t::OK);
1002 ADD_EXPECT(arp_proxy_config::config_cmd(hw_ap_cfg, low, high));
1003 TRY_CHECK_RC(OM::write(kurt, ap));
1004
1005 std::string itf3_name = "host3";
1006 interface itf3(itf3_name,
1007 interface::type_t::AFPACKET,
1008 interface::admin_state_t::UP);
1009 HW::item<handle_t> hw_ifh(2, rc_t::OK);
1010 HW::item<interface::admin_state_t> hw_as_up(interface::admin_state_t::UP, rc_t::OK);
1011 ADD_EXPECT(interface::af_packet_create_cmd(hw_ifh, itf3_name));
1012 ADD_EXPECT(interface::state_change_cmd(hw_as_up, hw_ifh));
1013 TRY_CHECK_RC(OM::write(kurt, itf3));
1014
1015 arp_proxy_binding *apb = new arp_proxy_binding(itf3, ap);
1016 HW::item<bool> hw_binding(true, rc_t::OK);
1017 ADD_EXPECT(arp_proxy_binding::bind_cmd(hw_binding, hw_ifh.data()));
1018 TRY_CHECK_RC(OM::write(kurt, *apb));
1019
1020 delete apb;
1021
1022 HW::item<interface::admin_state_t> hw_as_down(interface::admin_state_t::DOWN,
1023 rc_t::OK);
1024 STRICT_ORDER_OFF();
1025 ADD_EXPECT(arp_proxy_binding::unbind_cmd(hw_binding, hw_ifh.data()));
1026 ADD_EXPECT(interface::state_change_cmd(hw_as_down, hw_ifh));
1027 ADD_EXPECT(interface::af_packet_delete_cmd(hw_ifh, itf3_name));
1028 ADD_EXPECT(arp_proxy_config::unconfig_cmd(hw_ap_cfg, low, high));
1029
1030 TRY_CHECK(OM::remove(kurt));
1031}
1032
1033BOOST_AUTO_TEST_CASE(test_ip_unnumbered) {
1034 VppInit vi;
1035 const std::string eric = "EricAmbler";
1036 rc_t rc = rc_t::OK;
1037
1038 /*
1039 * Interface 1 has the L3 address
1040 */
1041 std::string itf1_name = "host1";
1042 interface itf1(itf1_name,
1043 interface::type_t::AFPACKET,
1044 interface::admin_state_t::UP);
1045 HW::item<handle_t> hw_ifh(2, rc_t::OK);
1046 HW::item<interface::admin_state_t> hw_as_up(interface::admin_state_t::UP, rc_t::OK);
1047 ADD_EXPECT(interface::af_packet_create_cmd(hw_ifh, itf1_name));
1048 ADD_EXPECT(interface::state_change_cmd(hw_as_up, hw_ifh));
1049 TRY_CHECK_RC(OM::write(eric, itf1));
1050
1051 route::prefix_t pfx_10("10.10.10.10", 24);
1052 l3_binding *l3 = new l3_binding(itf1, pfx_10);
1053 HW::item<bool> hw_l3_bind(true, rc_t::OK);
1054 HW::item<bool> hw_l3_unbind(false, rc_t::OK);
1055 ADD_EXPECT(l3_binding::bind_cmd(hw_l3_bind, hw_ifh.data(), pfx_10));
1056 TRY_CHECK_RC(OM::write(eric, *l3));
1057
1058 /*
1059 * Interface 2 is unnumbered
1060 */
1061 std::string itf2_name = "host2";
1062 interface itf2(itf2_name,
1063 interface::type_t::AFPACKET,
1064 interface::admin_state_t::UP);
1065
1066 HW::item<handle_t> hw_ifh2(4, rc_t::OK);
1067 ADD_EXPECT(interface::af_packet_create_cmd(hw_ifh2, itf2_name));
1068 ADD_EXPECT(interface::state_change_cmd(hw_as_up, hw_ifh2));
1069 TRY_CHECK_RC(OM::write(eric, itf2));
1070
1071 ip_unnumbered *ipun = new ip_unnumbered(itf2, itf1);
1072 HW::item<bool> hw_ip_cfg(true, rc_t::OK);
1073 HW::item<bool> hw_ip_uncfg(false, rc_t::OK);
1074 ADD_EXPECT(ip_unnumbered::config_cmd(hw_ip_cfg, hw_ifh2.data(), hw_ifh.data()));
1075 TRY_CHECK_RC(OM::write(eric, *ipun));
1076
1077 delete l3;
1078 delete ipun;
1079
1080 HW::item<interface::admin_state_t> hw_as_down(interface::admin_state_t::DOWN, rc_t::OK);
1081 STRICT_ORDER_OFF();
1082 ADD_EXPECT(ip_unnumbered::unconfig_cmd(hw_ip_uncfg, hw_ifh2.data(), hw_ifh.data()));
1083 ADD_EXPECT(l3_binding::unbind_cmd(hw_l3_unbind, hw_ifh.data(), pfx_10));
1084 ADD_EXPECT(interface::state_change_cmd(hw_as_down, hw_ifh2));
1085 ADD_EXPECT(interface::af_packet_delete_cmd(hw_ifh2, itf2_name));
1086 ADD_EXPECT(interface::state_change_cmd(hw_as_down, hw_ifh));
1087 ADD_EXPECT(interface::af_packet_delete_cmd(hw_ifh, itf1_name));
1088
1089 TRY_CHECK(OM::remove(eric));
1090}
1091
1092BOOST_AUTO_TEST_CASE(test_ip6nd) {
1093 VppInit vi;
1094 const std::string paulo = "PauloCoelho";
1095 rc_t rc = rc_t::OK;
1096
1097 /*
1098 * ra config
1099 */
1100 std::string itf_name = "host_ip6nd";
1101 interface itf(itf_name,
1102 interface::type_t::AFPACKET,
1103 interface::admin_state_t::UP);
1104 HW::item<handle_t> hw_ifh(3, rc_t::OK);
1105 HW::item<interface::admin_state_t> hw_as_up(interface::admin_state_t::UP, rc_t::OK);
1106 ADD_EXPECT(interface::af_packet_create_cmd(hw_ifh, itf_name));
1107 ADD_EXPECT(interface::state_change_cmd(hw_as_up, hw_ifh));
1108 TRY_CHECK_RC(OM::write(paulo, itf));
1109
1110 route::prefix_t pfx_10("fd8f:69d8:c12c:ca62::3", 128);
1111 l3_binding *l3 = new l3_binding(itf, pfx_10);
1112 HW::item<bool> hw_l3_bind(true, rc_t::OK);
1113 HW::item<bool> hw_l3_unbind(false, rc_t::OK);
1114 ADD_EXPECT(l3_binding::bind_cmd(hw_l3_bind, hw_ifh.data(), pfx_10));
1115 TRY_CHECK_RC(OM::write(paulo, *l3));
1116
1117 ra_config ra(0, 1, 0, 4);
1118 ip6nd_ra_config *ip6ra = new ip6nd_ra_config(itf, ra);
1119 HW::item<bool> hw_ip6nd_ra_config_config(true, rc_t::OK);
1120 HW::item<bool> hw_ip6nd_ra_config_unconfig(false, rc_t::OK);
1121 ADD_EXPECT(ip6nd_ra_config::config_cmd(hw_ip6nd_ra_config_config, hw_ifh.data(), ra));
1122 TRY_CHECK_RC(OM::write(paulo, *ip6ra));
1123
1124 /*
1125 * ra prefix
1126 */
1127 ra_prefix ra_pfx(pfx_10, 0, 0, 2592000, 604800);
1128 ip6nd_ra_prefix *ip6pfx = new ip6nd_ra_prefix(itf, ra_pfx);
1129 HW::item<bool> hw_ip6nd_ra_prefix_config(true, rc_t::OK);
1130 HW::item<bool> hw_ip6nd_ra_prefix_unconfig(false, rc_t::OK);
1131 ADD_EXPECT(ip6nd_ra_prefix::config_cmd(hw_ip6nd_ra_prefix_config, hw_ifh.data(), ra_pfx));
1132 TRY_CHECK_RC(OM::write(paulo, *ip6pfx));
1133
1134 delete ip6pfx;
1135
1136 ADD_EXPECT(ip6nd_ra_prefix::unconfig_cmd(hw_ip6nd_ra_prefix_unconfig, hw_ifh.data(), ra_pfx));
1137
1138 delete ip6ra;
1139 delete l3;
1140
1141 HW::item<interface::admin_state_t> hw_as_down(interface::admin_state_t::DOWN, rc_t::OK);
1142
1143 STRICT_ORDER_OFF();
1144 ADD_EXPECT(ip6nd_ra_config::unconfig_cmd(hw_ip6nd_ra_config_unconfig, hw_ifh.data(), ra));
1145 ADD_EXPECT(l3_binding::unbind_cmd(hw_l3_unbind, hw_ifh.data(), pfx_10));
1146 ADD_EXPECT(interface::state_change_cmd(hw_as_down, hw_ifh));
1147 ADD_EXPECT(interface::af_packet_delete_cmd(hw_ifh, itf_name));
1148
1149 TRY_CHECK(OM::remove(paulo));
1150}
1151
1152BOOST_AUTO_TEST_CASE(test_interface_span) {
1153 VppInit vi;
1154 const std::string elif = "ElifShafak";
1155 rc_t rc = rc_t::OK;
1156
1157 /*
1158 * Interface 1 to be mirrored
1159 */
1160 std::string itf1_name = "port-from";
1161 interface itf1(itf1_name,
1162 interface::type_t::AFPACKET,
1163 interface::admin_state_t::UP);
1164 HW::item<handle_t> hw_ifh(2, rc_t::OK);
1165 HW::item<interface::admin_state_t> hw_as_up(interface::admin_state_t::UP, rc_t::OK);
1166 ADD_EXPECT(interface::af_packet_create_cmd(hw_ifh, itf1_name));
1167 ADD_EXPECT(interface::state_change_cmd(hw_as_up, hw_ifh));
1168 TRY_CHECK_RC(OM::write(elif, itf1));
1169
1170 /*
1171 * Interface 2 where traffic is mirrored
1172 */
1173 std::string itf2_name = "port-to";
1174 interface itf2(itf2_name,
1175 interface::type_t::AFPACKET,
1176 interface::admin_state_t::UP);
1177
1178 HW::item<handle_t> hw_ifh2(4, rc_t::OK);
1179 HW::item<interface::admin_state_t> hw_as_up2(interface::admin_state_t::UP, rc_t::OK);
1180
1181 ADD_EXPECT(interface::af_packet_create_cmd(hw_ifh2, itf2_name));
1182 ADD_EXPECT(interface::state_change_cmd(hw_as_up2, hw_ifh2));
1183 TRY_CHECK_RC(OM::write(elif, itf2));
1184
1185 interface_span *itf_span = new interface_span(itf1, itf2, interface_span::state_t::TX_RX_ENABLED);
1186 HW::item<bool> hw_is_cfg(true, rc_t::OK);
1187 HW::item<bool> hw_is_uncfg(true, rc_t::OK);
1188 ADD_EXPECT(interface_span::config_cmd(hw_is_cfg, hw_ifh.data(), hw_ifh2.data(), interface_span::state_t::TX_RX_ENABLED));
1189 TRY_CHECK_RC(OM::write(elif, *itf_span));
1190
1191 HW::item<interface::admin_state_t> hw_as_down(interface::admin_state_t::DOWN, rc_t::OK);
1192 HW::item<interface::admin_state_t> hw_as_down2(interface::admin_state_t::DOWN, rc_t::OK);
1193
1194 delete itf_span;
1195 STRICT_ORDER_OFF();
1196 ADD_EXPECT(interface_span::unconfig_cmd(hw_is_uncfg, hw_ifh.data(), hw_ifh2.data()));
1197 ADD_EXPECT(interface::state_change_cmd(hw_as_down, hw_ifh));
1198 ADD_EXPECT(interface::af_packet_delete_cmd(hw_ifh, itf1_name));
1199 ADD_EXPECT(interface::state_change_cmd(hw_as_down2, hw_ifh2));
1200 ADD_EXPECT(interface::af_packet_delete_cmd(hw_ifh2, itf2_name));
1201
1202 TRY_CHECK(OM::remove(elif));
1203}
1204
1205BOOST_AUTO_TEST_CASE(test_routing) {
1206 VppInit vi;
1207 const std::string ian = "IanFleming";
1208 rc_t rc = rc_t::OK;
1209
1210 /*
1211 * non-default route domain
1212 */
1213 route_domain rd4(1);
1214 HW::item<bool> hw_rd4_create(true, rc_t::OK);
1215 HW::item<bool> hw_rd4_delete(false, rc_t::OK);
1216 HW::item<bool> hw_rd6_create(true, rc_t::OK);
1217 HW::item<bool> hw_rd6_delete(false, rc_t::OK);
1218 HW::item<route::table_id_t> hw_rd4_bind(1, rc_t::OK);
1219 HW::item<route::table_id_t> hw_rd4_unbind(route::DEFAULT_TABLE, rc_t::OK);
1220 HW::item<route::table_id_t> hw_rd6_bind(1, rc_t::OK);
1221 HW::item<route::table_id_t> hw_rd7_unbind(route::DEFAULT_TABLE, rc_t::OK);
1222 ADD_EXPECT(route_domain::create_cmd(hw_rd4_create, l3_proto_t::IPV4, 1));
1223 ADD_EXPECT(route_domain::create_cmd(hw_rd6_create, l3_proto_t::IPV6, 1));
1224 TRY_CHECK_RC(OM::write(ian, rd4));
1225
1226 /*
1227 * a couple of interfaces
1228 */
1229 std::string itf1_name = "af1";
1230 interface itf1(itf1_name,
1231 interface::type_t::AFPACKET,
1232 interface::admin_state_t::UP);
1233 HW::item<handle_t> hw_ifh(2, rc_t::OK);
1234 HW::item<interface::admin_state_t> hw_as_up(interface::admin_state_t::UP, rc_t::OK);
1235 HW::item<interface::admin_state_t> hw_as_down(interface::admin_state_t::DOWN, rc_t::OK);
1236 ADD_EXPECT(interface::af_packet_create_cmd(hw_ifh, itf1_name));
1237 ADD_EXPECT(interface::state_change_cmd(hw_as_up, hw_ifh));
1238 TRY_CHECK_RC(OM::write(ian, itf1));
1239
1240 std::string itf2_name = "af2";
1241 interface *itf2 = new interface(itf2_name,
1242 interface::type_t::AFPACKET,
1243 interface::admin_state_t::UP,
1244 rd4);
1245
1246 HW::item<handle_t> hw_ifh2(4, rc_t::OK);
1247 HW::item<interface::admin_state_t> hw_as_up2(interface::admin_state_t::UP, rc_t::OK);
1248 HW::item<interface::admin_state_t> hw_as_down2(interface::admin_state_t::DOWN, rc_t::OK);
1249 ADD_EXPECT(interface::af_packet_create_cmd(hw_ifh2, itf2_name));
1250 ADD_EXPECT(interface::state_change_cmd(hw_as_up2, hw_ifh2));
1251 ADD_EXPECT(interface::set_table_cmd(hw_rd4_bind, l3_proto_t::IPV4, hw_ifh2));
1252 ADD_EXPECT(interface::set_table_cmd(hw_rd6_bind, l3_proto_t::IPV6, hw_ifh2));
1253 TRY_CHECK_RC(OM::write(ian, *itf2));
1254
1255 /*
1256 * prefix on each interface
1257 */
1258 route::prefix_t pfx_10("10.10.10.10", 24);
1259 l3_binding *l3_10 = new l3_binding(itf1, pfx_10);
1260 HW::item<bool> hw_l3_10_bind(true, rc_t::OK);
1261 HW::item<bool> hw_l3_10_unbind(false, rc_t::OK);
1262 ADD_EXPECT(l3_binding::bind_cmd(hw_l3_10_bind, hw_ifh.data(), pfx_10));
1263 TRY_CHECK_RC(OM::write(ian, *l3_10));
1264 route::prefix_t pfx_11("11.11.11.11", 24);
1265 l3_binding *l3_11 = new l3_binding(*itf2, pfx_11);
1266 HW::item<bool> hw_l3_11_bind(true, rc_t::OK);
1267 HW::item<bool> hw_l3_11_unbind(false, rc_t::OK);
1268 ADD_EXPECT(l3_binding::bind_cmd(hw_l3_11_bind, hw_ifh2.data(), pfx_11));
1269 TRY_CHECK_RC(OM::write(ian, *l3_11));
1270
1271 /*
1272 * A route via interface 1 in the default table
1273 */
1274 route::prefix_t pfx_5("5.5.5.5", 32);
1275 boost::asio::ip::address nh_10 = boost::asio::ip::address::from_string("10.10.10.11");
1276 route::path *path_10 = new route::path(nh_10, itf1);
1277 route::ip_route *route_5 = new route::ip_route(pfx_5);
1278 route_5->add(*path_10);
1279 HW::item<bool> hw_route_5(true, rc_t::OK);
1280 ADD_EXPECT(route::ip_route::update_cmd(hw_route_5, 0, pfx_5, {*path_10}));
1281 TRY_CHECK_RC(OM::write(ian, *route_5));
1282
1283 /*
1284 * A route via interface 2 in the non-default table
1285 */
1286 boost::asio::ip::address nh_11 = boost::asio::ip::address::from_string("11.11.11.10");
1287 route::path *path_11 = new route::path(nh_11, *itf2);
1288 route::ip_route *route_5_2 = new route::ip_route(rd4, pfx_5);
1289 route_5_2->add(*path_11);
1290 HW::item<bool> hw_route_5_2(true, rc_t::OK);
1291 ADD_EXPECT(route::ip_route::update_cmd(hw_route_5_2, 1, pfx_5, {*path_11}));
1292 TRY_CHECK_RC(OM::write(ian, *route_5_2));
1293
1294 /*
1295 * An ARP entry for the neighbour on itf1
1296 */
1297 HW::item<bool> hw_neighbour(true, rc_t::OK);
1298 mac_address_t mac_n({0,1,2,4,5,6});
1299 neighbour *ne = new neighbour(itf1, mac_n, nh_10);
1300 ADD_EXPECT(neighbour::create_cmd(hw_neighbour, hw_ifh.data(), mac_n, nh_10));
1301 TRY_CHECK_RC(OM::write(ian, *ne));
1302
1303 /*
1304 * A DVR route
1305 */
1306 route::prefix_t pfx_6("6.6.6.6", 32);
1307 route::path *path_l2 = new route::path(*itf2, nh_proto_t::ETHERNET);
1308 route::ip_route *route_dvr = new route::ip_route(pfx_6);
1309 route_dvr->add(*path_l2);
1310 HW::item<bool> hw_route_dvr(true, rc_t::OK);
1311 ADD_EXPECT(route::ip_route::update_cmd(hw_route_dvr, 0, pfx_6, {*path_l2}));
1312 TRY_CHECK_RC(OM::write(ian, *route_dvr));
1313
1314 STRICT_ORDER_OFF();
1315 // delete the stack objects that hold references to others
1316 // so the OM::remove is the call that removes the last reference
1317 delete l3_11;
1318 delete l3_10;
1319 delete itf2;
1320 delete route_5;
1321 delete path_10;
1322 delete route_5_2;
1323 delete path_11;
1324 delete route_dvr;
1325 delete path_l2;
1326 delete ne;
1327 ADD_EXPECT(neighbour::delete_cmd(hw_neighbour, hw_ifh.data(), mac_n, nh_10));
1328 ADD_EXPECT(route::ip_route::delete_cmd(hw_route_dvr, 0, pfx_6));
1329 ADD_EXPECT(route::ip_route::delete_cmd(hw_route_5_2, 1, pfx_5));
1330 ADD_EXPECT(route::ip_route::delete_cmd(hw_route_5, 0, pfx_5));
1331 ADD_EXPECT(l3_binding::unbind_cmd(hw_l3_10_unbind, hw_ifh.data(), pfx_10));
1332 ADD_EXPECT(l3_binding::unbind_cmd(hw_l3_11_unbind, hw_ifh2.data(), pfx_11));
1333 ADD_EXPECT(interface::state_change_cmd(hw_as_down, hw_ifh));
1334 ADD_EXPECT(interface::af_packet_delete_cmd(hw_ifh, itf1_name));
1335 ADD_EXPECT(interface::set_table_cmd(hw_rd4_unbind, l3_proto_t::IPV4, hw_ifh2));
1336 ADD_EXPECT(interface::set_table_cmd(hw_rd4_unbind, l3_proto_t::IPV6, hw_ifh2));
1337 ADD_EXPECT(interface::state_change_cmd(hw_as_down2, hw_ifh2));
1338 ADD_EXPECT(interface::af_packet_delete_cmd(hw_ifh2, itf2_name));
1339 ADD_EXPECT(route_domain::delete_cmd(hw_rd4_delete, l3_proto_t::IPV4, 1));
1340 ADD_EXPECT(route_domain::delete_cmd(hw_rd6_delete, l3_proto_t::IPV6, 1));
1341
1342 TRY_CHECK(OM::remove(ian));
1343}
1344
1345BOOST_AUTO_TEST_CASE(test_nat) {
1346 VppInit vi;
1347 const std::string gs = "GeorgeSimenon";
1348 rc_t rc = rc_t::OK;
1349
1350 /*
1351 * Inside Interface
1352 */
1353 std::string itf_in_name = "inside";
1354 interface itf_in(itf_in_name,
1355 interface::type_t::AFPACKET,
1356 interface::admin_state_t::UP);
1357 HW::item<handle_t> hw_ifh(2, rc_t::OK);
1358 HW::item<interface::admin_state_t> hw_as_up(interface::admin_state_t::UP, rc_t::OK);
1359 HW::item<interface::admin_state_t> hw_as_down(interface::admin_state_t::DOWN, rc_t::OK);
1360 ADD_EXPECT(interface::af_packet_create_cmd(hw_ifh, itf_in_name));
1361 ADD_EXPECT(interface::state_change_cmd(hw_as_up, hw_ifh));
1362 TRY_CHECK_RC(OM::write(gs, itf_in));
1363
1364 /*
1365 * outside
1366 */
1367 std::string itf_out_name = "port-to";
1368 interface itf_out(itf_out_name,
1369 interface::type_t::AFPACKET,
1370 interface::admin_state_t::UP);
1371
1372 HW::item<handle_t> hw_ifh2(4, rc_t::OK);
1373 HW::item<interface::admin_state_t> hw_as_up2(interface::admin_state_t::UP, rc_t::OK);
1374 HW::item<interface::admin_state_t> hw_as_down2(interface::admin_state_t::DOWN, rc_t::OK);
1375
1376 ADD_EXPECT(interface::af_packet_create_cmd(hw_ifh2, itf_out_name));
1377 ADD_EXPECT(interface::state_change_cmd(hw_as_up2, hw_ifh2));
1378 TRY_CHECK_RC(OM::write(gs, itf_out));
1379
1380 /*
1381 * A NAT static mapping
1382 */
1383 boost::asio::ip::address in_addr = boost::asio::ip::address::from_string("10.0.0.1");
1384 boost::asio::ip::address_v4 out_addr = boost::asio::ip::address_v4::from_string("1.1.1.1");
1385
1386 nat_static ns(in_addr, out_addr);
1387 HW::item<bool> hw_ns(true, rc_t::OK);
1388
1389 ADD_EXPECT(nat_static::create_44_cmd(hw_ns, 0, in_addr.to_v4(), out_addr));
1390 TRY_CHECK_RC(OM::write(gs, ns));
1391
1392 /*
1393 * bind nat inside and out
1394 */
1395 nat_binding *nb_in = new nat_binding(itf_in,
1396 direction_t::INPUT,
1397 l3_proto_t::IPV4,
1398 nat_binding::zone_t::INSIDE);
1399 HW::item<bool> hw_nb_in(true, rc_t::OK);
1400
1401 ADD_EXPECT(nat_binding::bind_44_input_cmd(hw_nb_in, hw_ifh.data().value(),
1402 nat_binding::zone_t::INSIDE));
1403 TRY_CHECK_RC(OM::write(gs, *nb_in));
1404
1405 nat_binding *nb_out = new nat_binding(itf_out,
1406 direction_t::INPUT,
1407 l3_proto_t::IPV4,
1408 nat_binding::zone_t::OUTSIDE);
1409 HW::item<bool> hw_nb_out(true, rc_t::OK);
1410
1411 ADD_EXPECT(nat_binding::bind_44_input_cmd(hw_nb_out, hw_ifh2.data().value(),
1412 nat_binding::zone_t::OUTSIDE));
1413 TRY_CHECK_RC(OM::write(gs, *nb_out));
1414
1415
1416 STRICT_ORDER_OFF();
1417 delete nb_in;
1418 delete nb_out;
1419 ADD_EXPECT(nat_binding::unbind_44_input_cmd(hw_nb_in, hw_ifh.data().value(),
1420 nat_binding::zone_t::INSIDE));
1421 ADD_EXPECT(nat_binding::unbind_44_input_cmd(hw_nb_out, hw_ifh2.data().value(),
1422 nat_binding::zone_t::OUTSIDE));
1423 ADD_EXPECT(nat_static::delete_44_cmd(hw_ns, 0, in_addr.to_v4(), out_addr));
1424 ADD_EXPECT(interface::state_change_cmd(hw_as_down, hw_ifh));
1425 ADD_EXPECT(interface::af_packet_delete_cmd(hw_ifh, itf_in_name));
1426 ADD_EXPECT(interface::state_change_cmd(hw_as_down2, hw_ifh2));
1427 ADD_EXPECT(interface::af_packet_delete_cmd(hw_ifh2, itf_out_name));
1428
1429 TRY_CHECK(OM::remove(gs));
1430}
1431
1432BOOST_AUTO_TEST_CASE(test_interface_events) {
1433 VppInit vi;
1434 MockListener ml;
1435
1436 HW::item<bool> hw_want(true, rc_t::OK);
1437
1438 ADD_EXPECT(interface::events_cmd(ml));
1439 cmd* itf = new interface::events_cmd(ml);
1440
1441 HW::enqueue(itf);
1442 HW::write();
1443
1444 HW::dequeue(itf);
1445}
1446
1447BOOST_AUTO_TEST_SUITE_END()