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