linux-cp: A V2 variant of pair create API that returns the host
[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 #define foreach_lcp_itf_pair_flag _ (STALE, 0, "stale")
25
26 typedef enum lip_flag_t_
27 {
28 #define _(a, b, c) LIP_FLAG_##a = (1 << b),
29   foreach_lcp_itf_pair_flag
30 #undef _
31 } lip_flag_t;
32
33 typedef enum
34 {
35   LCP_ITF_HOST_TAP = 0,
36   LCP_ITF_HOST_TUN = 1,
37 } lip_host_type_t;
38
39 #define N_LCP_ITF_HOST (LCP_ITF_HOST_TUN + 1)
40
41 typedef struct lcp_itf_phy_adj
42 {
43   adj_index_t adj_index[N_AF];
44 } lcp_itf_phy_adj_t;
45
46 /**
47  * A pair of interfaces
48  */
49 typedef struct lcp_itf_pair_t_
50 {
51   u32 lip_host_sw_if_index;       /* VPP's sw_if_index for the host tap */
52   u32 lip_phy_sw_if_index;        /* VPP's sw_if_index for the phy */
53   u8 *lip_host_name;              /* linux's name for the tap */
54   u32 lip_vif_index;              /* linux's index for the tap */
55   u8 *lip_namespace;              /* namespace in which the tap lives */
56   lip_host_type_t lip_host_type;  /* type of host interface */
57   lcp_itf_phy_adj_t lip_phy_adjs; /* adjacencies for phy l3 interface */
58   lip_flag_t lip_flags;           /* Flags */
59   u8 lip_rewrite_len;             /* The length of an L2 MAC rewrite */
60   f64 lip_create_ts;              /* Timestamp of creation */
61 } lcp_itf_pair_t;
62 extern lcp_itf_pair_t *lcp_itf_pair_pool;
63
64 extern vlib_node_registration_t lcp_ethernet_node;
65
66 u8 *format_lcp_itf_pair (u8 *s, va_list *args);
67 void lcp_itf_pair_show (u32 phy_sw_if_index);
68 u32 lcp_itf_num_pairs (void);
69
70 /**
71  * Get an interface-pair object from its VPP index
72  */
73 extern lcp_itf_pair_t *lcp_itf_pair_get (index_t index);
74
75 /**
76  * Find a interface-pair object from the host interface
77  *
78  * @param host_sw_if_index host interface
79  * @return VPP's object index
80  */
81 extern index_t lcp_itf_pair_find_by_vif (u32 vif_index);
82
83 /**
84  * Create an interface-pair
85  *
86  * @return error code
87  */
88 extern int lcp_itf_pair_add (u32 host_sw_if_index, u32 phy_sw_if_index,
89                              u8 *host_name, u32 host_index,
90                              lip_host_type_t host_type, u8 *ns);
91 extern int lcp_itf_pair_add_sub (u32 vif, u8 *host_name, u32 sub_sw_if_index,
92                                  u32 phy_sw_if_index, u8 *ns);
93 extern int lcp_itf_pair_del (u32 phy_sw_if_index);
94
95 /**
96  * Create an interface-pair from PHY sw_if_index and tap name.
97  *
98  * @return error code
99  */
100 extern int lcp_itf_pair_create (u32 phy_sw_if_index, u8 *host_if_name,
101                                 lip_host_type_t host_if_type, u8 *ns,
102                                 u32 *host_sw_if_indexp);
103
104 /**
105  * Delete a LCP_ITF_PAIR
106  */
107 extern int lcp_itf_pair_delete (u32 phy_sw_if_index);
108
109 /**
110  * Callback function invoked during a walk of all interface-pairs
111  */
112 typedef walk_rc_t (*lcp_itf_pair_walk_cb_t) (index_t index, void *ctx);
113
114 /**
115  * Walk/visit each of the interface pairs
116  */
117 extern void lcp_itf_pair_walk (lcp_itf_pair_walk_cb_t cb, void *ctx);
118
119 /**
120  * Begin and End the replace process
121  */
122 extern int lcp_itf_pair_replace_begin (void);
123 extern int lcp_itf_pair_replace_end (void);
124
125 /**
126  * Retreive the pair in the DP
127  */
128 extern index_t *lip_db_by_phy;
129 extern u32 *lip_db_by_host;
130
131 always_inline index_t
132 lcp_itf_pair_find_by_phy (u32 phy_sw_if_index)
133 {
134   if (phy_sw_if_index >= vec_len (lip_db_by_phy))
135     return INDEX_INVALID;
136   return (lip_db_by_phy[phy_sw_if_index]);
137 }
138
139 always_inline index_t
140 lcp_itf_pair_find_by_host (u32 host_sw_if_index)
141 {
142   if (host_sw_if_index >= vec_len (lip_db_by_host))
143     return INDEX_INVALID;
144   return (lip_db_by_host[host_sw_if_index]);
145 }
146
147 /**
148  * manage interface auto creation
149  */
150 void lcp_set_auto_intf (u8 is_auto);
151 int lcp_auto_intf (void);
152
153 /*
154  * fd.io coding-style-patch-verification: ON
155  *
156  * Local Variables:
157  * eval: (c-set-style "gnu")
158  * End:
159  */
160
161 #endif