Click
Loading...
Searching...
No Matches
flowrulemanager.hh
1// -*- c-basic-offset: 4 -*-
2/*
3 * flowrulemanager.hh -- Flow rule manager API for DPDK-based NICs, based on DPDK's Flow API
4 *
5 * Copyright (c) 2018 Georgios Katsikas, RISE SICS & KTH Royal Institute of Technology
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, subject to the conditions
10 * listed in the Click LICENSE file. These conditions include: you must
11 * preserve this copyright notice, and you cannot mention the copyright
12 * holders in advertising related to the Software without their permission.
13 * The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
14 * notice is a summary of the Click LICENSE file; the license in that file is
15 * legally binding.
16 */
17
18#ifndef CLICK_FLOWRULEMANAGER_H
19#define CLICK_FLOWRULEMANAGER_H
20
21#include <click/error.hh>
22#include <click/hashmap.hh>
23#include <click/hashtable.hh>
24#include <click/dpdkdevice.hh>
25#if RTE_VERSION >= RTE_VERSION_NUM(20,2,0,0)
26 #include <click/flowrulecache.hh>
27 #include <click/flowruleparser.hh>
28#endif
29
30CLICK_DECLS
31
35#if RTE_VERSION >= RTE_VERSION_NUM(20,2,0,0)
36
37class DPDKDevice;
38
39class RuleTiming {
40 public:
41 RuleTiming(portid_t pt_id) : port_id(pt_id) {
42 }
43
44 ~RuleTiming() {
45 }
46
47 portid_t port_id; // The NIC to measure
48 uint32_t rules_nb; // Log the number of rules being installed/deleted
49 float latency_ms; // Measure rule installation/deletion latency (ms)
50 float rules_per_sec; // Measure rule installation/deletion rate (rules/sec)
51 Timestamp start, end;
52
53 void update(const uint32_t &rules_nb) {
54 this->rules_nb = rules_nb;
55 this->latency_ms = (float) (end - start).usecval() / (float) 1000;
56 this->rules_per_sec = (rules_nb > 0) ? (float) (rules_nb * 1000) / this->latency_ms : 0;
57 }
58};
59
60class FlowRuleManager {
61 public:
62 FlowRuleManager();
63 FlowRuleManager(portid_t port_id, ErrorHandler *errh);
64 ~FlowRuleManager();
65
66 // DPDKDevice mode for the Flow Rule Manager
67 static String DISPATCHING_MODE;
68
69 // Supported Flow Rule Manager handlers (called from FromDPDKDevice)
70 static String FLOW_RULE_ADD;
71 static String FLOW_RULE_DEL;
72 static String FLOW_RULE_IDS_GLB;
73 static String FLOW_RULE_IDS_INT;
74 static String FLOW_RULE_PACKET_HITS;
75 static String FLOW_RULE_BYTE_COUNT;
76 static String FLOW_RULE_AGGR_STATS;
77 static String FLOW_RULE_LIST;
78 static String FLOW_RULE_LIST_WITH_HITS;
79 static String FLOW_RULE_COUNT;
80 static String FLOW_RULE_COUNT_WITH_HITS;
81 static String FLOW_RULE_ISOLATE;
82 static String FLOW_RULE_FLUSH;
83
84 // For debugging
85 static bool DEF_VERBOSITY;
86 static bool DEF_DEBUG_MODE;
87
88 // Set of flow rule items supported by the Flow API
89 static HashMap<int, String> flow_item;
90
91 // Set of flow rule actions supported by the Flow API
92 static HashMap<int, String> flow_action;
93
94 // Global table of DPDK ports mapped to their Flow Rule Manager objects
95 static HashTable<portid_t, FlowRuleManager *> dev_flow_rule_mgr;
96
97 // Map of ports to Flow Rule Manager instances
98 static HashTable<portid_t, FlowRuleManager *> flow_rule_manager_map();
99
100 // Cleans the mappings between ports and Flow Rule Manager instances
101 static void clean_flow_rule_manager_map();
102
103 // Acquires a Flow Rule Manager instance on a port
104 static FlowRuleManager *get_flow_rule_mgr(const portid_t &port_id, ErrorHandler *errh = NULL);
105
106 // Parser initialization
107 static struct cmdline *flow_rule_parser(ErrorHandler *errh);
108
109 // Get a flow rule cache associated with a Flow Rule Manager
110 FlowRuleCache *flow_rule_cache();
111
112 // Deletes the error handler of this element
113 inline void delete_error_handler() { if (_errh) delete _errh; };
114
115 // Port ID handlers
116 inline void set_port_id(const portid_t &port_id) {
117 _port_id = port_id;
118 };
119 inline portid_t get_port_id() { return _port_id; };
120
121 // Activation/deactivation handlers
122 inline void set_active(const bool &active) {
123 _active = active;
124 };
125 inline bool active() { return _active; };
126
127 // Verbosity handlers
128 inline void set_verbose(const bool &verbose) {
129 _verbose = verbose;
130 };
131 inline bool verbose() { return _verbose; };
132
133 inline void set_debug_mode(const bool &debug_mode) {
134 _debug_mode = debug_mode;
135 };
136 inline bool debug_mode() { return _debug_mode; };
137
138 // Rules' file handlers
139 inline void set_rules_filename(const String &file) {
140 _rules_filename = file;
141 };
142 inline String rules_filename() { return _rules_filename; };
143
144 // Calibrates flow rule cache before inserting new rules
145 void flow_rule_cache_calibrate(const uint32_t *int_rule_ids, const uint32_t &rules_nb);
146 void flow_rule_cache_calibrate(const HashMap<uint32_t, String> &rules_map);
147
148 // Install NIC flow rules from a file
149 int32_t flow_rules_add_from_file(const String &filename);
150
151 // Update NIC flow rules
152 int32_t flow_rules_update(const HashMap<uint32_t, String> &rules_map, bool by_controller = true, int core_id = -1);
153
154 // Loads a set of rules from a file to memory
155 String flow_rules_from_file_to_string(const String &filename);
156
157 // Install flow rule(s) in a NIC
158 int flow_rules_install(const String &rules, const uint32_t &rules_nb, const bool verbose=true);
159 int flow_rule_install(
160 const uint32_t &int_rule_id, const uint32_t &rule_id,
161 const int &core_id, const String &rule,
162 const bool with_cache = true,
163 const bool verbose=true
164 );
165
166 // Verify the presence/absence of a list of rules in/from the NIC
167 int flow_rules_verify(const Vector<uint32_t> &int_rule_ids_vec, const Vector<uint32_t> &old_int_rule_ids_vec);
168 int flow_rules_verify_presence(const Vector<uint32_t> &int_rule_ids_vec);
169 int flow_rules_verify_absence(const Vector<uint32_t> &old_int_rule_ids_vec);
170
171 // Return a flow rule object with a specific ID
172 struct port_flow *flow_rule_get(const uint32_t &int_rule_id);
173
174 // Delete a batch of flow rules from a NIC
175 int32_t flow_rules_delete(const Vector<uint32_t> &old_int_rule_ids_vec, const bool with_cache = true);
176 int32_t flow_rules_delete(uint32_t *int_rule_ids, const uint32_t &rules_nb, const bool with_cache = true);
177
178 // Query flow rule statistics
179 String flow_rule_query(const uint32_t &int_rule_id, int64_t &matched_pkts, int64_t &matched_bytes);
180
181 // Query aggregate flow rule statistics
182 String flow_rule_aggregate_stats();
183
184 // Counts the number of rules with hits in a NIC
185 uint32_t flow_rules_with_hits_count();
186
187 // Counts the number of rules in a NIC (using the cache)
188 uint32_t flow_rules_count();
189
190 // Counts the number of rules in a NIC (in hardware)
191 uint32_t flow_rules_count_explicit();
192
193 // Compares NIC and cache rule counts and asserts inconsistency
194 void nic_and_cache_counts_agree();
195
196 // Lists all NIC flow rules
197 String flow_rules_list(const bool only_matching_rules = false);
198
199 // Lists all installed (internal + global flow rule IDs along with counters
200 String flow_rule_ids_internal_counters();
201 String flow_rule_ids_internal_cache();
202 String flow_rule_ids_internal_nic();
203 String flow_rule_ids_internal(const bool from_nic = true);
204 String flow_rule_ids_global();
205
206 // Flush all of the rules from a NIC
207 uint32_t flow_rules_flush();
208
209 // Verify the consistency of the NIC and Flow Cache upon a rule update
210 void flow_rule_consistency_check(const int32_t &target_number_of_rules);
211
212 // Filters unwanted components from rule
213 static bool flow_rule_filter(String &rule);
214
215 // Returns a rule token after an input keyword
216 static String fetch_token_after_keyword(char *rule, const String &keyword);
217
218 // Returns statistics related to rule installation/deletion
219 void min_avg_max(float &min, float &mean, float &max, const bool install = true, const bool latency = true);
220
221 // Methods to access per port rule installation/deletion statistics
222 static inline void add_rule_inst_stats(const RuleTiming &rits) {
223 Vector<RuleTiming> *rule_stats_vec = _rule_inst_stats_map.findp(rits.port_id);
224 if (!rule_stats_vec) {
225 Vector<RuleTiming> new_vec;
226 new_vec.push_back(rits);
227 _rule_inst_stats_map.insert(rits.port_id, new_vec);
228 } else {
229 rule_stats_vec->push_back(rits);
230 }
231 }
232 static inline void add_rule_del_stats(const RuleTiming &rdts) {
233 Vector<RuleTiming> *rule_stats_vec = _rule_del_stats_map.findp(rdts.port_id);
234 if (!rule_stats_vec) {
235 Vector<RuleTiming> new_vec;
236 new_vec.push_back(rdts);
237 _rule_del_stats_map.insert(rdts.port_id, new_vec);
238 } else {
239 rule_stats_vec->push_back(rdts);
240 }
241 }
242
243 private:
244 // Device ID
245 portid_t _port_id;
246
247 // Indicates whether Flow Rule Manager is active for a given device
248 bool _active;
249
250 // Set stdout verbosity
251 bool _verbose;
252 bool _debug_mode;
253
254 // Filename that contains the rules to be installed
255 String _rules_filename;
256
257 // A dedicated error handler
258 ErrorVeneer *_errh;
259
260 // A flow rule cache associated with the port of this Flow Rule Manager
261 FlowRuleCache *_flow_rule_cache;
262
263 // Isolated mode guarantees that all ingress traffic comes from defined flow rules only (current and future)
264 static HashMap<portid_t, bool> _isolated;
265
266 // Flow rule commands' parser
267 static struct cmdline *_parser;
268
269 // Map of ports to their rule installation/deletion statistics
270 static HashMap<portid_t, Vector<RuleTiming>> _rule_inst_stats_map;
271 static HashMap<portid_t, Vector<RuleTiming>> _rule_del_stats_map;
272
273 // Restrict ingress traffic to the defined flow rules
274 static int flow_rules_isolate(const portid_t &port_id, const int &set);
275
276 // Pre-populate the supported matches and actions on relevant maps
277 void populate_supported_flow_items_and_actions();
278
279 // Sorts a list of flow rules by group, priority, and ID
280 void flow_rules_sort(struct rte_port *port, struct port_flow **sorted_rules);
281
282 // Verify that the NIC has the right number of rules
283 void nic_consistency_check(const int32_t &target_number_of_rules);
284
285};
286
287#endif /* RTE_VERSION >= RTE_VERSION_NUM(20,2,0,0) */
288
289CLICK_ENDDECLS
290
291#endif /* CLICK_FLOWRULEMANAGER_H */
Error reporting class.
Definition error.hh:90
Base class for ErrorHandlers that forward messages.
Definition error.hh:618
Definition flowrulecache.hh:32
Hash table template.
Definition hashtable.hh:570
A string of characters.
Definition string.hh:22
Represents a moment or interval in time.
Definition timestamp.hh:125
value_type usecval() const
Return this timestamp's interval length in microseconds.
Definition timestamp.hh:281
Vector template.
Definition vector.hh:111
void push_back(value_argument_type v)
Append element v.
Definition vector.hh:399
Flexible error handling classes.
Click's hash table container template.