6cda0d2b3ba6eeb26f8f87b33a9cb114251ba63c
[vpp.git] / extras / vom / vom / ip_punt_redirect.hpp
1 /*
2  * Copyright (c) 2018 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #ifndef __VOM_IP_PUNT_REDIRECT_H__
17 #define __VOM_IP_PUNT_REDIRECT_H__
18
19 #include "vom/hw.hpp"
20 #include "vom/inspect.hpp"
21 #include "vom/interface.hpp"
22 #include "vom/object_base.hpp"
23 #include "vom/om.hpp"
24 #include "vom/singular_db.hpp"
25
26 namespace VOM {
27 /**
28  * A representation of IP punt_redirect configuration on an interface
29  */
30 class ip_punt_redirect : public object_base
31 {
32 public:
33   /**
34    * Construct a new object matching the desried state
35    *
36    * @param rx_itf - The interface from where the punt traffic should come.
37    * @param tx_itf - The interface to which traffic should be redirected.
38    * @param addr - The next hop ip address to redirect the traffic.
39    */
40   ip_punt_redirect(const interface& rx_itf,
41                    const interface& tx_itf,
42                    const boost::asio::ip::address& addr);
43
44   /**
45    * Copy Constructor
46    */
47   ip_punt_redirect(const ip_punt_redirect& o);
48
49   /**
50    * Destructor
51    */
52   ~ip_punt_redirect();
53
54   /**
55    * Return the 'singular instance' of the ip_punt_redirect that matches this
56    * object
57    */
58   std::shared_ptr<ip_punt_redirect> singular() const;
59
60   /**
61    * convert to string format for debug purposes
62    */
63   std::string to_string() const;
64
65   /**
66    * Dump all ip_punt_redirects into the stream provided
67    */
68   static void dump(std::ostream& os);
69
70   /**
71    * The key type for ip_punt_redirects
72    */
73   typedef interface::key_t key_t;
74
75   /**
76    * Find an singular instance in the DB for the interface passed
77    */
78   static std::shared_ptr<ip_punt_redirect> find(const interface& i);
79
80 private:
81   /**
82    * Class definition for listeners to OM events
83    */
84   class event_handler : public OM::listener, public inspect::command_handler
85   {
86   public:
87     event_handler();
88     virtual ~event_handler() = default;
89
90     /**
91      * Handle a populate event
92      */
93     void handle_populate(const client_db::key_t& key);
94
95     /**
96      * Handle a replay event
97      */
98     void handle_replay();
99
100     /**
101      * Show the object in the Singular DB
102      */
103     void show(std::ostream& os);
104
105     /**
106      * Get the sortable Id of the listener
107      */
108     dependency_t order() const;
109   };
110
111   /**
112    * event_handler to register with OM
113    */
114   static event_handler m_evh;
115
116   /**
117    * Enquue commonds to the VPP command Q for the update
118    */
119   void update(const ip_punt_redirect& obj);
120
121   /**
122    * Find or add the singular instance in the DB
123    */
124   static std::shared_ptr<ip_punt_redirect> find_or_add(
125     const ip_punt_redirect& temp);
126
127   /*
128    * It's the VPPHW class that updates the objects in HW
129    */
130   friend class OM;
131
132   /**
133    * It's the singular_db class that calls replay
134    */
135   friend class singular_db<key_t, ip_punt_redirect>;
136
137   /**
138    * Sweep/reap the object if still stale
139    */
140   void sweep(void);
141
142   /**
143    * replay the object to create it in hardware
144    */
145   void replay(void);
146
147   /**
148    * A reference counting pointer the interface that requires an address.
149    */
150   const std::shared_ptr<interface> m_rx_itf;
151   /**
152    * A reference counting pointer the interface that has an address.
153    */
154   const std::shared_ptr<interface> m_tx_itf;
155
156   /**
157    * host Ip Prefix to redirect traffic to
158    */
159   const boost::asio::ip::address m_addr;
160
161   /**
162    * HW configuration for the binding. The bool representing the
163    * do/don't bind.
164    */
165   HW::item<bool> m_config;
166
167   /**
168    * A map of all ip punt redirect keyed against a combination of the interface.
169    */
170   static singular_db<key_t, ip_punt_redirect> m_db;
171 };
172 };
173
174 /*
175  * fd.io coding-style-patch-verification: ON
176  *
177  * Local Variables:
178  * eval: (c-set-style "mozilla")
179  * End:
180  */
181
182 #endif