FD.io VPP  v16.09
Vector Packet Processing
policer_classify.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 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  */
16 
17 static void
20  u32 sw_if_index,
22  int feature_enable)
23 {
24  if (tid == POLICER_CLASSIFY_TABLE_L2)
25  {
26  l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_POLICER_CLAS,
27  feature_enable);
28  }
29  else
30  {
31  ip_lookup_main_t * lm;
32  ip_config_main_t * ipcm;
33  u32 ftype;
34  u32 ci;
35 
36  if (tid == POLICER_CLASSIFY_TABLE_IP4)
37  {
38  lm = &ip4_main.lookup_main;
40  }
41  else
42  {
43  lm = &ip6_main.lookup_main;
45  }
46 
47  ipcm = &lm->rx_config_mains[VNET_UNICAST];
48 
49  ci = ipcm->config_index_by_sw_if_index[sw_if_index];
50  ci = (feature_enable ? vnet_config_add_feature : vnet_config_del_feature)
51  (vnm, &ipcm->config_main, ci, ftype, 0, 0);
52 
53  ipcm->config_index_by_sw_if_index[sw_if_index] = ci;
54  pcm->vnet_config_main[tid] = &ipcm->config_main;
55  }
56 }
57 
59  u32 ip4_table_index, u32 ip6_table_index,
60  u32 l2_table_index, u32 is_add)
61 {
64  u32 pct[POLICER_CLASSIFY_N_TABLES] = {ip4_table_index, ip6_table_index,
65  l2_table_index};
66  u32 ti;
67 
68  /* Assume that we've validated sw_if_index in the API layer */
69 
70  for (ti = 0; ti < POLICER_CLASSIFY_N_TABLES; ti++)
71  {
72  if (pct[ti] == ~0)
73  continue;
74 
75  if (pool_is_free_index (vcm->tables, pct[ti]))
76  return VNET_API_ERROR_NO_SUCH_TABLE;
77 
79  (pcm->classify_table_index_by_sw_if_index[ti], sw_if_index, ~0);
80 
81  /* Reject any DEL operation with wrong sw_if_index */
82  if (!is_add &&
83  (pct[ti] != pcm->classify_table_index_by_sw_if_index[ti][sw_if_index]))
84  {
85  clib_warning ("Non-existent intf_idx=%d with table_index=%d for delete",
86  sw_if_index, pct[ti]);
87  return VNET_API_ERROR_NO_SUCH_TABLE;
88  }
89 
90  /* Return ok on ADD operaton if feature is already enabled */
91  if (is_add &&
92  pcm->classify_table_index_by_sw_if_index[ti][sw_if_index] != ~0)
93  return 0;
94 
95  vnet_policer_classify_feature_enable (vm, pcm, sw_if_index, ti, is_add);
96 
97  if (is_add)
98  pcm->classify_table_index_by_sw_if_index[ti][sw_if_index] = pct[ti];
99  else
100  pcm->classify_table_index_by_sw_if_index[ti][sw_if_index] = ~0;
101  }
102 
103 
104  return 0;
105 }
106 
107 static clib_error_t *
109  unformat_input_t * input,
110  vlib_cli_command_t * cmd)
111 {
112  vnet_main_t * vnm = vnet_get_main();
113  u32 sw_if_index = ~0;
114  u32 ip4_table_index = ~0;
115  u32 ip6_table_index = ~0;
116  u32 l2_table_index = ~0;
117  u32 is_add = 1;
118  u32 idx_cnt = 0;
119  int rv;
120 
122  {
123  if (unformat (input, "interface %U", unformat_vnet_sw_interface,
124  vnm, &sw_if_index))
125  ;
126  else if (unformat (input, "ip4-table %d", &ip4_table_index))
127  idx_cnt++;
128  else if (unformat (input, "ip6-table %d", &ip6_table_index))
129  idx_cnt++;
130  else if (unformat (input, "l2-table %d", &l2_table_index))
131  idx_cnt++;
132  else if (unformat (input, "del"))
133  is_add = 0;
134  else
135  break;
136  }
137 
138  if (sw_if_index == ~0)
139  return clib_error_return (0, "Interface must be specified.");
140 
141  if (!idx_cnt)
142  return clib_error_return (0, "Table index should be specified.");
143 
144  if (idx_cnt > 1)
145  return clib_error_return (0, "Only one table index per API is allowed.");
146 
147  rv = vnet_set_policer_classify_intfc(vm, sw_if_index, ip4_table_index,
148  ip6_table_index, l2_table_index, is_add);
149 
150  switch (rv)
151  {
152  case 0:
153  break;
154 
155  case VNET_API_ERROR_NO_MATCHING_INTERFACE:
156  return clib_error_return (0, "No such interface");
157 
158  case VNET_API_ERROR_NO_SUCH_ENTRY:
159  return clib_error_return (0, "No such classifier table");
160  }
161  return 0;
162 }
163 
164 VLIB_CLI_COMMAND (set_policer_classify_command, static) = {
165  .path = "set policer classify",
166  .short_help =
167  "set policer classify interface <int> [ip4-table <index>]\n"
168  " [ip6-table <index>] [l2-table <index>] [del]",
170 };
171 
172 static uword
173 unformat_table_type (unformat_input_t * input, va_list * va)
174 {
175  u32 * r = va_arg (*va, u32 *);
176  u32 tid;
177 
178  if (unformat (input, "ip4"))
180  else if (unformat (input, "ip6"))
182  else if (unformat (input, "l2"))
184  else
185  return 0;
186 
187  *r = tid;
188  return 1;
189 }
190 static clib_error_t *
192  unformat_input_t * input,
193  vlib_cli_command_t * cmd)
194 {
197  u32 * vec_tbl;
198  int i;
199 
200  if (unformat (input, "type %U", unformat_table_type, &type))
201  ;
202  else
203  return clib_error_return (0, "Type must be specified.");;
204 
205  if (type == POLICER_CLASSIFY_N_TABLES)
206  return clib_error_return (0, "Invalid table type.");
207 
209 
210  if (vec_len(vec_tbl))
211  vlib_cli_output (vm, "%10s%20s\t\t%s", "Intfc idx", "Classify table",
212  "Interface name");
213  else
214  vlib_cli_output (vm, "No tables configured.");
215 
216  for (i = 0; i < vec_len (vec_tbl); i++)
217  {
218  if (vec_elt(vec_tbl, i) == ~0)
219  continue;
220 
221  vlib_cli_output (vm, "%10d%20d\t\t%U", i, vec_elt(vec_tbl, i),
223  }
224 
225  return 0;
226 }
227 
228 VLIB_CLI_COMMAND (show_policer_classify_command, static) = {
229  .path = "show classify policer",
230  .short_help = "show classify policer type [ip4|ip6|l2]",
232 };
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
u32 * config_index_by_sw_if_index
Definition: lookup.h:369
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:966
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
u32 vnet_config_del_feature(vlib_main_t *vm, vnet_config_main_t *cm, u32 config_string_heap_index, u32 feature_index, void *feature_config, u32 n_feature_config_bytes)
Definition: config.c:300
ip_config_main_t rx_config_mains[VNET_N_CAST]
rx/tx interface/feature configuration.
Definition: lookup.h:452
ip_lookup_main_t lookup_main
Definition: ip4.h:115
unformat_function_t unformat_vnet_sw_interface
format_function_t format_vnet_sw_if_index_name
policer_classify_table_id_t
u32 ip4_unicast_rx_feature_policer_classify
Built-in unicast feature path indice, see ip_feature_init_cast()
Definition: ip4.h:152
vnet_main_t * vnet_get_main(void)
Definition: misc.c:45
#define clib_warning(format, args...)
Definition: error.h:59
u32 * classify_table_index_by_sw_if_index[POLICER_CLASSIFY_N_TABLES]
vnet_config_main_t * vnet_config_main[POLICER_CLASSIFY_N_TABLES]
static clib_error_t * set_policer_classify_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
u32 ip6_unicast_rx_feature_policer_classify
Definition: ip6.h:151
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:575
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:211
struct _vnet_classify_main vnet_classify_main_t
Definition: vnet_classify.h:50
unsigned int u32
Definition: types.h:88
ip6_main_t ip6_main
Definition: ip6_forward.c:2955
ip_lookup_main_t lookup_main
Definition: ip6.h:110
static clib_error_t * show_policer_classify_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
u32 vnet_config_add_feature(vlib_main_t *vm, vnet_config_main_t *cm, u32 config_string_heap_index, u32 feature_index, void *feature_config, u32 n_feature_config_bytes)
Definition: config.c:239
static uword unformat_table_type(unformat_input_t *input, va_list *va)
u64 uword
Definition: types.h:112
#define vec_elt(v, i)
Get vector value at index i.
u32 l2input_intf_bitmap_enable(u32 sw_if_index, u32 feature_bitmap, u32 enable)
Enable (or disable) the feature in the bitmap for the given interface.
Definition: l2_input.c:501
vnet_classify_main_t * vnet_classify_main
VLIB_CLI_COMMAND(set_interface_ip_source_and_port_range_check_command, static)
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169
policer_classify_main_t policer_classify_main
ip4_main_t ip4_main
Global ip4 main structure.
Definition: ip4_forward.c:1578
static void vnet_policer_classify_feature_enable(vlib_main_t *vnm, policer_classify_main_t *pcm, u32 sw_if_index, policer_classify_table_id_t tid, int feature_enable)
int vnet_set_policer_classify_intfc(vlib_main_t *vm, u32 sw_if_index, u32 ip4_table_index, u32 ip6_table_index, u32 l2_table_index, u32 is_add)
#define clib_error_return(e, args...)
Definition: error.h:111
struct _unformat_input_t unformat_input_t
#define vec_validate_init_empty(V, I, INIT)
Make sure vector is long enough for given index and initialize empty space (no header, unspecified alignment)
Definition: vec.h:445
vnet_config_main_t config_main
Definition: lookup.h:367