GBP V2
[vpp.git] / src / plugins / gbp / gbp_contract.c
1 /*
2  * gbp.h : Group Based Policy
3  *
4  * Copyright (c) 2018 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <plugins/gbp/gbp.h>
19
20 /**
21  * Single contract DB instance
22  */
23 gbp_contract_db_t gbp_contract_db;
24
25 void
26 gbp_contract_update (epg_id_t src_epg, epg_id_t dst_epg, u32 acl_index)
27 {
28   gbp_contract_key_t key = {
29     .gck_src = src_epg,
30     .gck_dst = dst_epg,
31   };
32
33   hash_set (gbp_contract_db.gc_hash, key.as_u64, acl_index);
34 }
35
36 void
37 gbp_contract_delete (epg_id_t src_epg, epg_id_t dst_epg)
38 {
39   gbp_contract_key_t key = {
40     .gck_src = src_epg,
41     .gck_dst = dst_epg,
42   };
43
44   hash_unset (gbp_contract_db.gc_hash, key.as_u64);
45 }
46
47 void
48 gbp_contract_walk (gbp_contract_cb_t cb, void *ctx)
49 {
50   gbp_contract_key_t key;
51   u32 acl_index;
52
53   /* *INDENT-OFF* */
54   hash_foreach(key.as_u64, acl_index, gbp_contract_db.gc_hash,
55   ({
56     gbp_contract_t gbpc = {
57       .gc_key = key,
58       .gc_acl_index = acl_index,
59     };
60
61     if (!cb(&gbpc, ctx))
62       break;
63   }));
64   /* *INDENT-ON* */
65 }
66
67 static clib_error_t *
68 gbp_contract_cli (vlib_main_t * vm,
69                   unformat_input_t * input, vlib_cli_command_t * cmd)
70 {
71   epg_id_t src_epg_id = EPG_INVALID, dst_epg_id = EPG_INVALID;
72   u32 acl_index = ~0;
73   u8 add = 1;
74
75   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
76     {
77       if (unformat (input, "add"))
78         add = 1;
79       else if (unformat (input, "del"))
80         add = 0;
81       else if (unformat (input, "src-epg %d", &src_epg_id))
82         ;
83       else if (unformat (input, "dst-epg %d", &dst_epg_id))
84         ;
85       else if (unformat (input, "acl-index %d", &acl_index))
86         ;
87       else
88         break;
89     }
90
91   if (EPG_INVALID == src_epg_id)
92     return clib_error_return (0, "Source EPG-ID must be specified");
93   if (EPG_INVALID == dst_epg_id)
94     return clib_error_return (0, "Destination EPG-ID must be specified");
95
96   if (add)
97     {
98       gbp_contract_update (src_epg_id, dst_epg_id, acl_index);
99     }
100   else
101     {
102       gbp_contract_delete (src_epg_id, dst_epg_id);
103     }
104
105   return (NULL);
106 }
107
108 /*?
109  * Configure a GBP Contract
110  *
111  * @cliexpar
112  * @cliexstart{set gbp contract [del] src-epg <ID> dst-epg <ID> acl-index <ACL>}
113  * @cliexend
114  ?*/
115 VLIB_CLI_COMMAND (gbp_contract_cli_node, static) =
116 {
117 .path = "gbp contract",.short_help =
118     "gbp contract [del] src-epg <ID> dst-epg <ID> acl-index <ACL>",.function
119     = gbp_contract_cli,};
120 /* *INDENT-ON* */
121
122 static clib_error_t *
123 gbp_contract_show (vlib_main_t * vm,
124                    unformat_input_t * input, vlib_cli_command_t * cmd)
125 {
126   gbp_contract_key_t key;
127   epg_id_t epg_id;
128
129   vlib_cli_output (vm, "Contracts:");
130
131   /* *INDENT-OFF* */
132   hash_foreach (key.as_u64, epg_id, gbp_contract_db.gc_hash,
133   {
134     vlib_cli_output (vm, "  {%d,%d} -> %d", key.gck_src,
135                      key.gck_dst, epg_id);
136   });
137   /* *INDENT-ON* */
138
139   return (NULL);
140 }
141
142 /*?
143  * Show Group Based Policy Contracts
144  *
145  * @cliexpar
146  * @cliexstart{show gbp contract}
147  * @cliexend
148  ?*/
149 /* *INDENT-OFF* */
150 VLIB_CLI_COMMAND (gbp_contract_show_node, static) = {
151   .path = "show gbp contract",
152   .short_help = "show gbp contract\n",
153   .function = gbp_contract_show,
154 };
155 /* *INDENT-ON* */
156
157 /*
158  * fd.io coding-style-patch-verification: ON
159  *
160  * Local Variables:
161  * eval: (c-set-style "gnu")
162  * End:
163  */