Click
Loading...
Searching...
No Matches
flowrulecache.hh
1// -*- c-basic-offset: 4 -*-
2/*
3 * flowrulecache.hh -- A flow rule cache API for 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_FLOWRULECACHE_H
19#define CLICK_FLOWRULECACHE_H
20
21#include <click/error.hh>
22#include <click/hashmap.hh>
23#include <click/dpdkdevice.hh>
24
25CLICK_DECLS
26
30#if RTE_VERSION >= RTE_VERSION_NUM(20,2,0,0)
31
33 public:
34 FlowRuleCache(portid_t port_id, bool verbose, bool debug_mode, ErrorHandler *errh)
35 : _port_id(port_id), _rules_nb(0), _next_rule_id(0),
36 _rules(), _internal_rule_map(), _matched_pkts(), _matched_bytes() {
37 assert(_port_id >= 0);
38 _errh = new ErrorVeneer(errh);
39 assert(_errh);
40 _verbose = verbose;
41 _debug_mode = debug_mode;
42 }
43
46 }
47
48 // Device methods
49 portid_t get_port_id();
51
52 // Search methods
53 bool has_rules();
54 bool global_rule_id_exists(const uint32_t &rule_id);
55 bool internal_rule_id_exists(const uint32_t &int_rule_id);
56 uint32_t global_from_internal_rule_id(const uint32_t &int_rule_id);
57 int32_t internal_from_global_rule_id(const uint32_t &rule_id);
58 template<typename T> void sort_rule_ids_inc(Vector<T> &rule_ids_vec);
59 template<typename T> void sort_rule_ids_dec(Vector<T> &rule_ids_vec);
60 Vector<uint32_t> global_rule_ids(const bool increasing = true);
61 Vector<uint32_t> internal_rule_ids(const bool increasing = true);
62 Vector<uint32_t> internal_rule_ids_counters(const bool increasing = true);
63 HashMap<uint32_t, String> *rules_map_by_core_id(const int &core_id);
64 Vector<String> rules_list_by_core_id(const int &core_id);
66 String get_rule_by_global_id(const uint32_t &rule_id);
67 String get_rule_by_internal_id(const uint32_t &int_rule_id);
68
69 // Flow Rule Cache methods
71 uint32_t next_internal_rule_id();
72 void set_next_internal_rule_id(uint32_t next_id);
74 const int &core_id, const uint32_t &rule_id,
75 const uint32_t &int_rule_id, const String rule
76 );
78 const int &core_id, const uint32_t &rule_id,
79 const uint32_t &int_rule_id, String rule
80 );
81 int32_t delete_rule_by_global_id(const uint32_t &rule_id);
82 String delete_rules_by_internal_id(const uint32_t *int_rule_ids, const uint32_t &rules_nb);
84 int32_t flush_rules_from_cache();
85
86 // Flow Rule Cache monitoring methods
87 void set_matched_packets(const uint32_t &int_rule_id, uint64_t value);
88 uint64_t get_matched_packets(const uint32_t &int_rule_id);
89 void set_matched_bytes(const uint32_t &int_rule_id, uint64_t value);
90 uint64_t get_matched_bytes(const uint32_t &int_rule_id);
91 inline uint32_t get_rule_counter() { return _rules_nb; };
92 void initialize_rule_counters(uint32_t *int_rule_ids, const uint32_t &rules_nb);
93 void delete_rule_counters(uint32_t *int_rule_ids, const uint32_t &rules_nb);
94 void cache_consistency_check(const int32_t &target_number_of_rules);
95 void correlate_candidate_id_with_cache(int32_t &candidate);
97
98 private:
99 // NIC's port ID associated with this Flow Rule Cache
100 portid_t _port_id;
101
102 // Flow rules' counter
103 uint32_t _rules_nb;
104
105 // Next available rule ID
106 uint32_t _next_rule_id;
107
108 // Maps CPU cores to a map of global rule IDs -> rules
109 HashMap<int, HashMap<uint32_t, String> *> _rules;
110
111 // Maps global rule IDs to internal NIC rule IDs
112 HashMap<uint32_t, uint32_t> _internal_rule_map;
113
114 // Matched packets and bytes per rule ID
115 HashMap<uint32_t, uint64_t> _matched_pkts;
116 HashMap<uint32_t, uint64_t> _matched_bytes;
117
118 // An error handler
119 ErrorVeneer *_errh;
120
121 // Set stdout verbosity
122 bool _verbose;
123 bool _debug_mode;
124
125 // Methods to facilitate the mapping between ONOS and NIC rule IDs
126 bool store_rule_id_mapping(const uint32_t &rule_id, const uint32_t &int_rule_id);
127 bool delete_rule_id_mapping(const uint32_t &rule_id);
128
129 // Methods to verify cache consistency
130 bool verify_transactions(const Vector<uint32_t> &int_vec, const Vector<uint32_t> &glb_vec);
131};
132
138template<typename T>
139void
141{
142 for (uint32_t i = 1; i < rule_ids_vec.size(); ++i) {
143 for (uint32_t j = 0; j < rule_ids_vec.size(); ++j) {
144 if (rule_ids_vec[j] > rule_ids_vec[i]) {
145 T temp = rule_ids_vec[j];
146 rule_ids_vec[j] = rule_ids_vec[i];
147 rule_ids_vec[i] = temp;
148 }
149 }
150 }
151}
152
158template<typename T>
159void
161{
162 for (uint32_t i = 0; i < rule_ids_vec.size(); ++i) {
163 for (uint32_t j = i; j < rule_ids_vec.size(); ++j) {
164 if (rule_ids_vec[j] > rule_ids_vec[i]) {
165 T temp = rule_ids_vec[j];
166 rule_ids_vec[j] = rule_ids_vec[i];
167 rule_ids_vec[i] = temp;
168 }
169 }
170 }
171}
172
173#endif /* RTE_VERSION >= RTE_VERSION_NUM(20,2,0,0) */
174
175CLICK_ENDDECLS
176
177#endif /* CLICK_FLOWRULECACHE_H */
Error reporting class.
Definition error.hh:90
Base class for ErrorHandlers that forward messages.
Definition error.hh:618
Definition flowrulecache.hh:32
String delete_rules_by_internal_id(const uint32_t *int_rule_ids, const uint32_t &rules_nb)
Definition flowrulecache.cc:605
bool global_rule_id_exists(const uint32_t &rule_id)
Definition flowrulecache.cc:71
uint64_t get_matched_bytes(const uint32_t &int_rule_id)
Definition flowrulecache.cc:756
Vector< int > cores_with_rules()
Definition flowrulecache.cc:309
HashMap< uint32_t, String > * rules_map_by_core_id(const int &core_id)
Definition flowrulecache.cc:257
uint64_t get_matched_packets(const uint32_t &int_rule_id)
Definition flowrulecache.cc:726
String get_rule_by_global_id(const uint32_t &rule_id)
Definition flowrulecache.cc:333
int insert_rule_in_flow_cache(const int &core_id, const uint32_t &rule_id, const uint32_t &int_rule_id, const String rule)
Definition flowrulecache.cc:472
bool has_rules()
Definition flowrulecache.cc:59
void correlate_candidate_id_with_cache(int32_t &candidate)
Definition flowrulecache.cc:950
Vector< uint32_t > global_rule_ids(const bool increasing=true)
Definition flowrulecache.cc:162
void set_matched_packets(const uint32_t &int_rule_id, uint64_t value)
Definition flowrulecache.cc:711
int32_t delete_rule_by_global_id(const uint32_t &rule_id)
Definition flowrulecache.cc:543
void set_next_internal_rule_id(uint32_t next_id)
Definition flowrulecache.cc:457
int32_t internal_from_global_rule_id(const uint32_t &rule_id)
Definition flowrulecache.cc:134
void sort_rule_ids_dec(Vector< T > &rule_ids_vec)
Definition flowrulecache.hh:160
Vector< String > rules_list_by_core_id(const int &core_id)
Definition flowrulecache.cc:274
uint32_t next_internal_rule_id()
Definition flowrulecache.cc:446
void set_matched_bytes(const uint32_t &int_rule_id, uint64_t value)
Definition flowrulecache.cc:741
void delete_rule_counters(uint32_t *int_rule_ids, const uint32_t &rules_nb)
Definition flowrulecache.cc:798
bool internal_rule_id_exists(const uint32_t &int_rule_id)
Definition flowrulecache.cc:83
Vector< uint32_t > internal_rule_ids(const bool increasing=true)
Definition flowrulecache.cc:203
bool update_rule_in_flow_cache(const int &core_id, const uint32_t &rule_id, const uint32_t &int_rule_id, String rule)
Definition flowrulecache.cc:527
portid_t get_port_id()
Definition flowrulecache.cc:35
void flush_rule_counters()
Definition flowrulecache.cc:999
uint32_t global_from_internal_rule_id(const uint32_t &int_rule_id)
Definition flowrulecache.cc:97
void sort_rule_ids_inc(Vector< T > &rule_ids_vec)
Definition flowrulecache.hh:140
String get_rule_by_internal_id(const uint32_t &int_rule_id)
Definition flowrulecache.cc:372
Vector< uint32_t > internal_rule_ids_counters(const bool increasing=true)
Definition flowrulecache.cc:230
void cache_consistency_check(const int32_t &target_number_of_rules)
Definition flowrulecache.cc:824
String get_device_address()
Definition flowrulecache.cc:46
int32_t flush_rules_from_cache()
Definition flowrulecache.cc:666
void initialize_rule_counters(uint32_t *int_rule_ids, const uint32_t &rules_nb)
Definition flowrulecache.cc:772
int32_t currently_max_internal_rule_id()
Definition flowrulecache.cc:435
A string of characters.
Definition string.hh:22
Vector template.
Definition vector.hh:111
size_type size() const
Return the number of elements.
Definition vector.hh:231
Flexible error handling classes.