FD.io VPP  v18.07-rc0-415-g6c78436
Vector Packet Processing
gbp_contract.c
Go to the documentation of this file.
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  */
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
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
49 {
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 *
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 
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 *
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  */
static clib_error_t * gbp_contract_show(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: gbp_contract.c:123
#define hash_set(h, key, value)
Definition: hash.h:255
#define hash_unset(h, key)
Definition: hash.h:261
#define NULL
Definition: clib.h:55
gbp_contract_db_t gbp_contract_db
Single contract DB instance.
Definition: gbp_contract.c:23
The key for an Contract.
Definition: gbp_contract.h:24
static clib_error_t * gbp_contract_cli(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: gbp_contract.c:68
EPG src,dst pair to ACL mapping table, aka contract DB.
Definition: gbp_contract.h:60
unsigned char u8
Definition: types.h:56
#define hash_foreach(key_var, value_var, h, body)
Definition: hash.h:442
#define clib_error_return(e, args...)
Definition: error.h:99
u32 epg_id_t
Definition: gbp_types.h:21
unsigned int u32
Definition: types.h:88
uword * gc_hash
We can form a u64 key from the pair, so use a simple hash table.
Definition: gbp_contract.h:65
struct _unformat_input_t unformat_input_t
void gbp_contract_delete(epg_id_t src_epg, epg_id_t dst_epg)
Definition: gbp_contract.c:37
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
vlib_main_t * vm
Definition: buffer.c:294
epg_id_t gck_src
source and destination EPGs for which the ACL applies
Definition: gbp_contract.h:33
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
long ctx[MAX_CONNS]
Definition: main.c:126
#define EPG_INVALID
Definition: gbp_types.h:22
void gbp_contract_update(epg_id_t src_epg, epg_id_t dst_epg, u32 acl_index)
Definition: gbp_contract.c:26
void gbp_contract_walk(gbp_contract_cb_t cb, void *ctx)
Definition: gbp_contract.c:48
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:681
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169
int(* gbp_contract_cb_t)(gbp_contract_t *gbpe, void *ctx)
Definition: gbp_contract.h:72