linux-cp: Add VPP->Linux synchronization
[vpp.git] / src / plugins / linux-cp / lcp_interface.h
1 /*
2  * Copyright (c) 2020 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 #ifndef __LCP_ITF_PAIR_H__
16 #define __LCP_ITF_PAIR_H__
17
18 #include <vnet/dpo/dpo.h>
19 #include <vnet/adj/adj.h>
20 #include <vnet/ip/ip_types.h>
21
22 #include <plugins/linux-cp/lcp.h>
23
24 extern vlib_log_class_t lcp_itf_pair_logger;
25
26 #define LCP_ITF_PAIR_DBG(...)                                                 \
27   vlib_log_debug (lcp_itf_pair_logger, __VA_ARGS__);
28
29 #define LCP_ITF_PAIR_INFO(...)                                                \
30   vlib_log_info (lcp_itf_pair_logger, __VA_ARGS__);
31
32 #define LCP_ITF_PAIR_NOTICE(...)                                              \
33   vlib_log_notice (lcp_itf_pair_logger, __VA_ARGS__);
34
35 #define LCP_ITF_PAIR_WARN(...)                                                \
36   vlib_log_warn (lcp_itf_pair_logger, __VA_ARGS__);
37
38 #define LCP_ITF_PAIR_ERR(...) vlib_log_err (lcp_itf_pair_logger, __VA_ARGS__);
39
40 #define foreach_lcp_itf_pair_flag _ (STALE, 0, "stale")
41
42 typedef enum lip_flag_t_
43 {
44 #define _(a, b, c) LIP_FLAG_##a = (1 << b),
45   foreach_lcp_itf_pair_flag
46 #undef _
47 } lip_flag_t;
48
49 typedef enum
50 {
51   LCP_ITF_HOST_TAP = 0,
52   LCP_ITF_HOST_TUN = 1,
53 } lip_host_type_t;
54
55 #define N_LCP_ITF_HOST (LCP_ITF_HOST_TUN + 1)
56
57 typedef struct lcp_itf_phy_adj
58 {
59   adj_index_t adj_index[N_AF];
60 } lcp_itf_phy_adj_t;
61
62 /**
63  * A pair of interfaces
64  */
65 typedef struct lcp_itf_pair_t_
66 {
67   u32 lip_host_sw_if_index;       /* VPP's sw_if_index for the host tap */
68   u32 lip_phy_sw_if_index;        /* VPP's sw_if_index for the phy */
69   u8 *lip_host_name;              /* linux's name for the tap */
70   u32 lip_vif_index;              /* linux's index for the tap */
71   u8 *lip_namespace;              /* namespace in which the tap lives */
72   lip_host_type_t lip_host_type;  /* type of host interface */
73   lcp_itf_phy_adj_t lip_phy_adjs; /* adjacencies for phy l3 interface */
74   lip_flag_t lip_flags;           /* Flags */
75   u8 lip_rewrite_len;             /* The length of an L2 MAC rewrite */
76   f64 lip_create_ts;              /* Timestamp of creation */
77 } lcp_itf_pair_t;
78 extern lcp_itf_pair_t *lcp_itf_pair_pool;
79
80 extern vlib_node_registration_t lcp_ethernet_node;
81
82 u8 *format_lcp_itf_pair (u8 *s, va_list *args);
83 void lcp_itf_pair_show (u32 phy_sw_if_index);
84 u32 lcp_itf_num_pairs (void);
85
86 /**
87  * Get an interface-pair object from its VPP index
88  */
89 extern lcp_itf_pair_t *lcp_itf_pair_get (index_t index);
90
91 /**
92  * Find a interface-pair object from the host interface
93  *
94  * @param host_sw_if_index host interface
95  * @return VPP's object index
96  */
97 extern index_t lcp_itf_pair_find_by_vif (u32 vif_index);
98
99 /**
100  * Create an interface-pair
101  *
102  * @return error code
103  */
104 extern int lcp_itf_pair_add (u32 host_sw_if_index, u32 phy_sw_if_index,
105                              u8 *host_name, u32 host_index,
106                              lip_host_type_t host_type, u8 *ns);
107 extern int lcp_itf_pair_del (u32 phy_sw_if_index);
108
109 /**
110  * Create an interface-pair from PHY sw_if_index and tap name.
111  *
112  * @return error code
113  */
114 extern int lcp_itf_pair_create (u32 phy_sw_if_index, u8 *host_if_name,
115                                 lip_host_type_t host_if_type, u8 *ns,
116                                 u32 *host_sw_if_indexp);
117
118 /**
119  * Delete a LCP_ITF_PAIR
120  */
121 extern int lcp_itf_pair_delete (u32 phy_sw_if_index);
122
123 /**
124  * Callback function invoked during a walk of all interface-pairs
125  */
126 typedef walk_rc_t (*lcp_itf_pair_walk_cb_t) (index_t index, void *ctx);
127
128 /**
129  * Walk/visit each of the interface pairs
130  */
131 extern void lcp_itf_pair_walk (lcp_itf_pair_walk_cb_t cb, void *ctx);
132
133 /**
134  * Begin and End the replace process
135  */
136 extern int lcp_itf_pair_replace_begin (void);
137 extern int lcp_itf_pair_replace_end (void);
138
139 /**
140  * Retreive the pair in the DP
141  */
142 extern index_t *lip_db_by_phy;
143 extern u32 *lip_db_by_host;
144
145 always_inline index_t
146 lcp_itf_pair_find_by_phy (u32 phy_sw_if_index)
147 {
148   if (phy_sw_if_index >= vec_len (lip_db_by_phy))
149     return INDEX_INVALID;
150   return (lip_db_by_phy[phy_sw_if_index]);
151 }
152
153 always_inline index_t
154 lcp_itf_pair_find_by_host (u32 host_sw_if_index)
155 {
156   if (host_sw_if_index >= vec_len (lip_db_by_host))
157     return INDEX_INVALID;
158   return (lip_db_by_host[host_sw_if_index]);
159 }
160
161 typedef void (*lcp_itf_pair_add_cb_t) (lcp_itf_pair_t *);
162 typedef void (*lcp_itf_pair_del_cb_t) (lcp_itf_pair_t *);
163
164 typedef struct lcp_itf_pair_vft
165 {
166   lcp_itf_pair_add_cb_t pair_add_fn;
167   lcp_itf_pair_del_cb_t pair_del_fn;
168 } lcp_itf_pair_vft_t;
169
170 void lcp_itf_pair_register_vft (lcp_itf_pair_vft_t *lcp_itf_vft);
171
172 /**
173  * sub-interface auto creation/deletion for LCP
174  */
175 void lcp_set_auto_subint (u8 is_auto);
176 int lcp_auto_subint (void);
177
178 /**
179  * sync state changes from VPP into LCP
180  */
181 void lcp_set_sync (u8 is_auto);
182 int lcp_sync (void);
183
184 /* Set TAP and Linux host link state */
185 void lcp_itf_set_link_state (const lcp_itf_pair_t *lip, u8 state);
186
187 /* Set any VPP L3 addresses on Linux host device */
188 void lcp_itf_set_interface_addr (const lcp_itf_pair_t *lip);
189
190 /* Sync all state from VPP to a specific Linux device, all sub-interfaces
191  * of a hardware interface, or all interfaces in the system.
192  *
193  * Note: in some circumstances, this syncer will (have to) make changes to
194  * the VPP interface, for example if its MTU is greater than its parent.
195  * See the function for rationale.
196  */
197 void lcp_itf_pair_sync_state (lcp_itf_pair_t *lip);
198 void lcp_itf_pair_sync_state_hw (vnet_hw_interface_t *hi);
199 void lcp_itf_pair_sync_state_all ();
200
201 /*
202  * fd.io coding-style-patch-verification: ON
203  *
204  * Local Variables:
205  * eval: (c-set-style "gnu")
206  * End:
207  */
208
209 #endif