]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
04f48a68 | 2 | * Copyright (c) 2009-2017 Nicira, Inc. |
43253595 | 3 | * Copyright (c) 2010 Jean Tourrilhes - HP-Labs. |
064af421 | 4 | * |
a14bc59f BP |
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: | |
064af421 | 8 | * |
a14bc59f BP |
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. | |
064af421 BP |
16 | */ |
17 | ||
18 | #include <config.h> | |
064af421 BP |
19 | #include <errno.h> |
20 | #include <inttypes.h> | |
064af421 BP |
21 | #include <stdbool.h> |
22 | #include <stdlib.h> | |
448a4b2f | 23 | #include <unistd.h> |
b598f214 | 24 | |
52a90c29 | 25 | #include "bitmap.h" |
b598f214 | 26 | #include "bundles.h" |
10a24935 | 27 | #include "byte-order.h" |
064af421 | 28 | #include "classifier.h" |
da4a6191 | 29 | #include "connectivity.h" |
19a87e36 | 30 | #include "connmgr.h" |
064af421 | 31 | #include "coverage.h" |
b598f214 | 32 | #include "dp-packet.h" |
ca0f572c | 33 | #include "hash.h" |
ee89ea7b | 34 | #include "openvswitch/hmap.h" |
064af421 | 35 | #include "netdev.h" |
09246b99 | 36 | #include "nx-match.h" |
b598f214 | 37 | #include "ofproto.h" |
5bee6e26 | 38 | #include "ofproto-provider.h" |
064af421 BP |
39 | #include "openflow/nicira-ext.h" |
40 | #include "openflow/openflow.h" | |
d271907f BW |
41 | #include "openvswitch/dynamic-string.h" |
42 | #include "openvswitch/meta-flow.h" | |
b598f214 | 43 | #include "openvswitch/ofp-actions.h" |
d271907f BW |
44 | #include "openvswitch/ofp-errors.h" |
45 | #include "openvswitch/ofp-msgs.h" | |
25d436fb | 46 | #include "openvswitch/ofp-print.h" |
d271907f BW |
47 | #include "openvswitch/ofp-util.h" |
48 | #include "openvswitch/ofpbuf.h" | |
49 | #include "openvswitch/vlog.h" | |
06a0f3e2 | 50 | #include "ovs-rcu.h" |
064af421 BP |
51 | #include "packets.h" |
52 | #include "pinsched.h" | |
064af421 | 53 | #include "poll-loop.h" |
254750ce | 54 | #include "random.h" |
da4a6191 | 55 | #include "seq.h" |
ee89ea7b | 56 | #include "openvswitch/shash.h" |
0d085684 | 57 | #include "simap.h" |
e8f9a7bb | 58 | #include "smap.h" |
b3c01ed3 | 59 | #include "sset.h" |
064af421 | 60 | #include "timeval.h" |
9558d2a5 | 61 | #include "tun-metadata.h" |
c4617b3c | 62 | #include "unaligned.h" |
4f2cad2c | 63 | #include "unixctl.h" |
ee89ea7b | 64 | #include "util.h" |
064af421 | 65 | |
d98e6007 | 66 | VLOG_DEFINE_THIS_MODULE(ofproto); |
064af421 | 67 | |
d76f09ea | 68 | COVERAGE_DEFINE(ofproto_flush); |
d76f09ea BP |
69 | COVERAGE_DEFINE(ofproto_packet_out); |
70 | COVERAGE_DEFINE(ofproto_queue_req); | |
71 | COVERAGE_DEFINE(ofproto_recv_openflow); | |
72 | COVERAGE_DEFINE(ofproto_reinit_ports); | |
d76f09ea BP |
73 | COVERAGE_DEFINE(ofproto_update_port); |
74 | ||
f017d986 JR |
75 | /* Default fields to use for prefix tries in each flow table, unless something |
76 | * else is configured. */ | |
77 | const enum mf_field_id default_prefix_fields[2] = | |
78 | { MFF_IPV4_DST, MFF_IPV4_SRC }; | |
79 | ||
d0918789 BP |
80 | /* oftable. */ |
81 | static void oftable_init(struct oftable *); | |
82 | static void oftable_destroy(struct oftable *); | |
878ae780 | 83 | |
254750ce BP |
84 | static void oftable_set_name(struct oftable *, const char *name); |
85 | ||
6787a49f | 86 | static enum ofperr evict_rules_from_table(struct oftable *) |
834fe5cb | 87 | OVS_REQUIRES(ofproto_mutex); |
82c22d34 BP |
88 | static void oftable_configure_eviction(struct oftable *, |
89 | unsigned int eviction, | |
90 | const struct mf_subfield *fields, | |
91 | size_t n_fields) | |
7394b8fb | 92 | OVS_REQUIRES(ofproto_mutex); |
254750ce | 93 | |
82c22d34 BP |
94 | /* This is the only combination of OpenFlow eviction flags that OVS supports: a |
95 | * combination of OF1.4+ importance, the remaining lifetime of the flow, and | |
96 | * fairness based on user-specified fields. */ | |
97 | #define OFPROTO_EVICTION_FLAGS \ | |
98 | (OFPTMPEF14_OTHER | OFPTMPEF14_IMPORTANCE | OFPTMPEF14_LIFETIME) | |
99 | ||
254750ce BP |
100 | /* A set of rules within a single OpenFlow table (oftable) that have the same |
101 | * values for the oftable's eviction_fields. A rule to be evicted, when one is | |
102 | * needed, is taken from the eviction group that contains the greatest number | |
103 | * of rules. | |
104 | * | |
105 | * An oftable owns any number of eviction groups, each of which contains any | |
106 | * number of rules. | |
107 | * | |
108 | * Membership in an eviction group is imprecise, based on the hash of the | |
109 | * oftable's eviction_fields (in the eviction_group's id_node.hash member). | |
110 | * That is, if two rules have different eviction_fields, but those | |
111 | * eviction_fields hash to the same value, then they will belong to the same | |
112 | * eviction_group anyway. | |
113 | * | |
114 | * (When eviction is not enabled on an oftable, we don't track any eviction | |
115 | * groups, to save time and space.) */ | |
116 | struct eviction_group { | |
117 | struct hmap_node id_node; /* In oftable's "eviction_groups_by_id". */ | |
118 | struct heap_node size_node; /* In oftable's "eviction_groups_by_size". */ | |
119 | struct heap rules; /* Contains "struct rule"s. */ | |
120 | }; | |
121 | ||
3d900aa7 BP |
122 | static bool choose_rule_to_evict(struct oftable *table, struct rule **rulep) |
123 | OVS_REQUIRES(ofproto_mutex); | |
f70b94de BP |
124 | static uint64_t rule_eviction_priority(struct ofproto *ofproto, struct rule *) |
125 | OVS_REQUIRES(ofproto_mutex); | |
3d900aa7 BP |
126 | static void eviction_group_add_rule(struct rule *) |
127 | OVS_REQUIRES(ofproto_mutex); | |
128 | static void eviction_group_remove_rule(struct rule *) | |
129 | OVS_REQUIRES(ofproto_mutex); | |
f29152ca | 130 | |
a9b22b7f | 131 | static void rule_criteria_init(struct rule_criteria *, uint8_t table_id, |
eb391b76 | 132 | const struct match *match, int priority, |
44e0c35d | 133 | ovs_version_t version, |
a9b22b7f BP |
134 | ovs_be64 cookie, ovs_be64 cookie_mask, |
135 | ofp_port_t out_port, uint32_t out_group); | |
dd51dae2 BP |
136 | static void rule_criteria_require_rw(struct rule_criteria *, |
137 | bool can_write_readonly); | |
a9b22b7f BP |
138 | static void rule_criteria_destroy(struct rule_criteria *); |
139 | ||
35f48b8b BP |
140 | static enum ofperr collect_rules_loose(struct ofproto *, |
141 | const struct rule_criteria *, | |
142 | struct rule_collection *); | |
143 | ||
35f48b8b BP |
144 | struct learned_cookie { |
145 | union { | |
146 | /* In struct ofproto's 'learned_cookies' hmap. */ | |
147 | struct hmap_node hmap_node OVS_GUARDED_BY(ofproto_mutex); | |
148 | ||
149 | /* In 'dead_cookies' list when removed from hmap. */ | |
ca6ba700 | 150 | struct ovs_list list_node; |
35f48b8b BP |
151 | } u; |
152 | ||
153 | /* Key. */ | |
154 | ovs_be64 cookie OVS_GUARDED_BY(ofproto_mutex); | |
155 | uint8_t table_id OVS_GUARDED_BY(ofproto_mutex); | |
156 | ||
157 | /* Number of references from "learn" actions. | |
158 | * | |
159 | * When this drops to 0, all of the flows in 'table_id' with the specified | |
160 | * 'cookie' are deleted. */ | |
161 | int n OVS_GUARDED_BY(ofproto_mutex); | |
162 | }; | |
163 | ||
164 | static const struct ofpact_learn *next_learn_with_delete( | |
165 | const struct rule_actions *, const struct ofpact_learn *start); | |
166 | ||
167 | static void learned_cookies_inc(struct ofproto *, const struct rule_actions *) | |
168 | OVS_REQUIRES(ofproto_mutex); | |
169 | static void learned_cookies_dec(struct ofproto *, const struct rule_actions *, | |
ca6ba700 | 170 | struct ovs_list *dead_cookies) |
35f48b8b | 171 | OVS_REQUIRES(ofproto_mutex); |
ca6ba700 | 172 | static void learned_cookies_flush(struct ofproto *, struct ovs_list *dead_cookies) |
35f48b8b BP |
173 | OVS_REQUIRES(ofproto_mutex); |
174 | ||
d0918789 | 175 | /* ofport. */ |
15aaf599 | 176 | static void ofport_destroy__(struct ofport *) OVS_EXCLUDED(ofproto_mutex); |
9aad5a5a | 177 | static void ofport_destroy(struct ofport *, bool del); |
3a414a0a DDP |
178 | static bool ofport_is_mtu_overridden(const struct ofproto *, |
179 | const struct ofport *); | |
d0918789 | 180 | |
01c77073 | 181 | static int update_port(struct ofproto *, const char *devname); |
d0918789 BP |
182 | static int init_ports(struct ofproto *); |
183 | static void reinit_ports(struct ofproto *); | |
7ee20df1 | 184 | |
fdcea803 GS |
185 | static long long int ofport_get_usage(const struct ofproto *, |
186 | ofp_port_t ofp_port); | |
187 | static void ofport_set_usage(struct ofproto *, ofp_port_t ofp_port, | |
188 | long long int last_used); | |
865b26a4 | 189 | static void ofport_remove_usage(struct ofproto *, ofp_port_t ofp_port); |
fdcea803 GS |
190 | |
191 | /* Ofport usage. | |
192 | * | |
193 | * Keeps track of the currently used and recently used ofport values and is | |
194 | * used to prevent immediate recycling of ofport values. */ | |
195 | struct ofport_usage { | |
196 | struct hmap_node hmap_node; /* In struct ofproto's "ofport_usage" hmap. */ | |
197 | ofp_port_t ofp_port; /* OpenFlow port number. */ | |
198 | long long int last_used; /* Last time the 'ofp_port' was used. LLONG_MAX | |
199 | represents in-use ofports. */ | |
200 | }; | |
201 | ||
254750ce | 202 | /* rule. */ |
f695ebfa JR |
203 | static void ofproto_rule_send_removed(struct rule *) |
204 | OVS_EXCLUDED(ofproto_mutex); | |
834fe5cb | 205 | static bool rule_is_readonly(const struct rule *); |
bc5e6a91 JR |
206 | static void ofproto_rule_insert__(struct ofproto *, struct rule *) |
207 | OVS_REQUIRES(ofproto_mutex); | |
802f84ff JR |
208 | static void ofproto_rule_remove__(struct ofproto *, struct rule *) |
209 | OVS_REQUIRES(ofproto_mutex); | |
254750ce | 210 | |
0a0d9385 | 211 | /* The source of an OpenFlow request. |
baae3d02 | 212 | * |
0a0d9385 JR |
213 | * A table modification request can be generated externally, via OpenFlow, or |
214 | * internally through a function call. This structure indicates the source of | |
215 | * an OpenFlow-generated table modification. For an internal flow_mod, it | |
216 | * isn't meaningful and thus supplied as NULL. */ | |
217 | struct openflow_mod_requester { | |
baae3d02 | 218 | struct ofconn *ofconn; /* Connection on which flow_mod arrived. */ |
c84d8691 | 219 | const struct ofp_header *request; |
baae3d02 BP |
220 | }; |
221 | ||
d0918789 | 222 | /* OpenFlow. */ |
5bacd5cd JR |
223 | static enum ofperr ofproto_rule_create(struct ofproto *, struct cls_rule *, |
224 | uint8_t table_id, ovs_be64 new_cookie, | |
225 | uint16_t idle_timeout, | |
226 | uint16_t hard_timeout, | |
227 | enum ofputil_flow_mod_flags flags, | |
228 | uint16_t importance, | |
229 | const struct ofpact *ofpacts, | |
230 | size_t ofpacts_len, | |
39c94593 | 231 | struct rule **new_rule) |
5bacd5cd | 232 | OVS_NO_THREAD_SAFETY_ANALYSIS; |
39c94593 | 233 | |
5bacd5cd JR |
234 | static void replace_rule_start(struct ofproto *, struct ofproto_flow_mod *, |
235 | struct rule *old_rule, struct rule *new_rule) | |
dd27be82 | 236 | OVS_REQUIRES(ofproto_mutex); |
39c94593 | 237 | |
6787a49f JR |
238 | static void replace_rule_revert(struct ofproto *, struct rule *old_rule, |
239 | struct rule *new_rule) | |
39c94593 JR |
240 | OVS_REQUIRES(ofproto_mutex); |
241 | ||
81dee635 | 242 | static void replace_rule_finish(struct ofproto *, struct ofproto_flow_mod *, |
0a0d9385 | 243 | const struct openflow_mod_requester *, |
39c94593 JR |
244 | struct rule *old_rule, struct rule *new_rule, |
245 | struct ovs_list *dead_cookies) | |
dd27be82 | 246 | OVS_REQUIRES(ofproto_mutex); |
39c94593 | 247 | static void delete_flows__(struct rule_collection *, |
9ca4a86f | 248 | enum ofp_flow_removed_reason, |
0a0d9385 | 249 | const struct openflow_mod_requester *) |
15aaf599 | 250 | OVS_REQUIRES(ofproto_mutex); |
834fe5cb | 251 | |
db88b35c | 252 | static bool ofproto_group_exists(const struct ofproto *, uint32_t group_id); |
b20f4073 | 253 | static void handle_openflow(struct ofconn *, const struct ofpbuf *); |
5bacd5cd JR |
254 | static enum ofperr ofproto_flow_mod_init(struct ofproto *, |
255 | struct ofproto_flow_mod *, | |
2c7ee524 JR |
256 | const struct ofputil_flow_mod *fm, |
257 | struct rule *) | |
5bacd5cd | 258 | OVS_EXCLUDED(ofproto_mutex); |
8be00367 JR |
259 | static enum ofperr ofproto_flow_mod_start(struct ofproto *, |
260 | struct ofproto_flow_mod *) | |
1f42be1c | 261 | OVS_REQUIRES(ofproto_mutex); |
6dd3c787 JR |
262 | static void ofproto_flow_mod_revert(struct ofproto *, |
263 | struct ofproto_flow_mod *) | |
264 | OVS_REQUIRES(ofproto_mutex); | |
8be00367 JR |
265 | static void ofproto_flow_mod_finish(struct ofproto *, |
266 | struct ofproto_flow_mod *, | |
0a0d9385 | 267 | const struct openflow_mod_requester *) |
1f42be1c | 268 | OVS_REQUIRES(ofproto_mutex); |
baae3d02 | 269 | static enum ofperr handle_flow_mod__(struct ofproto *, |
7338102b | 270 | const struct ofputil_flow_mod *, |
0a0d9385 | 271 | const struct openflow_mod_requester *) |
15aaf599 | 272 | OVS_EXCLUDED(ofproto_mutex); |
65e0be10 BP |
273 | static void calc_duration(long long int start, long long int now, |
274 | uint32_t *sec, uint32_t *nsec); | |
f29152ca | 275 | |
d0918789 BP |
276 | /* ofproto. */ |
277 | static uint64_t pick_datapath_id(const struct ofproto *); | |
278 | static uint64_t pick_fallback_dpid(void); | |
279 | static void ofproto_destroy__(struct ofproto *); | |
ada3428f | 280 | static void update_mtu(struct ofproto *, struct ofport *); |
0670b5d0 | 281 | static void update_mtu_ofproto(struct ofproto *); |
6b3f7f02 | 282 | static void meter_delete(struct ofproto *, uint32_t first, uint32_t last); |
062fea06 | 283 | static void meter_insert_rule(struct rule *); |
f29152ca | 284 | |
d0918789 | 285 | /* unixctl. */ |
abe529af | 286 | static void ofproto_unixctl_init(void); |
f29152ca | 287 | |
abe529af BP |
288 | /* All registered ofproto classes, in probe order. */ |
289 | static const struct ofproto_class **ofproto_classes; | |
290 | static size_t n_ofproto_classes; | |
291 | static size_t allocated_ofproto_classes; | |
7aa697dd | 292 | |
15aaf599 BP |
293 | /* Global lock that protects all flow table operations. */ |
294 | struct ovs_mutex ofproto_mutex = OVS_MUTEX_INITIALIZER; | |
abe7b10f | 295 | |
e79a6c83 | 296 | unsigned ofproto_flow_limit = OFPROTO_FLOW_LIMIT_DEFAULT; |
72310b04 | 297 | unsigned ofproto_max_idle = OFPROTO_MAX_IDLE_DEFAULT; |
380f49c4 | 298 | |
e79a6c83 | 299 | size_t n_handlers, n_revalidators; |
6567010f | 300 | |
f797957a | 301 | /* Map from datapath name to struct ofproto, for use by unixctl commands. */ |
abe529af | 302 | static struct hmap all_ofprotos = HMAP_INITIALIZER(&all_ofprotos); |
ebe482fd | 303 | |
e1b1d06a JP |
304 | /* Initial mappings of port to OpenFlow number mappings. */ |
305 | static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports); | |
306 | ||
abe529af | 307 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); |
f29152ca | 308 | |
40358701 GS |
309 | /* The default value of true waits for flow restore. */ |
310 | static bool flow_restore_wait = true; | |
311 | ||
b0408fca JP |
312 | /* Must be called to initialize the ofproto library. |
313 | * | |
314 | * The caller may pass in 'iface_hints', which contains an shash of | |
315 | * "iface_hint" elements indexed by the interface's name. The provider | |
316 | * may use these hints to describe the startup configuration in order to | |
317 | * reinitialize its state. The caller owns the provided data, so a | |
318 | * provider will make copies of anything required. An ofproto provider | |
319 | * will remove any existing state that is not described by the hint, and | |
320 | * may choose to remove it all. */ | |
321 | void | |
322 | ofproto_init(const struct shash *iface_hints) | |
abe529af | 323 | { |
e1b1d06a | 324 | struct shash_node *node; |
b0408fca | 325 | size_t i; |
f29152ca | 326 | |
b0408fca JP |
327 | ofproto_class_register(&ofproto_dpif_class); |
328 | ||
e1b1d06a JP |
329 | /* Make a local copy, since we don't own 'iface_hints' elements. */ |
330 | SHASH_FOR_EACH(node, iface_hints) { | |
331 | const struct iface_hint *orig_hint = node->data; | |
332 | struct iface_hint *new_hint = xmalloc(sizeof *new_hint); | |
333 | const char *br_type = ofproto_normalize_type(orig_hint->br_type); | |
334 | ||
335 | new_hint->br_name = xstrdup(orig_hint->br_name); | |
336 | new_hint->br_type = xstrdup(br_type); | |
337 | new_hint->ofp_port = orig_hint->ofp_port; | |
338 | ||
339 | shash_add(&init_ofp_ports, node->name, new_hint); | |
340 | } | |
341 | ||
b0408fca | 342 | for (i = 0; i < n_ofproto_classes; i++) { |
e1b1d06a | 343 | ofproto_classes[i]->init(&init_ofp_ports); |
abe529af | 344 | } |
0fc1f5c0 HH |
345 | |
346 | ofproto_unixctl_init(); | |
abe529af | 347 | } |
f29152ca | 348 | |
abe529af BP |
349 | /* 'type' should be a normalized datapath type, as returned by |
350 | * ofproto_normalize_type(). Returns the corresponding ofproto_class | |
351 | * structure, or a null pointer if there is none registered for 'type'. */ | |
352 | static const struct ofproto_class * | |
353 | ofproto_class_find__(const char *type) | |
354 | { | |
355 | size_t i; | |
f29152ca | 356 | |
abe529af BP |
357 | for (i = 0; i < n_ofproto_classes; i++) { |
358 | const struct ofproto_class *class = ofproto_classes[i]; | |
359 | struct sset types; | |
360 | bool found; | |
064af421 | 361 | |
abe529af BP |
362 | sset_init(&types); |
363 | class->enumerate_types(&types); | |
364 | found = sset_contains(&types, type); | |
365 | sset_destroy(&types); | |
bcf84111 | 366 | |
abe529af BP |
367 | if (found) { |
368 | return class; | |
369 | } | |
370 | } | |
371 | VLOG_WARN("unknown datapath type %s", type); | |
372 | return NULL; | |
373 | } | |
064af421 | 374 | |
abe529af BP |
375 | /* Registers a new ofproto class. After successful registration, new ofprotos |
376 | * of that type can be created using ofproto_create(). */ | |
377 | int | |
378 | ofproto_class_register(const struct ofproto_class *new_class) | |
379 | { | |
380 | size_t i; | |
7aa697dd | 381 | |
abe529af BP |
382 | for (i = 0; i < n_ofproto_classes; i++) { |
383 | if (ofproto_classes[i] == new_class) { | |
384 | return EEXIST; | |
385 | } | |
386 | } | |
064af421 | 387 | |
abe529af BP |
388 | if (n_ofproto_classes >= allocated_ofproto_classes) { |
389 | ofproto_classes = x2nrealloc(ofproto_classes, | |
390 | &allocated_ofproto_classes, | |
391 | sizeof *ofproto_classes); | |
392 | } | |
393 | ofproto_classes[n_ofproto_classes++] = new_class; | |
394 | return 0; | |
395 | } | |
064af421 | 396 | |
abe529af BP |
397 | /* Unregisters a datapath provider. 'type' must have been previously |
398 | * registered and not currently be in use by any ofprotos. After | |
399 | * unregistration new datapaths of that type cannot be opened using | |
400 | * ofproto_create(). */ | |
401 | int | |
402 | ofproto_class_unregister(const struct ofproto_class *class) | |
403 | { | |
404 | size_t i; | |
76ce9432 | 405 | |
abe529af BP |
406 | for (i = 0; i < n_ofproto_classes; i++) { |
407 | if (ofproto_classes[i] == class) { | |
408 | for (i++; i < n_ofproto_classes; i++) { | |
409 | ofproto_classes[i - 1] = ofproto_classes[i]; | |
410 | } | |
411 | n_ofproto_classes--; | |
412 | return 0; | |
413 | } | |
414 | } | |
415 | VLOG_WARN("attempted to unregister an ofproto class that is not " | |
416 | "registered"); | |
417 | return EAFNOSUPPORT; | |
418 | } | |
4a4cdb3b | 419 | |
f79e673f BP |
420 | /* Clears 'types' and enumerates all registered ofproto types into it. The |
421 | * caller must first initialize the sset. */ | |
422 | void | |
423 | ofproto_enumerate_types(struct sset *types) | |
424 | { | |
abe529af | 425 | size_t i; |
064af421 | 426 | |
c799c306 | 427 | sset_clear(types); |
abe529af BP |
428 | for (i = 0; i < n_ofproto_classes; i++) { |
429 | ofproto_classes[i]->enumerate_types(types); | |
430 | } | |
f79e673f | 431 | } |
064af421 | 432 | |
f79e673f BP |
433 | /* Returns the fully spelled out name for the given ofproto 'type'. |
434 | * | |
435 | * Normalized type string can be compared with strcmp(). Unnormalized type | |
436 | * string might be the same even if they have different spellings. */ | |
437 | const char * | |
438 | ofproto_normalize_type(const char *type) | |
439 | { | |
abe529af | 440 | return type && type[0] ? type : "system"; |
f79e673f | 441 | } |
064af421 | 442 | |
f79e673f BP |
443 | /* Clears 'names' and enumerates the names of all known created ofprotos with |
444 | * the given 'type'. The caller must first initialize the sset. Returns 0 if | |
445 | * successful, otherwise a positive errno value. | |
446 | * | |
447 | * Some kinds of datapaths might not be practically enumerable. This is not | |
448 | * considered an error. */ | |
449 | int | |
450 | ofproto_enumerate_names(const char *type, struct sset *names) | |
451 | { | |
abe529af BP |
452 | const struct ofproto_class *class = ofproto_class_find__(type); |
453 | return class ? class->enumerate_names(type, names) : EAFNOSUPPORT; | |
239ad7d1 | 454 | } |
7aa697dd | 455 | |
39c94593 JR |
456 | static void |
457 | ofproto_bump_tables_version(struct ofproto *ofproto) | |
458 | { | |
459 | ++ofproto->tables_version; | |
460 | ofproto->ofproto_class->set_tables_version(ofproto, | |
461 | ofproto->tables_version); | |
462 | } | |
463 | ||
064af421 | 464 | int |
abe529af | 465 | ofproto_create(const char *datapath_name, const char *datapath_type, |
064af421 | 466 | struct ofproto **ofprotop) |
25010c68 | 467 | OVS_EXCLUDED(ofproto_mutex) |
064af421 | 468 | { |
abe529af BP |
469 | const struct ofproto_class *class; |
470 | struct ofproto *ofproto; | |
064af421 | 471 | int error; |
c2f0373a | 472 | int i; |
064af421 BP |
473 | |
474 | *ofprotop = NULL; | |
475 | ||
abe529af BP |
476 | datapath_type = ofproto_normalize_type(datapath_type); |
477 | class = ofproto_class_find__(datapath_type); | |
478 | if (!class) { | |
479 | VLOG_WARN("could not create datapath %s of unknown type %s", | |
480 | datapath_name, datapath_type); | |
481 | return EAFNOSUPPORT; | |
064af421 BP |
482 | } |
483 | ||
abe529af BP |
484 | ofproto = class->alloc(); |
485 | if (!ofproto) { | |
486 | VLOG_ERR("failed to allocate datapath %s of type %s", | |
487 | datapath_name, datapath_type); | |
488 | return ENOMEM; | |
489 | } | |
490 | ||
491 | /* Initialize. */ | |
abe7b10f | 492 | ovs_mutex_lock(&ofproto_mutex); |
abe529af BP |
493 | memset(ofproto, 0, sizeof *ofproto); |
494 | ofproto->ofproto_class = class; | |
495 | ofproto->name = xstrdup(datapath_name); | |
496 | ofproto->type = xstrdup(datapath_type); | |
497 | hmap_insert(&all_ofprotos, &ofproto->hmap_node, | |
498 | hash_string(ofproto->name, 0)); | |
499 | ofproto->datapath_id = 0; | |
8402c74b | 500 | ofproto->forward_bpdu = false; |
abe529af | 501 | ofproto->fallback_dpid = pick_fallback_dpid(); |
061bfea4 BP |
502 | ofproto->mfr_desc = NULL; |
503 | ofproto->hw_desc = NULL; | |
504 | ofproto->sw_desc = NULL; | |
505 | ofproto->serial_desc = NULL; | |
506 | ofproto->dp_desc = NULL; | |
ad99e2ed | 507 | ofproto->frag_handling = OFPUTIL_FRAG_NORMAL; |
abe529af | 508 | hmap_init(&ofproto->ports); |
fdcea803 | 509 | hmap_init(&ofproto->ofport_usage); |
abe529af | 510 | shash_init(&ofproto->port_by_name); |
e1b1d06a | 511 | simap_init(&ofproto->ofp_requests); |
430dbb14 | 512 | ofproto->max_ports = ofp_to_u16(OFPP_MAX); |
448c2fa8 | 513 | ofproto->eviction_group_timer = LLONG_MIN; |
6c1491fb BP |
514 | ofproto->tables = NULL; |
515 | ofproto->n_tables = 0; | |
44e0c35d | 516 | ofproto->tables_version = OVS_VERSION_MIN; |
98eaac36 | 517 | hindex_init(&ofproto->cookies); |
35f48b8b | 518 | hmap_init(&ofproto->learned_cookies); |
417e7e66 | 519 | ovs_list_init(&ofproto->expirable); |
abe529af | 520 | ofproto->connmgr = connmgr_create(ofproto, datapath_name, datapath_name); |
7478b5a2 | 521 | ofproto->min_mtu = INT_MAX; |
db88b35c | 522 | cmap_init(&ofproto->groups); |
abe7b10f | 523 | ovs_mutex_unlock(&ofproto_mutex); |
0ae01c64 | 524 | ofproto->ogf.types = 0xf; |
7cb279c2 SH |
525 | ofproto->ogf.capabilities = OFPGFC_CHAINING | OFPGFC_SELECT_LIVENESS | |
526 | OFPGFC_SELECT_WEIGHT; | |
08d1e234 BP |
527 | for (i = 0; i < 4; i++) { |
528 | ofproto->ogf.max_groups[i] = OFPG_MAX; | |
529 | ofproto->ogf.ofpacts[i] = (UINT64_C(1) << N_OFPACTS) - 1; | |
530 | } | |
8d8ab6c2 | 531 | ovsrcu_set(&ofproto->metadata_tab, tun_metadata_alloc(NULL)); |
abe529af | 532 | |
04f48a68 YHW |
533 | ovs_mutex_init(&ofproto->vl_mff_map.mutex); |
534 | cmap_init(&ofproto->vl_mff_map.cmap); | |
535 | ||
0f5f95a9 | 536 | error = ofproto->ofproto_class->construct(ofproto); |
19a87e36 | 537 | if (error) { |
abe529af | 538 | VLOG_ERR("failed to open datapath %s: %s", |
10a89ef0 | 539 | datapath_name, ovs_strerror(error)); |
25010c68 | 540 | ovs_mutex_lock(&ofproto_mutex); |
58f19539 | 541 | connmgr_destroy(ofproto->connmgr); |
25010c68 JR |
542 | ofproto->connmgr = NULL; |
543 | ovs_mutex_unlock(&ofproto_mutex); | |
abe529af | 544 | ofproto_destroy__(ofproto); |
19a87e36 BP |
545 | return error; |
546 | } | |
073e2a6f | 547 | |
c2f0373a | 548 | /* Check that hidden tables, if any, are at the end. */ |
cb22974d | 549 | ovs_assert(ofproto->n_tables); |
c2f0373a BP |
550 | for (i = 0; i + 1 < ofproto->n_tables; i++) { |
551 | enum oftable_flags flags = ofproto->tables[i].flags; | |
552 | enum oftable_flags next_flags = ofproto->tables[i + 1].flags; | |
553 | ||
cb22974d | 554 | ovs_assert(!(flags & OFTABLE_HIDDEN) || next_flags & OFTABLE_HIDDEN); |
c2f0373a | 555 | } |
19a87e36 | 556 | |
abe529af | 557 | ofproto->datapath_id = pick_datapath_id(ofproto); |
abe529af | 558 | init_ports(ofproto); |
7aa697dd | 559 | |
9cae45dc JR |
560 | /* Initialize meters table. */ |
561 | if (ofproto->ofproto_class->meter_get_features) { | |
562 | ofproto->ofproto_class->meter_get_features(ofproto, | |
563 | &ofproto->meter_features); | |
564 | } else { | |
565 | memset(&ofproto->meter_features, 0, sizeof ofproto->meter_features); | |
566 | } | |
567 | ofproto->meters = xzalloc((ofproto->meter_features.max_meters + 1) | |
568 | * sizeof(struct meter *)); | |
569 | ||
621b8064 | 570 | /* Set the initial tables version. */ |
39c94593 | 571 | ofproto_bump_tables_version(ofproto); |
621b8064 | 572 | |
abe529af | 573 | *ofprotop = ofproto; |
064af421 BP |
574 | return 0; |
575 | } | |
576 | ||
91858960 BP |
577 | /* Must be called (only) by an ofproto implementation in its constructor |
578 | * function. See the large comment on 'construct' in struct ofproto_class for | |
579 | * details. */ | |
0f5f95a9 BP |
580 | void |
581 | ofproto_init_tables(struct ofproto *ofproto, int n_tables) | |
582 | { | |
583 | struct oftable *table; | |
584 | ||
cb22974d BP |
585 | ovs_assert(!ofproto->n_tables); |
586 | ovs_assert(n_tables >= 1 && n_tables <= 255); | |
0f5f95a9 BP |
587 | |
588 | ofproto->n_tables = n_tables; | |
589 | ofproto->tables = xmalloc(n_tables * sizeof *ofproto->tables); | |
590 | OFPROTO_FOR_EACH_TABLE (table, ofproto) { | |
591 | oftable_init(table); | |
592 | } | |
593 | } | |
594 | ||
91858960 BP |
595 | /* To be optionally called (only) by an ofproto implementation in its |
596 | * constructor function. See the large comment on 'construct' in struct | |
597 | * ofproto_class for details. | |
598 | * | |
599 | * Sets the maximum number of ports to 'max_ports'. The ofproto generic layer | |
600 | * will then ensure that actions passed into the ofproto implementation will | |
601 | * not refer to OpenFlow ports numbered 'max_ports' or higher. If this | |
602 | * function is not called, there will be no such restriction. | |
603 | * | |
604 | * Reserved ports numbered OFPP_MAX and higher are special and not subject to | |
605 | * the 'max_ports' restriction. */ | |
606 | void | |
430dbb14 | 607 | ofproto_init_max_ports(struct ofproto *ofproto, uint16_t max_ports) |
91858960 | 608 | { |
430dbb14 | 609 | ovs_assert(max_ports <= ofp_to_u16(OFPP_MAX)); |
91858960 BP |
610 | ofproto->max_ports = max_ports; |
611 | } | |
612 | ||
e825ace2 BP |
613 | uint64_t |
614 | ofproto_get_datapath_id(const struct ofproto *ofproto) | |
615 | { | |
616 | return ofproto->datapath_id; | |
617 | } | |
618 | ||
064af421 BP |
619 | void |
620 | ofproto_set_datapath_id(struct ofproto *p, uint64_t datapath_id) | |
621 | { | |
622 | uint64_t old_dpid = p->datapath_id; | |
fa60c019 | 623 | p->datapath_id = datapath_id ? datapath_id : pick_datapath_id(p); |
064af421 | 624 | if (p->datapath_id != old_dpid) { |
76ce9432 BP |
625 | /* Force all active connections to reconnect, since there is no way to |
626 | * notify a controller that the datapath ID has changed. */ | |
fa05809b | 627 | ofproto_reconnect_controllers(p); |
064af421 BP |
628 | } |
629 | } | |
630 | ||
76ce9432 BP |
631 | void |
632 | ofproto_set_controllers(struct ofproto *p, | |
633 | const struct ofproto_controller *controllers, | |
1d9ffc17 | 634 | size_t n_controllers, uint32_t allowed_versions) |
76ce9432 | 635 | { |
1d9ffc17 SH |
636 | connmgr_set_controllers(p->connmgr, controllers, n_controllers, |
637 | allowed_versions); | |
064af421 BP |
638 | } |
639 | ||
31681a5d JP |
640 | void |
641 | ofproto_set_fail_mode(struct ofproto *p, enum ofproto_fail_mode fail_mode) | |
642 | { | |
19a87e36 | 643 | connmgr_set_fail_mode(p->connmgr, fail_mode); |
31681a5d JP |
644 | } |
645 | ||
fa05809b BP |
646 | /* Drops the connections between 'ofproto' and all of its controllers, forcing |
647 | * them to reconnect. */ | |
648 | void | |
649 | ofproto_reconnect_controllers(struct ofproto *ofproto) | |
650 | { | |
19a87e36 | 651 | connmgr_reconnect(ofproto->connmgr); |
917e50e1 BP |
652 | } |
653 | ||
654 | /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'ofproto''s | |
655 | * in-band control should guarantee access, in the same way that in-band | |
656 | * control guarantees access to OpenFlow controllers. */ | |
657 | void | |
658 | ofproto_set_extra_in_band_remotes(struct ofproto *ofproto, | |
659 | const struct sockaddr_in *extras, size_t n) | |
660 | { | |
19a87e36 | 661 | connmgr_set_extra_in_band_remotes(ofproto->connmgr, extras, n); |
917e50e1 BP |
662 | } |
663 | ||
b1da6250 BP |
664 | /* Sets the OpenFlow queue used by flows set up by in-band control on |
665 | * 'ofproto' to 'queue_id'. If 'queue_id' is negative, then in-band control | |
666 | * flows will use the default queue. */ | |
667 | void | |
668 | ofproto_set_in_band_queue(struct ofproto *ofproto, int queue_id) | |
669 | { | |
19a87e36 | 670 | connmgr_set_in_band_queue(ofproto->connmgr, queue_id); |
b1da6250 BP |
671 | } |
672 | ||
084f5290 SH |
673 | /* Sets the number of flows at which eviction from the kernel flow table |
674 | * will occur. */ | |
675 | void | |
e79a6c83 | 676 | ofproto_set_flow_limit(unsigned limit) |
084f5290 | 677 | { |
e79a6c83 | 678 | ofproto_flow_limit = limit; |
084f5290 SH |
679 | } |
680 | ||
72310b04 JS |
681 | /* Sets the maximum idle time for flows in the datapath before they are |
682 | * expired. */ | |
683 | void | |
684 | ofproto_set_max_idle(unsigned max_idle) | |
685 | { | |
686 | ofproto_max_idle = max_idle; | |
687 | } | |
688 | ||
b53055f4 | 689 | /* If forward_bpdu is true, the NORMAL action will forward frames with |
8402c74b SS |
690 | * reserved (e.g. STP) destination Ethernet addresses. if forward_bpdu is false, |
691 | * the NORMAL action will drop these frames. */ | |
692 | void | |
693 | ofproto_set_forward_bpdu(struct ofproto *ofproto, bool forward_bpdu) | |
694 | { | |
695 | bool old_val = ofproto->forward_bpdu; | |
696 | ofproto->forward_bpdu = forward_bpdu; | |
697 | if (old_val != ofproto->forward_bpdu) { | |
698 | if (ofproto->ofproto_class->forward_bpdu_changed) { | |
699 | ofproto->ofproto_class->forward_bpdu_changed(ofproto); | |
700 | } | |
b53055f4 | 701 | } |
8402c74b SS |
702 | } |
703 | ||
e764773c | 704 | /* Sets the MAC aging timeout for the OFPP_NORMAL action on 'ofproto' to |
c4069512 BP |
705 | * 'idle_time', in seconds, and the maximum number of MAC table entries to |
706 | * 'max_entries'. */ | |
e764773c | 707 | void |
c4069512 BP |
708 | ofproto_set_mac_table_config(struct ofproto *ofproto, unsigned idle_time, |
709 | size_t max_entries) | |
e764773c | 710 | { |
c4069512 BP |
711 | if (ofproto->ofproto_class->set_mac_table_config) { |
712 | ofproto->ofproto_class->set_mac_table_config(ofproto, idle_time, | |
713 | max_entries); | |
e764773c BP |
714 | } |
715 | } | |
716 | ||
7c38d0a5 FL |
717 | /* Multicast snooping configuration. */ |
718 | ||
719 | /* Configures multicast snooping on 'ofproto' using the settings | |
720 | * defined in 's'. If 's' is NULL, disables multicast snooping. | |
721 | * | |
722 | * Returns 0 if successful, otherwise a positive errno value. */ | |
723 | int | |
724 | ofproto_set_mcast_snooping(struct ofproto *ofproto, | |
725 | const struct ofproto_mcast_snooping_settings *s) | |
726 | { | |
727 | return (ofproto->ofproto_class->set_mcast_snooping | |
728 | ? ofproto->ofproto_class->set_mcast_snooping(ofproto, s) | |
729 | : EOPNOTSUPP); | |
730 | } | |
731 | ||
8e04a33f | 732 | /* Configures multicast snooping flood settings on 'ofp_port' of 'ofproto'. |
7c38d0a5 FL |
733 | * |
734 | * Returns 0 if successful, otherwise a positive errno value.*/ | |
735 | int | |
8e04a33f FL |
736 | ofproto_port_set_mcast_snooping(struct ofproto *ofproto, void *aux, |
737 | const struct ofproto_mcast_snooping_port_settings *s) | |
7c38d0a5 FL |
738 | { |
739 | return (ofproto->ofproto_class->set_mcast_snooping_port | |
8e04a33f | 740 | ? ofproto->ofproto_class->set_mcast_snooping_port(ofproto, aux, s) |
7c38d0a5 FL |
741 | : EOPNOTSUPP); |
742 | } | |
743 | ||
f2eee189 | 744 | void |
d4f6865c | 745 | ofproto_type_set_config(const char *datapath_type, const struct smap *cfg) |
f2eee189 | 746 | { |
d4f6865c DDP |
747 | const struct ofproto_class *class; |
748 | ||
749 | datapath_type = ofproto_normalize_type(datapath_type); | |
750 | class = ofproto_class_find__(datapath_type); | |
751 | ||
752 | if (class->type_set_config) { | |
753 | class->type_set_config(datapath_type, cfg); | |
754 | } | |
f2eee189 AW |
755 | } |
756 | ||
448a4b2f | 757 | void |
30ea0da7 | 758 | ofproto_set_threads(int n_handlers_, int n_revalidators_) |
448a4b2f | 759 | { |
e79a6c83 EJ |
760 | int threads = MAX(count_cpu_cores(), 2); |
761 | ||
30ea0da7 GS |
762 | n_revalidators = MAX(n_revalidators_, 0); |
763 | n_handlers = MAX(n_handlers_, 0); | |
e79a6c83 EJ |
764 | |
765 | if (!n_revalidators) { | |
766 | n_revalidators = n_handlers | |
767 | ? MAX(threads - (int) n_handlers, 1) | |
768 | : threads / 4 + 1; | |
769 | } | |
770 | ||
771 | if (!n_handlers) { | |
772 | n_handlers = MAX(threads - (int) n_revalidators, 1); | |
773 | } | |
448a4b2f AW |
774 | } |
775 | ||
8b6ff729 BP |
776 | void |
777 | ofproto_set_dp_desc(struct ofproto *p, const char *dp_desc) | |
778 | { | |
779 | free(p->dp_desc); | |
2225c0b9 | 780 | p->dp_desc = nullable_xstrdup(dp_desc); |
8b6ff729 BP |
781 | } |
782 | ||
064af421 | 783 | int |
81e2083f | 784 | ofproto_set_snoops(struct ofproto *ofproto, const struct sset *snoops) |
064af421 | 785 | { |
19a87e36 | 786 | return connmgr_set_snoops(ofproto->connmgr, snoops); |
064af421 BP |
787 | } |
788 | ||
789 | int | |
0193b2af JG |
790 | ofproto_set_netflow(struct ofproto *ofproto, |
791 | const struct netflow_options *nf_options) | |
064af421 | 792 | { |
abe529af BP |
793 | if (nf_options && sset_is_empty(&nf_options->collectors)) { |
794 | nf_options = NULL; | |
795 | } | |
796 | ||
797 | if (ofproto->ofproto_class->set_netflow) { | |
798 | return ofproto->ofproto_class->set_netflow(ofproto, nf_options); | |
064af421 | 799 | } else { |
abe529af | 800 | return nf_options ? EOPNOTSUPP : 0; |
064af421 BP |
801 | } |
802 | } | |
803 | ||
abe529af | 804 | int |
72b06300 BP |
805 | ofproto_set_sflow(struct ofproto *ofproto, |
806 | const struct ofproto_sflow_options *oso) | |
807 | { | |
abe529af BP |
808 | if (oso && sset_is_empty(&oso->targets)) { |
809 | oso = NULL; | |
810 | } | |
72b06300 | 811 | |
abe529af BP |
812 | if (ofproto->ofproto_class->set_sflow) { |
813 | return ofproto->ofproto_class->set_sflow(ofproto, oso); | |
72b06300 | 814 | } else { |
abe529af | 815 | return oso ? EOPNOTSUPP : 0; |
72b06300 BP |
816 | } |
817 | } | |
29089a54 RL |
818 | |
819 | int | |
820 | ofproto_set_ipfix(struct ofproto *ofproto, | |
821 | const struct ofproto_ipfix_bridge_exporter_options *bo, | |
822 | const struct ofproto_ipfix_flow_exporter_options *fo, | |
823 | size_t n_fo) | |
824 | { | |
825 | if (ofproto->ofproto_class->set_ipfix) { | |
826 | return ofproto->ofproto_class->set_ipfix(ofproto, bo, fo, n_fo); | |
827 | } else { | |
828 | return (bo || fo) ? EOPNOTSUPP : 0; | |
829 | } | |
830 | } | |
40358701 | 831 | |
fb8f22c1 BY |
832 | static int |
833 | ofproto_get_ipfix_stats(struct ofproto *ofproto, | |
834 | bool bridge_ipfix, | |
835 | struct ovs_list *replies) | |
836 | { | |
837 | int error; | |
838 | ||
839 | if (ofproto->ofproto_class->get_ipfix_stats) { | |
840 | error = ofproto->ofproto_class->get_ipfix_stats(ofproto, | |
841 | bridge_ipfix, | |
842 | replies); | |
843 | } else { | |
844 | error = EOPNOTSUPP; | |
845 | } | |
846 | ||
847 | return error; | |
848 | } | |
849 | ||
850 | static enum ofperr | |
851 | handle_ipfix_bridge_stats_request(struct ofconn *ofconn, | |
852 | const struct ofp_header *request) | |
853 | { | |
854 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
855 | struct ovs_list replies; | |
856 | enum ofperr error; | |
857 | ||
858 | ofpmp_init(&replies, request); | |
859 | error = ofproto_get_ipfix_stats(ofproto, true, &replies); | |
860 | ||
861 | if (!error) { | |
862 | ofconn_send_replies(ofconn, &replies); | |
863 | } else { | |
864 | ofpbuf_list_delete(&replies); | |
865 | } | |
866 | ||
867 | return error; | |
868 | } | |
869 | ||
870 | static enum ofperr | |
871 | handle_ipfix_flow_stats_request(struct ofconn *ofconn, | |
872 | const struct ofp_header *request) | |
873 | { | |
874 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
875 | struct ovs_list replies; | |
876 | enum ofperr error; | |
877 | ||
878 | ofpmp_init(&replies, request); | |
879 | error = ofproto_get_ipfix_stats(ofproto, false, &replies); | |
880 | ||
881 | if (!error) { | |
882 | ofconn_send_replies(ofconn, &replies); | |
883 | } else { | |
884 | ofpbuf_list_delete(&replies); | |
885 | } | |
886 | ||
887 | return error; | |
888 | } | |
889 | ||
2a7c4805 JP |
890 | static enum ofperr |
891 | handle_nxt_ct_flush_zone(struct ofconn *ofconn, const struct ofp_header *oh) | |
892 | { | |
893 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
894 | const struct nx_zone_id *nzi = ofpmsg_body(oh); | |
895 | ||
896 | if (!is_all_zeros(nzi->zero, sizeof nzi->zero)) { | |
897 | return OFPERR_NXBRC_MUST_BE_ZERO; | |
898 | } | |
899 | ||
900 | uint16_t zone = ntohs(nzi->zone_id); | |
901 | if (ofproto->ofproto_class->ct_flush) { | |
902 | ofproto->ofproto_class->ct_flush(ofproto, &zone); | |
903 | } else { | |
904 | return EOPNOTSUPP; | |
905 | } | |
906 | ||
907 | return 0; | |
908 | } | |
909 | ||
40358701 GS |
910 | void |
911 | ofproto_set_flow_restore_wait(bool flow_restore_wait_db) | |
912 | { | |
913 | flow_restore_wait = flow_restore_wait_db; | |
914 | } | |
915 | ||
916 | bool | |
917 | ofproto_get_flow_restore_wait(void) | |
918 | { | |
919 | return flow_restore_wait; | |
920 | } | |
921 | ||
e7934396 | 922 | \f |
21f7563c JP |
923 | /* Spanning Tree Protocol (STP) configuration. */ |
924 | ||
925 | /* Configures STP on 'ofproto' using the settings defined in 's'. If | |
926 | * 's' is NULL, disables STP. | |
927 | * | |
928 | * Returns 0 if successful, otherwise a positive errno value. */ | |
929 | int | |
930 | ofproto_set_stp(struct ofproto *ofproto, | |
931 | const struct ofproto_stp_settings *s) | |
932 | { | |
933 | return (ofproto->ofproto_class->set_stp | |
934 | ? ofproto->ofproto_class->set_stp(ofproto, s) | |
935 | : EOPNOTSUPP); | |
936 | } | |
937 | ||
938 | /* Retrieves STP status of 'ofproto' and stores it in 's'. If the | |
939 | * 'enabled' member of 's' is false, then the other members are not | |
940 | * meaningful. | |
941 | * | |
942 | * Returns 0 if successful, otherwise a positive errno value. */ | |
943 | int | |
944 | ofproto_get_stp_status(struct ofproto *ofproto, | |
945 | struct ofproto_stp_status *s) | |
946 | { | |
947 | return (ofproto->ofproto_class->get_stp_status | |
948 | ? ofproto->ofproto_class->get_stp_status(ofproto, s) | |
949 | : EOPNOTSUPP); | |
950 | } | |
951 | ||
952 | /* Configures STP on 'ofp_port' of 'ofproto' using the settings defined | |
953 | * in 's'. The caller is responsible for assigning STP port numbers | |
954 | * (using the 'port_num' member in the range of 1 through 255, inclusive) | |
955 | * and ensuring there are no duplicates. If the 's' is NULL, then STP | |
956 | * is disabled on the port. | |
957 | * | |
958 | * Returns 0 if successful, otherwise a positive errno value.*/ | |
959 | int | |
4e022ec0 | 960 | ofproto_port_set_stp(struct ofproto *ofproto, ofp_port_t ofp_port, |
21f7563c JP |
961 | const struct ofproto_port_stp_settings *s) |
962 | { | |
963 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); | |
964 | if (!ofport) { | |
94783c7c | 965 | VLOG_WARN("%s: cannot configure STP on nonexistent port %"PRIu32, |
21f7563c JP |
966 | ofproto->name, ofp_port); |
967 | return ENODEV; | |
968 | } | |
969 | ||
970 | return (ofproto->ofproto_class->set_stp_port | |
971 | ? ofproto->ofproto_class->set_stp_port(ofport, s) | |
972 | : EOPNOTSUPP); | |
973 | } | |
974 | ||
975 | /* Retrieves STP port status of 'ofp_port' on 'ofproto' and stores it in | |
976 | * 's'. If the 'enabled' member in 's' is false, then the other members | |
977 | * are not meaningful. | |
978 | * | |
979 | * Returns 0 if successful, otherwise a positive errno value.*/ | |
980 | int | |
4e022ec0 | 981 | ofproto_port_get_stp_status(struct ofproto *ofproto, ofp_port_t ofp_port, |
21f7563c JP |
982 | struct ofproto_port_stp_status *s) |
983 | { | |
984 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); | |
985 | if (!ofport) { | |
b0a5c43b | 986 | VLOG_WARN_RL(&rl, "%s: cannot get STP status on nonexistent " |
94783c7c | 987 | "port %"PRIu32, ofproto->name, ofp_port); |
21f7563c JP |
988 | return ENODEV; |
989 | } | |
990 | ||
991 | return (ofproto->ofproto_class->get_stp_port_status | |
992 | ? ofproto->ofproto_class->get_stp_port_status(ofport, s) | |
993 | : EOPNOTSUPP); | |
994 | } | |
fd28ce3a JS |
995 | |
996 | /* Retrieves STP port statistics of 'ofp_port' on 'ofproto' and stores it in | |
997 | * 's'. If the 'enabled' member in 's' is false, then the other members | |
998 | * are not meaningful. | |
999 | * | |
1000 | * Returns 0 if successful, otherwise a positive errno value.*/ | |
1001 | int | |
1002 | ofproto_port_get_stp_stats(struct ofproto *ofproto, ofp_port_t ofp_port, | |
1003 | struct ofproto_port_stp_stats *s) | |
1004 | { | |
1005 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); | |
1006 | if (!ofport) { | |
1007 | VLOG_WARN_RL(&rl, "%s: cannot get STP stats on nonexistent " | |
94783c7c | 1008 | "port %"PRIu32, ofproto->name, ofp_port); |
fd28ce3a JS |
1009 | return ENODEV; |
1010 | } | |
1011 | ||
1012 | return (ofproto->ofproto_class->get_stp_port_stats | |
1013 | ? ofproto->ofproto_class->get_stp_port_stats(ofport, s) | |
1014 | : EOPNOTSUPP); | |
1015 | } | |
9efd308e DV |
1016 | |
1017 | /* Rapid Spanning Tree Protocol (RSTP) configuration. */ | |
1018 | ||
1019 | /* Configures RSTP on 'ofproto' using the settings defined in 's'. If | |
1020 | * 's' is NULL, disables RSTP. | |
1021 | * | |
1022 | * Returns 0 if successful, otherwise a positive errno value. */ | |
1023 | int | |
1024 | ofproto_set_rstp(struct ofproto *ofproto, | |
1025 | const struct ofproto_rstp_settings *s) | |
1026 | { | |
1027 | if (!ofproto->ofproto_class->set_rstp) { | |
1028 | return EOPNOTSUPP; | |
1029 | } | |
1030 | ofproto->ofproto_class->set_rstp(ofproto, s); | |
1031 | return 0; | |
1032 | } | |
1033 | ||
1034 | /* Retrieves RSTP status of 'ofproto' and stores it in 's'. If the | |
1035 | * 'enabled' member of 's' is false, then the other members are not | |
1036 | * meaningful. | |
1037 | * | |
1038 | * Returns 0 if successful, otherwise a positive errno value. */ | |
1039 | int | |
1040 | ofproto_get_rstp_status(struct ofproto *ofproto, | |
1041 | struct ofproto_rstp_status *s) | |
1042 | { | |
1043 | if (!ofproto->ofproto_class->get_rstp_status) { | |
1044 | return EOPNOTSUPP; | |
1045 | } | |
1046 | ofproto->ofproto_class->get_rstp_status(ofproto, s); | |
1047 | return 0; | |
1048 | } | |
1049 | ||
1050 | /* Configures RSTP on 'ofp_port' of 'ofproto' using the settings defined | |
1051 | * in 's'. The caller is responsible for assigning RSTP port numbers | |
1052 | * (using the 'port_num' member in the range of 1 through 255, inclusive) | |
1053 | * and ensuring there are no duplicates. If the 's' is NULL, then RSTP | |
1054 | * is disabled on the port. | |
1055 | * | |
1056 | * Returns 0 if successful, otherwise a positive errno value.*/ | |
1057 | int | |
1058 | ofproto_port_set_rstp(struct ofproto *ofproto, ofp_port_t ofp_port, | |
1059 | const struct ofproto_port_rstp_settings *s) | |
1060 | { | |
1061 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); | |
1062 | if (!ofport) { | |
94783c7c | 1063 | VLOG_WARN("%s: cannot configure RSTP on nonexistent port %"PRIu32, |
9efd308e DV |
1064 | ofproto->name, ofp_port); |
1065 | return ENODEV; | |
1066 | } | |
1067 | ||
1068 | if (!ofproto->ofproto_class->set_rstp_port) { | |
1069 | return EOPNOTSUPP; | |
1070 | } | |
1071 | ofproto->ofproto_class->set_rstp_port(ofport, s); | |
1072 | return 0; | |
1073 | } | |
1074 | ||
1075 | /* Retrieves RSTP port status of 'ofp_port' on 'ofproto' and stores it in | |
1076 | * 's'. If the 'enabled' member in 's' is false, then the other members | |
1077 | * are not meaningful. | |
1078 | * | |
1079 | * Returns 0 if successful, otherwise a positive errno value.*/ | |
1080 | int | |
1081 | ofproto_port_get_rstp_status(struct ofproto *ofproto, ofp_port_t ofp_port, | |
1082 | struct ofproto_port_rstp_status *s) | |
1083 | { | |
1084 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); | |
1085 | if (!ofport) { | |
1086 | VLOG_WARN_RL(&rl, "%s: cannot get RSTP status on nonexistent " | |
94783c7c | 1087 | "port %"PRIu32, ofproto->name, ofp_port); |
9efd308e DV |
1088 | return ENODEV; |
1089 | } | |
1090 | ||
1091 | if (!ofproto->ofproto_class->get_rstp_port_status) { | |
1092 | return EOPNOTSUPP; | |
1093 | } | |
1094 | ofproto->ofproto_class->get_rstp_port_status(ofport, s); | |
1095 | return 0; | |
1096 | } | |
21f7563c | 1097 | \f |
8b36f51e EJ |
1098 | /* Queue DSCP configuration. */ |
1099 | ||
1100 | /* Registers meta-data associated with the 'n_qdscp' Qualities of Service | |
1101 | * 'queues' attached to 'ofport'. This data is not intended to be sufficient | |
1102 | * to implement QoS. Instead, it is used to implement features which require | |
1103 | * knowledge of what queues exist on a port, and some basic information about | |
1104 | * them. | |
1105 | * | |
1106 | * Returns 0 if successful, otherwise a positive errno value. */ | |
1107 | int | |
4e022ec0 | 1108 | ofproto_port_set_queues(struct ofproto *ofproto, ofp_port_t ofp_port, |
8b36f51e EJ |
1109 | const struct ofproto_port_queue *queues, |
1110 | size_t n_queues) | |
1111 | { | |
1112 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); | |
1113 | ||
1114 | if (!ofport) { | |
94783c7c | 1115 | VLOG_WARN("%s: cannot set queues on nonexistent port %"PRIu32, |
8b36f51e EJ |
1116 | ofproto->name, ofp_port); |
1117 | return ENODEV; | |
1118 | } | |
1119 | ||
1120 | return (ofproto->ofproto_class->set_queues | |
1121 | ? ofproto->ofproto_class->set_queues(ofport, queues, n_queues) | |
1122 | : EOPNOTSUPP); | |
1123 | } | |
1124 | \f | |
0477baa9 DF |
1125 | /* LLDP configuration. */ |
1126 | void | |
1127 | ofproto_port_set_lldp(struct ofproto *ofproto, | |
1128 | ofp_port_t ofp_port, | |
1129 | const struct smap *cfg) | |
1130 | { | |
1131 | struct ofport *ofport; | |
1132 | int error; | |
1133 | ||
1134 | ofport = ofproto_get_port(ofproto, ofp_port); | |
1135 | if (!ofport) { | |
94783c7c | 1136 | VLOG_WARN("%s: cannot configure LLDP on nonexistent port %"PRIu32, |
0477baa9 DF |
1137 | ofproto->name, ofp_port); |
1138 | return; | |
1139 | } | |
1140 | error = (ofproto->ofproto_class->set_lldp | |
1141 | ? ofproto->ofproto_class->set_lldp(ofport, cfg) | |
1142 | : EOPNOTSUPP); | |
1143 | if (error) { | |
94783c7c | 1144 | VLOG_WARN("%s: lldp configuration on port %"PRIu32" (%s) failed (%s)", |
0477baa9 DF |
1145 | ofproto->name, ofp_port, netdev_get_name(ofport->netdev), |
1146 | ovs_strerror(error)); | |
1147 | } | |
1148 | } | |
1149 | ||
1150 | int | |
1151 | ofproto_set_aa(struct ofproto *ofproto, void *aux OVS_UNUSED, | |
1152 | const struct aa_settings *s) | |
1153 | { | |
1154 | if (!ofproto->ofproto_class->set_aa) { | |
1155 | return EOPNOTSUPP; | |
1156 | } | |
1157 | ofproto->ofproto_class->set_aa(ofproto, s); | |
1158 | return 0; | |
1159 | } | |
1160 | ||
1161 | int | |
1162 | ofproto_aa_mapping_register(struct ofproto *ofproto, void *aux, | |
1163 | const struct aa_mapping_settings *s) | |
1164 | { | |
1165 | if (!ofproto->ofproto_class->aa_mapping_set) { | |
1166 | return EOPNOTSUPP; | |
1167 | } | |
1168 | ofproto->ofproto_class->aa_mapping_set(ofproto, aux, s); | |
1169 | return 0; | |
1170 | } | |
1171 | ||
1172 | int | |
1173 | ofproto_aa_mapping_unregister(struct ofproto *ofproto, void *aux) | |
1174 | { | |
1175 | if (!ofproto->ofproto_class->aa_mapping_unset) { | |
1176 | return EOPNOTSUPP; | |
1177 | } | |
1178 | ofproto->ofproto_class->aa_mapping_unset(ofproto, aux); | |
1179 | return 0; | |
1180 | } | |
1181 | ||
1182 | int | |
1183 | ofproto_aa_vlan_get_queued(struct ofproto *ofproto, | |
1184 | struct ovs_list *list) | |
1185 | { | |
1186 | if (!ofproto->ofproto_class->aa_vlan_get_queued) { | |
1187 | return EOPNOTSUPP; | |
1188 | } | |
1189 | ofproto->ofproto_class->aa_vlan_get_queued(ofproto, list); | |
1190 | return 0; | |
1191 | } | |
1192 | ||
1193 | unsigned int | |
1194 | ofproto_aa_vlan_get_queue_size(struct ofproto *ofproto) | |
1195 | { | |
1196 | if (!ofproto->ofproto_class->aa_vlan_get_queue_size) { | |
1197 | return EOPNOTSUPP; | |
1198 | } | |
1199 | return ofproto->ofproto_class->aa_vlan_get_queue_size(ofproto); | |
1200 | } | |
1201 | ||
e7934396 BP |
1202 | /* Connectivity Fault Management configuration. */ |
1203 | ||
892815f5 | 1204 | /* Clears the CFM configuration from 'ofp_port' on 'ofproto'. */ |
e7934396 | 1205 | void |
4e022ec0 | 1206 | ofproto_port_clear_cfm(struct ofproto *ofproto, ofp_port_t ofp_port) |
e7934396 | 1207 | { |
abe529af BP |
1208 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); |
1209 | if (ofport && ofproto->ofproto_class->set_cfm) { | |
a5610457 | 1210 | ofproto->ofproto_class->set_cfm(ofport, NULL); |
e7934396 BP |
1211 | } |
1212 | } | |
72b06300 | 1213 | |
892815f5 | 1214 | /* Configures connectivity fault management on 'ofp_port' in 'ofproto'. Takes |
93b8df38 EJ |
1215 | * basic configuration from the configuration members in 'cfm', and the remote |
1216 | * maintenance point ID from remote_mpid. Ignores the statistics members of | |
1217 | * 'cfm'. | |
e7934396 | 1218 | * |
892815f5 | 1219 | * This function has no effect if 'ofproto' does not have a port 'ofp_port'. */ |
e7934396 | 1220 | void |
4e022ec0 | 1221 | ofproto_port_set_cfm(struct ofproto *ofproto, ofp_port_t ofp_port, |
a5610457 | 1222 | const struct cfm_settings *s) |
e7934396 BP |
1223 | { |
1224 | struct ofport *ofport; | |
abe529af | 1225 | int error; |
e7934396 | 1226 | |
abe529af | 1227 | ofport = ofproto_get_port(ofproto, ofp_port); |
e7934396 | 1228 | if (!ofport) { |
94783c7c | 1229 | VLOG_WARN("%s: cannot configure CFM on nonexistent port %"PRIu32, |
892815f5 | 1230 | ofproto->name, ofp_port); |
e7934396 BP |
1231 | return; |
1232 | } | |
1233 | ||
93b8df38 EJ |
1234 | /* XXX: For configuration simplicity, we only support one remote_mpid |
1235 | * outside of the CFM module. It's not clear if this is the correct long | |
1236 | * term solution or not. */ | |
abe529af | 1237 | error = (ofproto->ofproto_class->set_cfm |
a5610457 | 1238 | ? ofproto->ofproto_class->set_cfm(ofport, s) |
abe529af BP |
1239 | : EOPNOTSUPP); |
1240 | if (error) { | |
94783c7c | 1241 | VLOG_WARN("%s: CFM configuration on port %"PRIu32" (%s) failed (%s)", |
abe529af | 1242 | ofproto->name, ofp_port, netdev_get_name(ofport->netdev), |
10a89ef0 | 1243 | ovs_strerror(error)); |
e7934396 | 1244 | } |
e7934396 | 1245 | } |
e7934396 | 1246 | |
ccc09689 EJ |
1247 | /* Configures BFD on 'ofp_port' in 'ofproto'. This function has no effect if |
1248 | * 'ofproto' does not have a port 'ofp_port'. */ | |
1249 | void | |
4e022ec0 | 1250 | ofproto_port_set_bfd(struct ofproto *ofproto, ofp_port_t ofp_port, |
ccc09689 EJ |
1251 | const struct smap *cfg) |
1252 | { | |
1253 | struct ofport *ofport; | |
1254 | int error; | |
1255 | ||
1256 | ofport = ofproto_get_port(ofproto, ofp_port); | |
1257 | if (!ofport) { | |
94783c7c | 1258 | VLOG_WARN("%s: cannot configure bfd on nonexistent port %"PRIu32, |
ccc09689 | 1259 | ofproto->name, ofp_port); |
e8999bdc | 1260 | return; |
ccc09689 EJ |
1261 | } |
1262 | ||
1263 | error = (ofproto->ofproto_class->set_bfd | |
1264 | ? ofproto->ofproto_class->set_bfd(ofport, cfg) | |
1265 | : EOPNOTSUPP); | |
1266 | if (error) { | |
94783c7c | 1267 | VLOG_WARN("%s: bfd configuration on port %"PRIu32" (%s) failed (%s)", |
ccc09689 | 1268 | ofproto->name, ofp_port, netdev_get_name(ofport->netdev), |
10a89ef0 | 1269 | ovs_strerror(error)); |
ccc09689 EJ |
1270 | } |
1271 | } | |
1272 | ||
8f5514fe AW |
1273 | /* Checks the status change of BFD on 'ofport'. |
1274 | * | |
1275 | * Returns true if 'ofproto_class' does not support 'bfd_status_changed'. */ | |
1276 | bool | |
1277 | ofproto_port_bfd_status_changed(struct ofproto *ofproto, ofp_port_t ofp_port) | |
1278 | { | |
1279 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); | |
1280 | return (ofport && ofproto->ofproto_class->bfd_status_changed | |
1281 | ? ofproto->ofproto_class->bfd_status_changed(ofport) | |
1282 | : true); | |
1283 | } | |
1284 | ||
88bf179a | 1285 | /* Populates 'status' with the status of BFD on 'ofport'. Returns 0 on |
8f5514fe AW |
1286 | * success. Returns a positive errno otherwise. Has no effect if 'ofp_port' |
1287 | * is not an OpenFlow port in 'ofproto'. | |
88bf179a AW |
1288 | * |
1289 | * The caller must provide and own '*status'. */ | |
ccc09689 | 1290 | int |
4e022ec0 | 1291 | ofproto_port_get_bfd_status(struct ofproto *ofproto, ofp_port_t ofp_port, |
ccc09689 EJ |
1292 | struct smap *status) |
1293 | { | |
1294 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); | |
1295 | return (ofport && ofproto->ofproto_class->get_bfd_status | |
1296 | ? ofproto->ofproto_class->get_bfd_status(ofport, status) | |
1297 | : EOPNOTSUPP); | |
1298 | } | |
1299 | ||
fa066f01 BP |
1300 | /* Checks the status of LACP negotiation for 'ofp_port' within ofproto. |
1301 | * Returns 1 if LACP partner information for 'ofp_port' is up-to-date, | |
1302 | * 0 if LACP partner information is not current (generally indicating a | |
1303 | * connectivity problem), or -1 if LACP is not enabled on 'ofp_port'. */ | |
1304 | int | |
4e022ec0 | 1305 | ofproto_port_is_lacp_current(struct ofproto *ofproto, ofp_port_t ofp_port) |
fa066f01 | 1306 | { |
abe529af BP |
1307 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); |
1308 | return (ofport && ofproto->ofproto_class->port_is_lacp_current | |
1309 | ? ofproto->ofproto_class->port_is_lacp_current(ofport) | |
fa066f01 | 1310 | : -1); |
e7934396 | 1311 | } |
50b9699f NM |
1312 | |
1313 | int | |
1314 | ofproto_port_get_lacp_stats(const struct ofport *port, struct lacp_slave_stats *stats) | |
1315 | { | |
1316 | struct ofproto *ofproto = port->ofproto; | |
1317 | int error; | |
1318 | ||
1319 | if (ofproto->ofproto_class->port_get_lacp_stats) { | |
1320 | error = ofproto->ofproto_class->port_get_lacp_stats(port, stats); | |
1321 | } else { | |
1322 | error = EOPNOTSUPP; | |
1323 | } | |
1324 | ||
1325 | return error; | |
1326 | } | |
e7934396 | 1327 | \f |
fa066f01 | 1328 | /* Bundles. */ |
e7934396 | 1329 | |
abe529af BP |
1330 | /* Registers a "bundle" associated with client data pointer 'aux' in 'ofproto'. |
1331 | * A bundle is the same concept as a Port in OVSDB, that is, it consists of one | |
1332 | * or more "slave" devices (Interfaces, in OVSDB) along with a VLAN | |
1333 | * configuration plus, if there is more than one slave, a bonding | |
1334 | * configuration. | |
1335 | * | |
1336 | * If 'aux' is already registered then this function updates its configuration | |
1337 | * to 's'. Otherwise, this function registers a new bundle. | |
1338 | * | |
1339 | * Bundles only affect the NXAST_AUTOPATH action and output to the OFPP_NORMAL | |
1340 | * port. */ | |
1341 | int | |
1342 | ofproto_bundle_register(struct ofproto *ofproto, void *aux, | |
1343 | const struct ofproto_bundle_settings *s) | |
fa066f01 | 1344 | { |
abe529af BP |
1345 | return (ofproto->ofproto_class->bundle_set |
1346 | ? ofproto->ofproto_class->bundle_set(ofproto, aux, s) | |
1347 | : EOPNOTSUPP); | |
fa066f01 BP |
1348 | } |
1349 | ||
abe529af BP |
1350 | /* Unregisters the bundle registered on 'ofproto' with auxiliary data 'aux'. |
1351 | * If no such bundle has been registered, this has no effect. */ | |
1352 | int | |
1353 | ofproto_bundle_unregister(struct ofproto *ofproto, void *aux) | |
e7934396 | 1354 | { |
abe529af | 1355 | return ofproto_bundle_register(ofproto, aux, NULL); |
e7934396 | 1356 | } |
fa066f01 | 1357 | |
e7934396 | 1358 | \f |
abe529af BP |
1359 | /* Registers a mirror associated with client data pointer 'aux' in 'ofproto'. |
1360 | * If 'aux' is already registered then this function updates its configuration | |
c06bba01 | 1361 | * to 's'. Otherwise, this function registers a new mirror. */ |
abe529af BP |
1362 | int |
1363 | ofproto_mirror_register(struct ofproto *ofproto, void *aux, | |
1364 | const struct ofproto_mirror_settings *s) | |
064af421 | 1365 | { |
abe529af BP |
1366 | return (ofproto->ofproto_class->mirror_set |
1367 | ? ofproto->ofproto_class->mirror_set(ofproto, aux, s) | |
1368 | : EOPNOTSUPP); | |
064af421 BP |
1369 | } |
1370 | ||
abe529af BP |
1371 | /* Unregisters the mirror registered on 'ofproto' with auxiliary data 'aux'. |
1372 | * If no mirror has been registered, this has no effect. */ | |
1373 | int | |
1374 | ofproto_mirror_unregister(struct ofproto *ofproto, void *aux) | |
064af421 | 1375 | { |
abe529af | 1376 | return ofproto_mirror_register(ofproto, aux, NULL); |
064af421 BP |
1377 | } |
1378 | ||
9d24de3b JP |
1379 | /* Retrieves statistics from mirror associated with client data pointer |
1380 | * 'aux' in 'ofproto'. Stores packet and byte counts in 'packets' and | |
1381 | * 'bytes', respectively. If a particular counters is not supported, | |
0477baa9 DF |
1382 | * the appropriate argument is set to UINT64_MAX. |
1383 | */ | |
9d24de3b JP |
1384 | int |
1385 | ofproto_mirror_get_stats(struct ofproto *ofproto, void *aux, | |
1386 | uint64_t *packets, uint64_t *bytes) | |
1387 | { | |
1388 | if (!ofproto->ofproto_class->mirror_get_stats) { | |
1389 | *packets = *bytes = UINT64_MAX; | |
1390 | return EOPNOTSUPP; | |
1391 | } | |
1392 | ||
1393 | return ofproto->ofproto_class->mirror_get_stats(ofproto, aux, | |
1394 | packets, bytes); | |
1395 | } | |
1396 | ||
abe529af BP |
1397 | /* Configures the VLANs whose bits are set to 1 in 'flood_vlans' as VLANs on |
1398 | * which all packets are flooded, instead of using MAC learning. If | |
1399 | * 'flood_vlans' is NULL, then MAC learning applies to all VLANs. | |
1400 | * | |
1401 | * Flood VLANs affect only the treatment of packets output to the OFPP_NORMAL | |
1402 | * port. */ | |
1403 | int | |
1404 | ofproto_set_flood_vlans(struct ofproto *ofproto, unsigned long *flood_vlans) | |
abdfe474 | 1405 | { |
abe529af BP |
1406 | return (ofproto->ofproto_class->set_flood_vlans |
1407 | ? ofproto->ofproto_class->set_flood_vlans(ofproto, flood_vlans) | |
1408 | : EOPNOTSUPP); | |
abdfe474 JP |
1409 | } |
1410 | ||
abe529af BP |
1411 | /* Returns true if 'aux' is a registered bundle that is currently in use as the |
1412 | * output for a mirror. */ | |
1413 | bool | |
b4affc74 | 1414 | ofproto_is_mirror_output_bundle(const struct ofproto *ofproto, void *aux) |
abe529af BP |
1415 | { |
1416 | return (ofproto->ofproto_class->is_mirror_output_bundle | |
1417 | ? ofproto->ofproto_class->is_mirror_output_bundle(ofproto, aux) | |
1418 | : false); | |
1419 | } | |
1420 | \f | |
254750ce BP |
1421 | /* Configuration of OpenFlow tables. */ |
1422 | ||
1423 | /* Returns the number of OpenFlow tables in 'ofproto'. */ | |
1424 | int | |
1425 | ofproto_get_n_tables(const struct ofproto *ofproto) | |
1426 | { | |
1427 | return ofproto->n_tables; | |
1428 | } | |
1429 | ||
adcf00ba AZ |
1430 | /* Returns the number of Controller visible OpenFlow tables |
1431 | * in 'ofproto'. This number will exclude Hidden tables. | |
1432 | * This funtion's return value should be less or equal to that of | |
1433 | * ofproto_get_n_tables() . */ | |
1434 | uint8_t | |
1435 | ofproto_get_n_visible_tables(const struct ofproto *ofproto) | |
1436 | { | |
1437 | uint8_t n = ofproto->n_tables; | |
1438 | ||
1439 | /* Count only non-hidden tables in the number of tables. (Hidden tables, | |
1440 | * if present, are always at the end.) */ | |
1441 | while(n && (ofproto->tables[n - 1].flags & OFTABLE_HIDDEN)) { | |
1442 | n--; | |
1443 | } | |
1444 | ||
1445 | return n; | |
1446 | } | |
1447 | ||
254750ce BP |
1448 | /* Configures the OpenFlow table in 'ofproto' with id 'table_id' with the |
1449 | * settings from 's'. 'table_id' must be in the range 0 through the number of | |
1450 | * OpenFlow tables in 'ofproto' minus 1, inclusive. | |
1451 | * | |
1452 | * For read-only tables, only the name may be configured. */ | |
1453 | void | |
1454 | ofproto_configure_table(struct ofproto *ofproto, int table_id, | |
1455 | const struct ofproto_table_settings *s) | |
1456 | { | |
1457 | struct oftable *table; | |
1458 | ||
cb22974d | 1459 | ovs_assert(table_id >= 0 && table_id < ofproto->n_tables); |
254750ce BP |
1460 | table = &ofproto->tables[table_id]; |
1461 | ||
1462 | oftable_set_name(table, s->name); | |
1463 | ||
1464 | if (table->flags & OFTABLE_READONLY) { | |
1465 | return; | |
1466 | } | |
1467 | ||
f358a2cb JR |
1468 | if (classifier_set_prefix_fields(&table->cls, |
1469 | s->prefix_fields, s->n_prefix_fields)) { | |
1470 | /* XXX: Trigger revalidation. */ | |
1471 | } | |
834fe5cb BP |
1472 | |
1473 | ovs_mutex_lock(&ofproto_mutex); | |
82c22d34 BP |
1474 | unsigned int new_eviction = (s->enable_eviction |
1475 | ? table->eviction | EVICTION_CLIENT | |
1476 | : table->eviction & ~EVICTION_CLIENT); | |
1477 | oftable_configure_eviction(table, new_eviction, s->groups, s->n_groups); | |
7394b8fb | 1478 | table->max_flows = s->max_flows; |
6787a49f | 1479 | evict_rules_from_table(table); |
834fe5cb | 1480 | ovs_mutex_unlock(&ofproto_mutex); |
254750ce BP |
1481 | } |
1482 | \f | |
81e2083f BP |
1483 | bool |
1484 | ofproto_has_snoops(const struct ofproto *ofproto) | |
1485 | { | |
1486 | return connmgr_has_snoops(ofproto->connmgr); | |
1487 | } | |
1488 | ||
064af421 | 1489 | void |
81e2083f | 1490 | ofproto_get_snoops(const struct ofproto *ofproto, struct sset *snoops) |
064af421 | 1491 | { |
19a87e36 | 1492 | connmgr_get_snoops(ofproto->connmgr, snoops); |
064af421 BP |
1493 | } |
1494 | ||
a73fcd71 | 1495 | /* Deletes 'rule' from 'ofproto'. |
8037acb4 | 1496 | * |
b74cb5d8 BP |
1497 | * Within an ofproto implementation, this function allows an ofproto |
1498 | * implementation to destroy any rules that remain when its ->destruct() | |
1499 | * function is called. This function is not suitable for use elsewhere in an | |
1500 | * ofproto implementation. | |
1501 | * | |
b74cb5d8 | 1502 | * This function implements steps 4.4 and 4.5 in the section titled "Rule Life |
8b81d1ef | 1503 | * Cycle" in ofproto-provider.h. */ |
b74cb5d8 | 1504 | void |
8b81d1ef | 1505 | ofproto_rule_delete(struct ofproto *ofproto, struct rule *rule) |
15aaf599 | 1506 | OVS_EXCLUDED(ofproto_mutex) |
7ee20df1 | 1507 | { |
834fe5cb BP |
1508 | /* This skips the ofmonitor and flow-removed notifications because the |
1509 | * switch is being deleted and any OpenFlow channels have been or soon will | |
1510 | * be killed. */ | |
15aaf599 | 1511 | ovs_mutex_lock(&ofproto_mutex); |
39c94593 | 1512 | |
30d0452c | 1513 | if (rule->state == RULE_INSERTED) { |
39c94593 | 1514 | /* Make sure there is no postponed removal of the rule. */ |
44e0c35d | 1515 | ovs_assert(cls_rule_visible_in_version(&rule->cr, OVS_VERSION_MAX)); |
39c94593 JR |
1516 | |
1517 | if (!classifier_remove(&rule->ofproto->tables[rule->table_id].cls, | |
1518 | &rule->cr)) { | |
1519 | OVS_NOT_REACHED(); | |
1520 | } | |
1521 | ofproto_rule_remove__(rule->ofproto, rule); | |
1fc71871 JR |
1522 | if (ofproto->ofproto_class->rule_delete) { |
1523 | ofproto->ofproto_class->rule_delete(rule); | |
1524 | } | |
25010c68 JR |
1525 | |
1526 | /* This may not be the last reference to the rule. */ | |
39c94593 JR |
1527 | ofproto_rule_unref(rule); |
1528 | } | |
15aaf599 | 1529 | ovs_mutex_unlock(&ofproto_mutex); |
8037acb4 BP |
1530 | } |
1531 | ||
1532 | static void | |
1533 | ofproto_flush__(struct ofproto *ofproto) | |
15aaf599 | 1534 | OVS_EXCLUDED(ofproto_mutex) |
8037acb4 | 1535 | { |
d0918789 | 1536 | struct oftable *table; |
7ee20df1 | 1537 | |
3948503a | 1538 | /* This will flush all datapath flows. */ |
7ee20df1 BP |
1539 | if (ofproto->ofproto_class->flush) { |
1540 | ofproto->ofproto_class->flush(ofproto); | |
1541 | } | |
1542 | ||
3948503a JR |
1543 | /* XXX: There is a small race window here, where new datapath flows can be |
1544 | * created by upcall handlers based on the existing flow table. We can not | |
1545 | * call ofproto class flush while holding 'ofproto_mutex' to prevent this, | |
1546 | * as then we could deadlock on syncing with the handler threads waiting on | |
1547 | * the same mutex. */ | |
1548 | ||
15aaf599 | 1549 | ovs_mutex_lock(&ofproto_mutex); |
b772ded8 | 1550 | OFPROTO_FOR_EACH_TABLE (table, ofproto) { |
802f84ff | 1551 | struct rule_collection rules; |
78c8df12 | 1552 | struct rule *rule; |
7ee20df1 | 1553 | |
5c67e4af BP |
1554 | if (table->flags & OFTABLE_HIDDEN) { |
1555 | continue; | |
1556 | } | |
1557 | ||
802f84ff JR |
1558 | rule_collection_init(&rules); |
1559 | ||
de4ad4a2 | 1560 | CLS_FOR_EACH (rule, cr, &table->cls) { |
802f84ff | 1561 | rule_collection_add(&rules, rule); |
7ee20df1 | 1562 | } |
802f84ff | 1563 | delete_flows__(&rules, OFPRR_DELETE, NULL); |
7ee20df1 | 1564 | } |
3948503a JR |
1565 | /* XXX: Concurrent handler threads may insert new learned flows based on |
1566 | * learn actions of the now deleted flows right after we release | |
1567 | * 'ofproto_mutex'. */ | |
15aaf599 | 1568 | ovs_mutex_unlock(&ofproto_mutex); |
7ee20df1 BP |
1569 | } |
1570 | ||
abe529af BP |
1571 | static void |
1572 | ofproto_destroy__(struct ofproto *ofproto) | |
15aaf599 | 1573 | OVS_EXCLUDED(ofproto_mutex) |
abe529af | 1574 | { |
d0918789 | 1575 | struct oftable *table; |
6c1491fb | 1576 | |
db88b35c | 1577 | cmap_destroy(&ofproto->groups); |
7395c052 | 1578 | |
abe529af | 1579 | hmap_remove(&all_ofprotos, &ofproto->hmap_node); |
8d8ab6c2 JG |
1580 | tun_metadata_free(ovsrcu_get_protected(struct tun_table *, |
1581 | &ofproto->metadata_tab)); | |
04f48a68 YHW |
1582 | |
1583 | ovs_mutex_lock(&ofproto->vl_mff_map.mutex); | |
1584 | mf_vl_mff_map_clear(&ofproto->vl_mff_map); | |
1585 | ovs_mutex_unlock(&ofproto->vl_mff_map.mutex); | |
1586 | cmap_destroy(&ofproto->vl_mff_map.cmap); | |
1587 | ovs_mutex_destroy(&ofproto->vl_mff_map.mutex); | |
1588 | ||
abe529af | 1589 | free(ofproto->name); |
955a7127 | 1590 | free(ofproto->type); |
abe529af BP |
1591 | free(ofproto->mfr_desc); |
1592 | free(ofproto->hw_desc); | |
1593 | free(ofproto->sw_desc); | |
1594 | free(ofproto->serial_desc); | |
1595 | free(ofproto->dp_desc); | |
abe529af | 1596 | hmap_destroy(&ofproto->ports); |
fdcea803 | 1597 | hmap_destroy(&ofproto->ofport_usage); |
abe529af | 1598 | shash_destroy(&ofproto->port_by_name); |
e1b1d06a | 1599 | simap_destroy(&ofproto->ofp_requests); |
6c1491fb | 1600 | |
b772ded8 | 1601 | OFPROTO_FOR_EACH_TABLE (table, ofproto) { |
d0918789 | 1602 | oftable_destroy(table); |
6c1491fb BP |
1603 | } |
1604 | free(ofproto->tables); | |
fa066f01 | 1605 | |
1406c79b BP |
1606 | ovs_assert(hindex_is_empty(&ofproto->cookies)); |
1607 | hindex_destroy(&ofproto->cookies); | |
1608 | ||
35f48b8b BP |
1609 | ovs_assert(hmap_is_empty(&ofproto->learned_cookies)); |
1610 | hmap_destroy(&ofproto->learned_cookies); | |
1611 | ||
abe529af BP |
1612 | ofproto->ofproto_class->dealloc(ofproto); |
1613 | } | |
fa066f01 | 1614 | |
39c94593 JR |
1615 | /* Destroying rules is doubly deferred, must have 'ofproto' around for them. |
1616 | * - 1st we defer the removal of the rules from the classifier | |
1617 | * - 2nd we defer the actual destruction of the rules. */ | |
1618 | static void | |
1619 | ofproto_destroy_defer__(struct ofproto *ofproto) | |
1620 | OVS_EXCLUDED(ofproto_mutex) | |
1621 | { | |
1622 | ovsrcu_postpone(ofproto_destroy__, ofproto); | |
1623 | } | |
1624 | ||
064af421 | 1625 | void |
9aad5a5a | 1626 | ofproto_destroy(struct ofproto *p, bool del) |
15aaf599 | 1627 | OVS_EXCLUDED(ofproto_mutex) |
064af421 | 1628 | { |
ca0f572c | 1629 | struct ofport *ofport, *next_ofport; |
4ec3d7c7 | 1630 | struct ofport_usage *usage; |
064af421 BP |
1631 | |
1632 | if (!p) { | |
1633 | return; | |
1634 | } | |
1635 | ||
717de9ff JR |
1636 | if (p->meters) { |
1637 | meter_delete(p, 1, p->meter_features.max_meters); | |
1638 | p->meter_features.max_meters = 0; | |
1639 | free(p->meters); | |
1640 | p->meters = NULL; | |
1641 | } | |
1642 | ||
7ee20df1 | 1643 | ofproto_flush__(p); |
4e8e4213 | 1644 | HMAP_FOR_EACH_SAFE (ofport, next_ofport, hmap_node, &p->ports) { |
9aad5a5a | 1645 | ofport_destroy(ofport, del); |
064af421 | 1646 | } |
064af421 | 1647 | |
4ec3d7c7 | 1648 | HMAP_FOR_EACH_POP (usage, hmap_node, &p->ofport_usage) { |
fdcea803 GS |
1649 | free(usage); |
1650 | } | |
1651 | ||
abe529af | 1652 | p->ofproto_class->destruct(p); |
58f19539 DDP |
1653 | |
1654 | /* We should not postpone this because it involves deleting a listening | |
25010c68 JR |
1655 | * socket which we may want to reopen soon. 'connmgr' may be used by other |
1656 | * threads only if they take the ofproto_mutex and read a non-NULL | |
1657 | * 'ofproto->connmgr'. */ | |
1658 | ovs_mutex_lock(&ofproto_mutex); | |
58f19539 | 1659 | connmgr_destroy(p->connmgr); |
25010c68 JR |
1660 | p->connmgr = NULL; |
1661 | ovs_mutex_unlock(&ofproto_mutex); | |
58f19539 | 1662 | |
f416c8d6 | 1663 | /* Destroying rules is deferred, must have 'ofproto' around for them. */ |
39c94593 | 1664 | ovsrcu_postpone(ofproto_destroy_defer__, p); |
064af421 BP |
1665 | } |
1666 | ||
abe529af BP |
1667 | /* Destroys the datapath with the respective 'name' and 'type'. With the Linux |
1668 | * kernel datapath, for example, this destroys the datapath in the kernel, and | |
1669 | * with the netdev-based datapath, it tears down the data structures that | |
1670 | * represent the datapath. | |
1671 | * | |
1672 | * The datapath should not be currently open as an ofproto. */ | |
064af421 | 1673 | int |
abe529af | 1674 | ofproto_delete(const char *name, const char *type) |
064af421 | 1675 | { |
abe529af BP |
1676 | const struct ofproto_class *class = ofproto_class_find__(type); |
1677 | return (!class ? EAFNOSUPPORT | |
1678 | : !class->del ? EACCES | |
1679 | : class->del(type, name)); | |
064af421 BP |
1680 | } |
1681 | ||
e9e28be3 BP |
1682 | static void |
1683 | process_port_change(struct ofproto *ofproto, int error, char *devname) | |
1684 | { | |
1685 | if (error == ENOBUFS) { | |
1686 | reinit_ports(ofproto); | |
1687 | } else if (!error) { | |
1688 | update_port(ofproto, devname); | |
1689 | free(devname); | |
1690 | } | |
1691 | } | |
1692 | ||
11a574a7 JP |
1693 | int |
1694 | ofproto_type_run(const char *datapath_type) | |
1695 | { | |
1696 | const struct ofproto_class *class; | |
1697 | int error; | |
1698 | ||
1699 | datapath_type = ofproto_normalize_type(datapath_type); | |
1700 | class = ofproto_class_find__(datapath_type); | |
1701 | ||
1702 | error = class->type_run ? class->type_run(datapath_type) : 0; | |
1703 | if (error && error != EAGAIN) { | |
1704 | VLOG_ERR_RL(&rl, "%s: type_run failed (%s)", | |
10a89ef0 | 1705 | datapath_type, ovs_strerror(error)); |
11a574a7 JP |
1706 | } |
1707 | return error; | |
1708 | } | |
1709 | ||
11a574a7 JP |
1710 | void |
1711 | ofproto_type_wait(const char *datapath_type) | |
1712 | { | |
1713 | const struct ofproto_class *class; | |
1714 | ||
1715 | datapath_type = ofproto_normalize_type(datapath_type); | |
1716 | class = ofproto_class_find__(datapath_type); | |
1717 | ||
1718 | if (class->type_wait) { | |
1719 | class->type_wait(datapath_type); | |
1720 | } | |
1721 | } | |
1722 | ||
064af421 | 1723 | int |
abe529af | 1724 | ofproto_run(struct ofproto *p) |
064af421 | 1725 | { |
064af421 | 1726 | int error; |
da4a6191 | 1727 | uint64_t new_seq; |
064af421 | 1728 | |
abe529af | 1729 | error = p->ofproto_class->run(p); |
5fcc0d00 | 1730 | if (error && error != EAGAIN) { |
10a89ef0 | 1731 | VLOG_ERR_RL(&rl, "%s: run failed (%s)", p->name, ovs_strerror(error)); |
149f577a JG |
1732 | } |
1733 | ||
448c2fa8 EJ |
1734 | /* Restore the eviction group heap invariant occasionally. */ |
1735 | if (p->eviction_group_timer < time_msec()) { | |
1736 | size_t i; | |
1737 | ||
1738 | p->eviction_group_timer = time_msec() + 1000; | |
1739 | ||
1740 | for (i = 0; i < p->n_tables; i++) { | |
1741 | struct oftable *table = &p->tables[i]; | |
1742 | struct eviction_group *evg; | |
448c2fa8 EJ |
1743 | struct rule *rule; |
1744 | ||
82c22d34 | 1745 | if (!table->eviction) { |
448c2fa8 EJ |
1746 | continue; |
1747 | } | |
1748 | ||
d79e3d70 | 1749 | if (table->n_flows > 100000) { |
1a9bb326 EJ |
1750 | static struct vlog_rate_limit count_rl = |
1751 | VLOG_RATE_LIMIT_INIT(1, 1); | |
1752 | VLOG_WARN_RL(&count_rl, "Table %"PRIuSIZE" has an excessive" | |
d79e3d70 | 1753 | " number of rules: %d", i, table->n_flows); |
1a9bb326 EJ |
1754 | } |
1755 | ||
15aaf599 | 1756 | ovs_mutex_lock(&ofproto_mutex); |
5f0476ce | 1757 | CLS_FOR_EACH (rule, cr, &table->cls) { |
6d56c1f1 K |
1758 | if (rule->idle_timeout || rule->hard_timeout) { |
1759 | if (!rule->eviction_group) { | |
1760 | eviction_group_add_rule(rule); | |
1761 | } else { | |
1762 | heap_raw_change(&rule->evg_node, | |
1763 | rule_eviction_priority(p, rule)); | |
1764 | } | |
448c2fa8 EJ |
1765 | } |
1766 | } | |
6d56c1f1 K |
1767 | |
1768 | HEAP_FOR_EACH (evg, size_node, &table->eviction_groups_by_size) { | |
1769 | heap_rebuild(&evg->rules); | |
1770 | } | |
15aaf599 | 1771 | ovs_mutex_unlock(&ofproto_mutex); |
448c2fa8 EJ |
1772 | } |
1773 | } | |
1774 | ||
5bf0e941 | 1775 | if (p->ofproto_class->port_poll) { |
7436ed80 BP |
1776 | char *devname; |
1777 | ||
5bf0e941 BP |
1778 | while ((error = p->ofproto_class->port_poll(p, &devname)) != EAGAIN) { |
1779 | process_port_change(p, error, devname); | |
064af421 | 1780 | } |
e9e28be3 | 1781 | } |
031d8bff | 1782 | |
da4a6191 JS |
1783 | new_seq = seq_read(connectivity_seq_get()); |
1784 | if (new_seq != p->change_seq) { | |
1785 | struct sset devnames; | |
1786 | const char *devname; | |
1787 | struct ofport *ofport; | |
1788 | ||
1789 | /* Update OpenFlow port status for any port whose netdev has changed. | |
1790 | * | |
1791 | * Refreshing a given 'ofport' can cause an arbitrary ofport to be | |
1792 | * destroyed, so it's not safe to update ports directly from the | |
1793 | * HMAP_FOR_EACH loop, or even to use HMAP_FOR_EACH_SAFE. Instead, we | |
1794 | * need this two-phase approach. */ | |
1795 | sset_init(&devnames); | |
1796 | HMAP_FOR_EACH (ofport, hmap_node, &p->ports) { | |
61501798 AW |
1797 | uint64_t port_change_seq; |
1798 | ||
1799 | port_change_seq = netdev_get_change_seq(ofport->netdev); | |
1800 | if (ofport->change_seq != port_change_seq) { | |
1801 | ofport->change_seq = port_change_seq; | |
1802 | sset_add(&devnames, netdev_get_name(ofport->netdev)); | |
1803 | } | |
031d8bff | 1804 | } |
da4a6191 JS |
1805 | SSET_FOR_EACH (devname, &devnames) { |
1806 | update_port(p, devname); | |
1807 | } | |
1808 | sset_destroy(&devnames); | |
1809 | ||
1810 | p->change_seq = new_seq; | |
064af421 BP |
1811 | } |
1812 | ||
834fe5cb | 1813 | connmgr_run(p->connmgr, handle_openflow); |
064af421 | 1814 | |
5fcc0d00 BP |
1815 | return error; |
1816 | } | |
1817 | ||
064af421 BP |
1818 | void |
1819 | ofproto_wait(struct ofproto *p) | |
1820 | { | |
abe529af | 1821 | p->ofproto_class->wait(p); |
5bf0e941 BP |
1822 | if (p->ofproto_class->port_poll_wait) { |
1823 | p->ofproto_class->port_poll_wait(p); | |
e7934396 | 1824 | } |
da4a6191 | 1825 | seq_wait(connectivity_seq_get(), p->change_seq); |
834fe5cb | 1826 | connmgr_wait(p->connmgr); |
064af421 BP |
1827 | } |
1828 | ||
064af421 BP |
1829 | bool |
1830 | ofproto_is_alive(const struct ofproto *p) | |
1831 | { | |
19a87e36 | 1832 | return connmgr_has_controllers(p->connmgr); |
064af421 BP |
1833 | } |
1834 | ||
0d085684 BP |
1835 | /* Adds some memory usage statistics for 'ofproto' into 'usage', for use with |
1836 | * memory_report(). */ | |
1837 | void | |
1838 | ofproto_get_memory_usage(const struct ofproto *ofproto, struct simap *usage) | |
1839 | { | |
1840 | const struct oftable *table; | |
1841 | unsigned int n_rules; | |
1842 | ||
1843 | simap_increase(usage, "ports", hmap_count(&ofproto->ports)); | |
15aaf599 | 1844 | |
0d085684 BP |
1845 | n_rules = 0; |
1846 | OFPROTO_FOR_EACH_TABLE (table, ofproto) { | |
d79e3d70 | 1847 | n_rules += table->n_flows; |
0d085684 BP |
1848 | } |
1849 | simap_increase(usage, "rules", n_rules); | |
1850 | ||
1851 | if (ofproto->ofproto_class->get_memory_usage) { | |
1852 | ofproto->ofproto_class->get_memory_usage(ofproto, usage); | |
1853 | } | |
1854 | ||
1855 | connmgr_get_memory_usage(ofproto->connmgr, usage); | |
1856 | } | |
1857 | ||
1c030aa5 EJ |
1858 | void |
1859 | ofproto_type_get_memory_usage(const char *datapath_type, struct simap *usage) | |
1860 | { | |
1861 | const struct ofproto_class *class; | |
1862 | ||
1863 | datapath_type = ofproto_normalize_type(datapath_type); | |
1864 | class = ofproto_class_find__(datapath_type); | |
1865 | ||
1866 | if (class && class->type_get_memory_usage) { | |
1867 | class->type_get_memory_usage(datapath_type, usage); | |
1868 | } | |
1869 | } | |
1870 | ||
bffc0589 | 1871 | void |
2cdcb898 | 1872 | ofproto_get_ofproto_controller_info(const struct ofproto *ofproto, |
bffc0589 AE |
1873 | struct shash *info) |
1874 | { | |
19a87e36 | 1875 | connmgr_get_controller_info(ofproto->connmgr, info); |
bffc0589 AE |
1876 | } |
1877 | ||
1878 | void | |
1879 | ofproto_free_ofproto_controller_info(struct shash *info) | |
1880 | { | |
72ba2ed3 | 1881 | connmgr_free_controller_info(info); |
bffc0589 AE |
1882 | } |
1883 | ||
b5827b24 BP |
1884 | /* Makes a deep copy of 'old' into 'port'. */ |
1885 | void | |
1886 | ofproto_port_clone(struct ofproto_port *port, const struct ofproto_port *old) | |
1887 | { | |
1888 | port->name = xstrdup(old->name); | |
1889 | port->type = xstrdup(old->type); | |
1890 | port->ofp_port = old->ofp_port; | |
1891 | } | |
1892 | ||
1893 | /* Frees memory allocated to members of 'ofproto_port'. | |
3a6ccc8c | 1894 | * |
b5827b24 BP |
1895 | * Do not call this function on an ofproto_port obtained from |
1896 | * ofproto_port_dump_next(): that function retains ownership of the data in the | |
1897 | * ofproto_port. */ | |
1898 | void | |
1899 | ofproto_port_destroy(struct ofproto_port *ofproto_port) | |
1900 | { | |
1901 | free(ofproto_port->name); | |
1902 | free(ofproto_port->type); | |
1903 | } | |
1904 | ||
b5827b24 | 1905 | /* Initializes 'dump' to begin dumping the ports in an ofproto. |
3a6ccc8c | 1906 | * |
b5827b24 BP |
1907 | * This function provides no status indication. An error status for the entire |
1908 | * dump operation is provided when it is completed by calling | |
1909 | * ofproto_port_dump_done(). | |
1910 | */ | |
1911 | void | |
1912 | ofproto_port_dump_start(struct ofproto_port_dump *dump, | |
1913 | const struct ofproto *ofproto) | |
1914 | { | |
abe529af BP |
1915 | dump->ofproto = ofproto; |
1916 | dump->error = ofproto->ofproto_class->port_dump_start(ofproto, | |
1917 | &dump->state); | |
b5827b24 BP |
1918 | } |
1919 | ||
1920 | /* Attempts to retrieve another port from 'dump', which must have been created | |
1921 | * with ofproto_port_dump_start(). On success, stores a new ofproto_port into | |
1922 | * 'port' and returns true. On failure, returns false. | |
1923 | * | |
1924 | * Failure might indicate an actual error or merely that the last port has been | |
1925 | * dumped. An error status for the entire dump operation is provided when it | |
1926 | * is completed by calling ofproto_port_dump_done(). | |
1927 | * | |
1928 | * The ofproto owns the data stored in 'port'. It will remain valid until at | |
1929 | * least the next time 'dump' is passed to ofproto_port_dump_next() or | |
1930 | * ofproto_port_dump_done(). */ | |
1931 | bool | |
1932 | ofproto_port_dump_next(struct ofproto_port_dump *dump, | |
1933 | struct ofproto_port *port) | |
1934 | { | |
abe529af BP |
1935 | const struct ofproto *ofproto = dump->ofproto; |
1936 | ||
1937 | if (dump->error) { | |
1938 | return false; | |
1939 | } | |
b5827b24 | 1940 | |
abe529af BP |
1941 | dump->error = ofproto->ofproto_class->port_dump_next(ofproto, dump->state, |
1942 | port); | |
1943 | if (dump->error) { | |
1944 | ofproto->ofproto_class->port_dump_done(ofproto, dump->state); | |
1945 | return false; | |
b5827b24 | 1946 | } |
abe529af | 1947 | return true; |
b5827b24 BP |
1948 | } |
1949 | ||
1950 | /* Completes port table dump operation 'dump', which must have been created | |
1951 | * with ofproto_port_dump_start(). Returns 0 if the dump operation was | |
1952 | * error-free, otherwise a positive errno value describing the problem. */ | |
3a6ccc8c | 1953 | int |
b5827b24 | 1954 | ofproto_port_dump_done(struct ofproto_port_dump *dump) |
3a6ccc8c | 1955 | { |
abe529af BP |
1956 | const struct ofproto *ofproto = dump->ofproto; |
1957 | if (!dump->error) { | |
1958 | dump->error = ofproto->ofproto_class->port_dump_done(ofproto, | |
1959 | dump->state); | |
1960 | } | |
1961 | return dump->error == EOF ? 0 : dump->error; | |
b5827b24 BP |
1962 | } |
1963 | ||
0aeaabc8 JP |
1964 | /* Returns the type to pass to netdev_open() when a datapath of type |
1965 | * 'datapath_type' has a port of type 'port_type', for a few special | |
1966 | * cases when a netdev type differs from a port type. For example, when | |
1967 | * using the userspace datapath, a port of type "internal" needs to be | |
1968 | * opened as "tap". | |
1969 | * | |
1970 | * Returns either 'type' itself or a string literal, which must not be | |
1971 | * freed. */ | |
1972 | const char * | |
1973 | ofproto_port_open_type(const char *datapath_type, const char *port_type) | |
1974 | { | |
1975 | const struct ofproto_class *class; | |
1976 | ||
1977 | datapath_type = ofproto_normalize_type(datapath_type); | |
1978 | class = ofproto_class_find__(datapath_type); | |
1979 | if (!class) { | |
1980 | return port_type; | |
1981 | } | |
1982 | ||
1983 | return (class->port_open_type | |
1984 | ? class->port_open_type(datapath_type, port_type) | |
1985 | : port_type); | |
1986 | } | |
1987 | ||
81816a5f JP |
1988 | /* Attempts to add 'netdev' as a port on 'ofproto'. If 'ofp_portp' is |
1989 | * non-null and '*ofp_portp' is not OFPP_NONE, attempts to use that as | |
1990 | * the port's OpenFlow port number. | |
1991 | * | |
1992 | * If successful, returns 0 and sets '*ofp_portp' to the new port's | |
1993 | * OpenFlow port number (if 'ofp_portp' is non-null). On failure, | |
1994 | * returns a positive errno value and sets '*ofp_portp' to OFPP_NONE (if | |
1995 | * 'ofp_portp' is non-null). */ | |
b5827b24 BP |
1996 | int |
1997 | ofproto_port_add(struct ofproto *ofproto, struct netdev *netdev, | |
4e022ec0 | 1998 | ofp_port_t *ofp_portp) |
b5827b24 | 1999 | { |
4e022ec0 | 2000 | ofp_port_t ofp_port = ofp_portp ? *ofp_portp : OFPP_NONE; |
3a6ccc8c BP |
2001 | int error; |
2002 | ||
e1b1d06a | 2003 | error = ofproto->ofproto_class->port_add(ofproto, netdev); |
1fa24dea | 2004 | if (!error) { |
e1b1d06a JP |
2005 | const char *netdev_name = netdev_get_name(netdev); |
2006 | ||
4e022ec0 AW |
2007 | simap_put(&ofproto->ofp_requests, netdev_name, |
2008 | ofp_to_u16(ofp_port)); | |
01c77073 | 2009 | error = update_port(ofproto, netdev_name); |
1fa24dea | 2010 | } |
b5827b24 | 2011 | if (ofp_portp) { |
17f69db5 BP |
2012 | *ofp_portp = OFPP_NONE; |
2013 | if (!error) { | |
2014 | struct ofproto_port ofproto_port; | |
2015 | ||
2016 | error = ofproto_port_query_by_name(ofproto, | |
2017 | netdev_get_name(netdev), | |
2018 | &ofproto_port); | |
2019 | if (!error) { | |
2020 | *ofp_portp = ofproto_port.ofp_port; | |
2021 | ofproto_port_destroy(&ofproto_port); | |
2022 | } | |
2023 | } | |
3a6ccc8c BP |
2024 | } |
2025 | return error; | |
2026 | } | |
2027 | ||
b5827b24 BP |
2028 | /* Looks up a port named 'devname' in 'ofproto'. On success, returns 0 and |
2029 | * initializes '*port' appropriately; on failure, returns a positive errno | |
2030 | * value. | |
2031 | * | |
abe529af | 2032 | * The caller owns the data in 'ofproto_port' and must free it with |
b5827b24 BP |
2033 | * ofproto_port_destroy() when it is no longer needed. */ |
2034 | int | |
2035 | ofproto_port_query_by_name(const struct ofproto *ofproto, const char *devname, | |
2036 | struct ofproto_port *port) | |
a4e2e1f2 | 2037 | { |
b5827b24 BP |
2038 | int error; |
2039 | ||
abe529af BP |
2040 | error = ofproto->ofproto_class->port_query_by_name(ofproto, devname, port); |
2041 | if (error) { | |
2042 | memset(port, 0, sizeof *port); | |
b5827b24 BP |
2043 | } |
2044 | return error; | |
a4e2e1f2 EJ |
2045 | } |
2046 | ||
b5827b24 | 2047 | /* Deletes port number 'ofp_port' from the datapath for 'ofproto'. |
3a6ccc8c | 2048 | * Returns 0 if successful, otherwise a positive errno. */ |
064af421 | 2049 | int |
4e022ec0 | 2050 | ofproto_port_del(struct ofproto *ofproto, ofp_port_t ofp_port) |
064af421 | 2051 | { |
abe529af | 2052 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); |
1264ec95 | 2053 | const char *name = ofport ? netdev_get_name(ofport->netdev) : "<unknown>"; |
e1b1d06a | 2054 | struct simap_node *ofp_request_node; |
3cf10406 | 2055 | int error; |
cdee00fd | 2056 | |
e1b1d06a JP |
2057 | ofp_request_node = simap_find(&ofproto->ofp_requests, name); |
2058 | if (ofp_request_node) { | |
2059 | simap_delete(&ofproto->ofp_requests, ofp_request_node); | |
2060 | } | |
2061 | ||
abe529af | 2062 | error = ofproto->ofproto_class->port_del(ofproto, ofp_port); |
892815f5 | 2063 | if (!error && ofport) { |
1264ec95 BP |
2064 | /* 'name' is the netdev's name and update_port() is going to close the |
2065 | * netdev. Just in case update_port() refers to 'name' after it | |
3a6ccc8c BP |
2066 | * destroys 'ofport', make a copy of it around the update_port() |
2067 | * call. */ | |
2068 | char *devname = xstrdup(name); | |
2069 | update_port(ofproto, devname); | |
2070 | free(devname); | |
3cf10406 BP |
2071 | } |
2072 | return error; | |
064af421 BP |
2073 | } |
2074 | ||
91364d18 IM |
2075 | /* Refreshes datapath configuration of port number 'ofp_port' in 'ofproto'. |
2076 | * | |
2077 | * This function has no effect if 'ofproto' does not have a port 'ofp_port'. */ | |
2078 | void | |
2079 | ofproto_port_set_config(struct ofproto *ofproto, ofp_port_t ofp_port, | |
2080 | const struct smap *cfg) | |
2081 | { | |
2082 | struct ofport *ofport; | |
2083 | int error; | |
2084 | ||
2085 | ofport = ofproto_get_port(ofproto, ofp_port); | |
2086 | if (!ofport) { | |
94783c7c | 2087 | VLOG_WARN("%s: cannot configure datapath on nonexistent port %"PRIu32, |
91364d18 IM |
2088 | ofproto->name, ofp_port); |
2089 | return; | |
2090 | } | |
2091 | ||
2092 | error = (ofproto->ofproto_class->port_set_config | |
2093 | ? ofproto->ofproto_class->port_set_config(ofport, cfg) | |
2094 | : EOPNOTSUPP); | |
2095 | if (error) { | |
94783c7c | 2096 | VLOG_WARN("%s: datapath configuration on port %"PRIu32 |
91364d18 IM |
2097 | " (%s) failed (%s)", |
2098 | ofproto->name, ofp_port, netdev_get_name(ofport->netdev), | |
2099 | ovs_strerror(error)); | |
2100 | } | |
2101 | } | |
2102 | ||
2103 | ||
276f6518 SH |
2104 | static void |
2105 | flow_mod_init(struct ofputil_flow_mod *fm, | |
eb391b76 | 2106 | const struct match *match, int priority, |
276f6518 SH |
2107 | const struct ofpact *ofpacts, size_t ofpacts_len, |
2108 | enum ofp_flow_mod_command command) | |
2109 | { | |
39cc5c4a BP |
2110 | *fm = (struct ofputil_flow_mod) { |
2111 | .match = *match, | |
2112 | .priority = priority, | |
2113 | .table_id = 0, | |
2114 | .command = command, | |
2115 | .buffer_id = UINT32_MAX, | |
2116 | .out_port = OFPP_ANY, | |
2117 | .out_group = OFPG_ANY, | |
2118 | .ofpacts = CONST_CAST(struct ofpact *, ofpacts), | |
2119 | .ofpacts_len = ofpacts_len, | |
39cc5c4a | 2120 | }; |
276f6518 SH |
2121 | } |
2122 | ||
97f63e57 BP |
2123 | static int |
2124 | simple_flow_mod(struct ofproto *ofproto, | |
eb391b76 | 2125 | const struct match *match, int priority, |
97f63e57 BP |
2126 | const struct ofpact *ofpacts, size_t ofpacts_len, |
2127 | enum ofp_flow_mod_command command) | |
2128 | { | |
7338102b | 2129 | struct ofputil_flow_mod fm; |
97f63e57 | 2130 | |
7338102b | 2131 | flow_mod_init(&fm, match, priority, ofpacts, ofpacts_len, command); |
15aaf599 | 2132 | |
7338102b | 2133 | return handle_flow_mod__(ofproto, &fm, NULL); |
97f63e57 BP |
2134 | } |
2135 | ||
6c1491fb | 2136 | /* Adds a flow to OpenFlow flow table 0 in 'p' that matches 'cls_rule' and |
fa8b054f BP |
2137 | * performs the 'n_actions' actions in 'actions'. The new flow will not |
2138 | * timeout. | |
2139 | * | |
2140 | * If cls_rule->priority is in the range of priorities supported by OpenFlow | |
2141 | * (0...65535, inclusive) then the flow will be visible to OpenFlow | |
2142 | * controllers; otherwise, it will be hidden. | |
2143 | * | |
f25d0cf3 | 2144 | * The caller retains ownership of 'cls_rule' and 'ofpacts'. |
6c1491fb BP |
2145 | * |
2146 | * This is a helper function for in-band control and fail-open. */ | |
064af421 | 2147 | void |
81a76618 | 2148 | ofproto_add_flow(struct ofproto *ofproto, const struct match *match, |
eb391b76 | 2149 | int priority, |
f25d0cf3 | 2150 | const struct ofpact *ofpacts, size_t ofpacts_len) |
15aaf599 | 2151 | OVS_EXCLUDED(ofproto_mutex) |
064af421 | 2152 | { |
7ee20df1 | 2153 | const struct rule *rule; |
6f00e29b | 2154 | bool must_add; |
7ee20df1 | 2155 | |
97f63e57 BP |
2156 | /* First do a cheap check whether the rule we're looking for already exists |
2157 | * with the actions that we want. If it does, then we're done. */ | |
81a76618 | 2158 | rule = rule_from_cls_rule(classifier_find_match_exactly( |
2b7b1427 | 2159 | &ofproto->tables[0].cls, match, priority, |
44e0c35d | 2160 | OVS_VERSION_MAX)); |
6f00e29b | 2161 | if (rule) { |
dc723c44 | 2162 | const struct rule_actions *actions = rule_get_actions(rule); |
06a0f3e2 | 2163 | must_add = !ofpacts_equal(actions->ofpacts, actions->ofpacts_len, |
6f00e29b | 2164 | ofpacts, ofpacts_len); |
6f00e29b BP |
2165 | } else { |
2166 | must_add = true; | |
2167 | } | |
97f63e57 BP |
2168 | |
2169 | /* If there's no such rule or the rule doesn't have the actions we want, | |
2170 | * fall back to a executing a full flow mod. We can't optimize this at | |
2171 | * all because we didn't take enough locks above to ensure that the flow | |
2172 | * table didn't already change beneath us. */ | |
6f00e29b | 2173 | if (must_add) { |
97f63e57 BP |
2174 | simple_flow_mod(ofproto, match, priority, ofpacts, ofpacts_len, |
2175 | OFPFC_MODIFY_STRICT); | |
7ee20df1 | 2176 | } |
064af421 BP |
2177 | } |
2178 | ||
d51c8b71 JR |
2179 | /* Executes the flow modification specified in 'fm'. Returns 0 on success, or |
2180 | * an OFPERR_* OpenFlow error code on failure. | |
75a75043 | 2181 | * |
2c7ee524 | 2182 | * This is a helper function for in-band control and fail-open. */ |
d51c8b71 | 2183 | enum ofperr |
7338102b | 2184 | ofproto_flow_mod(struct ofproto *ofproto, const struct ofputil_flow_mod *fm) |
15aaf599 | 2185 | OVS_EXCLUDED(ofproto_mutex) |
75a75043 | 2186 | { |
7338102b | 2187 | return handle_flow_mod__(ofproto, fm, NULL); |
75a75043 BP |
2188 | } |
2189 | ||
6c1491fb BP |
2190 | /* Searches for a rule with matching criteria exactly equal to 'target' in |
2191 | * ofproto's table 0 and, if it finds one, deletes it. | |
2192 | * | |
2193 | * This is a helper function for in-band control and fail-open. */ | |
b20f4073 | 2194 | void |
81a76618 | 2195 | ofproto_delete_flow(struct ofproto *ofproto, |
eb391b76 | 2196 | const struct match *target, int priority) |
25010c68 | 2197 | OVS_REQUIRES(ofproto_mutex) |
064af421 | 2198 | { |
8037acb4 | 2199 | struct classifier *cls = &ofproto->tables[0].cls; |
064af421 BP |
2200 | struct rule *rule; |
2201 | ||
97f63e57 BP |
2202 | /* First do a cheap check whether the rule we're looking for has already |
2203 | * been deleted. If so, then we're done. */ | |
39c94593 | 2204 | rule = rule_from_cls_rule(classifier_find_match_exactly( |
44e0c35d | 2205 | cls, target, priority, OVS_VERSION_MAX)); |
834fe5cb BP |
2206 | if (!rule) { |
2207 | return; | |
bcf84111 | 2208 | } |
834fe5cb | 2209 | |
25010c68 JR |
2210 | struct rule_collection rules; |
2211 | ||
2212 | rule_collection_init(&rules); | |
2213 | rule_collection_add(&rules, rule); | |
2214 | delete_flows__(&rules, OFPRR_DELETE, NULL); | |
2215 | rule_collection_destroy(&rules); | |
142e1f5c BP |
2216 | } |
2217 | ||
834fe5cb BP |
2218 | /* Delete all of the flows from all of ofproto's flow tables, then reintroduce |
2219 | * the flows required by in-band control and fail-open. */ | |
142e1f5c BP |
2220 | void |
2221 | ofproto_flush_flows(struct ofproto *ofproto) | |
2222 | { | |
7ee20df1 | 2223 | COVERAGE_INC(ofproto_flush); |
834fe5cb BP |
2224 | ofproto_flush__(ofproto); |
2225 | connmgr_flushed(ofproto->connmgr); | |
064af421 BP |
2226 | } |
2227 | \f | |
2228 | static void | |
2229 | reinit_ports(struct ofproto *p) | |
2230 | { | |
abe529af | 2231 | struct ofproto_port_dump dump; |
b3c01ed3 | 2232 | struct sset devnames; |
064af421 | 2233 | struct ofport *ofport; |
abe529af | 2234 | struct ofproto_port ofproto_port; |
b3c01ed3 | 2235 | const char *devname; |
064af421 | 2236 | |
898bf89d JP |
2237 | COVERAGE_INC(ofproto_reinit_ports); |
2238 | ||
b3c01ed3 | 2239 | sset_init(&devnames); |
4e8e4213 | 2240 | HMAP_FOR_EACH (ofport, hmap_node, &p->ports) { |
1264ec95 | 2241 | sset_add(&devnames, netdev_get_name(ofport->netdev)); |
064af421 | 2242 | } |
abe529af BP |
2243 | OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) { |
2244 | sset_add(&devnames, ofproto_port.name); | |
064af421 | 2245 | } |
064af421 | 2246 | |
b3c01ed3 BP |
2247 | SSET_FOR_EACH (devname, &devnames) { |
2248 | update_port(p, devname); | |
064af421 | 2249 | } |
b3c01ed3 | 2250 | sset_destroy(&devnames); |
064af421 BP |
2251 | } |
2252 | ||
4e022ec0 | 2253 | static ofp_port_t |
e1b1d06a JP |
2254 | alloc_ofp_port(struct ofproto *ofproto, const char *netdev_name) |
2255 | { | |
4e022ec0 | 2256 | uint16_t port_idx; |
e1b1d06a | 2257 | |
4e022ec0 | 2258 | port_idx = simap_get(&ofproto->ofp_requests, netdev_name); |
430dbb14 | 2259 | port_idx = port_idx ? port_idx : UINT16_MAX; |
4e022ec0 | 2260 | |
430dbb14 | 2261 | if (port_idx >= ofproto->max_ports |
fdcea803 GS |
2262 | || ofport_get_usage(ofproto, u16_to_ofp(port_idx)) == LLONG_MAX) { |
2263 | uint16_t lru_ofport = 0, end_port_no = ofproto->alloc_port_no; | |
2264 | long long int last_used_at, lru = LLONG_MAX; | |
e1b1d06a | 2265 | |
e1b1d06a JP |
2266 | /* Search for a free OpenFlow port number. We try not to |
2267 | * immediately reuse them to prevent problems due to old | |
484c8355 BP |
2268 | * flows. |
2269 | * | |
2270 | * We limit the automatically assigned port numbers to the lower half | |
2271 | * of the port range, to reserve the upper half for assignment by | |
2272 | * controllers. */ | |
6033d9d9 | 2273 | for (;;) { |
484c8355 | 2274 | if (++ofproto->alloc_port_no >= MIN(ofproto->max_ports, 32768)) { |
fdcea803 | 2275 | ofproto->alloc_port_no = 1; |
6033d9d9 | 2276 | } |
fdcea803 GS |
2277 | last_used_at = ofport_get_usage(ofproto, |
2278 | u16_to_ofp(ofproto->alloc_port_no)); | |
2279 | if (!last_used_at) { | |
430dbb14 | 2280 | port_idx = ofproto->alloc_port_no; |
6033d9d9 | 2281 | break; |
865b26a4 GS |
2282 | } else if ( last_used_at < time_msec() - 60*60*1000) { |
2283 | /* If the port with ofport 'ofproto->alloc_port_no' was deleted | |
2284 | * more than an hour ago, consider it usable. */ | |
2285 | ofport_remove_usage(ofproto, | |
2286 | u16_to_ofp(ofproto->alloc_port_no)); | |
2287 | port_idx = ofproto->alloc_port_no; | |
2288 | break; | |
fdcea803 GS |
2289 | } else if (last_used_at < lru) { |
2290 | lru = last_used_at; | |
2291 | lru_ofport = ofproto->alloc_port_no; | |
e1b1d06a | 2292 | } |
fdcea803 | 2293 | |
430dbb14 | 2294 | if (ofproto->alloc_port_no == end_port_no) { |
fdcea803 GS |
2295 | if (lru_ofport) { |
2296 | port_idx = lru_ofport; | |
2297 | break; | |
2298 | } | |
6033d9d9 | 2299 | return OFPP_NONE; |
e1b1d06a JP |
2300 | } |
2301 | } | |
2302 | } | |
fdcea803 | 2303 | ofport_set_usage(ofproto, u16_to_ofp(port_idx), LLONG_MAX); |
4e022ec0 | 2304 | return u16_to_ofp(port_idx); |
e1b1d06a JP |
2305 | } |
2306 | ||
2307 | static void | |
fdcea803 | 2308 | dealloc_ofp_port(struct ofproto *ofproto, ofp_port_t ofp_port) |
e1b1d06a | 2309 | { |
430dbb14 | 2310 | if (ofp_to_u16(ofp_port) < ofproto->max_ports) { |
fdcea803 | 2311 | ofport_set_usage(ofproto, ofp_port, time_msec()); |
40fa9417 | 2312 | } |
e1b1d06a JP |
2313 | } |
2314 | ||
fbfa2911 BP |
2315 | /* Opens and returns a netdev for 'ofproto_port' in 'ofproto', or a null |
2316 | * pointer if the netdev cannot be opened. On success, also fills in | |
d7d92203 | 2317 | * '*pp'. */ |
b33951b8 | 2318 | static struct netdev * |
e1b1d06a JP |
2319 | ofport_open(struct ofproto *ofproto, |
2320 | struct ofproto_port *ofproto_port, | |
9e1fd49b | 2321 | struct ofputil_phy_port *pp) |
064af421 BP |
2322 | { |
2323 | enum netdev_flags flags; | |
064af421 | 2324 | struct netdev *netdev; |
064af421 BP |
2325 | int error; |
2326 | ||
18812dff | 2327 | error = netdev_open(ofproto_port->name, ofproto_port->type, &netdev); |
064af421 | 2328 | if (error) { |
94783c7c | 2329 | VLOG_WARN_RL(&rl, "%s: ignoring port %s (%"PRIu32") because netdev %s " |
064af421 | 2330 | "cannot be opened (%s)", |
fbfa2911 | 2331 | ofproto->name, |
abe529af | 2332 | ofproto_port->name, ofproto_port->ofp_port, |
10a89ef0 | 2333 | ofproto_port->name, ovs_strerror(error)); |
064af421 BP |
2334 | return NULL; |
2335 | } | |
2336 | ||
e1b1d06a JP |
2337 | if (ofproto_port->ofp_port == OFPP_NONE) { |
2338 | if (!strcmp(ofproto->name, ofproto_port->name)) { | |
2339 | ofproto_port->ofp_port = OFPP_LOCAL; | |
2340 | } else { | |
2341 | ofproto_port->ofp_port = alloc_ofp_port(ofproto, | |
2342 | ofproto_port->name); | |
2343 | } | |
2344 | } | |
9e1fd49b | 2345 | pp->port_no = ofproto_port->ofp_port; |
74ff3298 | 2346 | netdev_get_etheraddr(netdev, &pp->hw_addr); |
9e1fd49b | 2347 | ovs_strlcpy(pp->name, ofproto_port->name, sizeof pp->name); |
064af421 | 2348 | netdev_get_flags(netdev, &flags); |
9e1fd49b BP |
2349 | pp->config = flags & NETDEV_UP ? 0 : OFPUTIL_PC_PORT_DOWN; |
2350 | pp->state = netdev_get_carrier(netdev) ? 0 : OFPUTIL_PS_LINK_DOWN; | |
2351 | netdev_get_features(netdev, &pp->curr, &pp->advertised, | |
2352 | &pp->supported, &pp->peer); | |
cd65125d BP |
2353 | pp->curr_speed = netdev_features_to_bps(pp->curr, 0) / 1000; |
2354 | pp->max_speed = netdev_features_to_bps(pp->supported, 0) / 1000; | |
118c4676 | 2355 | |
b33951b8 | 2356 | return netdev; |
064af421 BP |
2357 | } |
2358 | ||
b33951b8 | 2359 | /* Returns true if most fields of 'a' and 'b' are equal. Differences in name, |
d7d92203 | 2360 | * port number, and 'config' bits other than OFPUTIL_PC_PORT_DOWN are |
b33951b8 BP |
2361 | * disregarded. */ |
2362 | static bool | |
9e1fd49b BP |
2363 | ofport_equal(const struct ofputil_phy_port *a, |
2364 | const struct ofputil_phy_port *b) | |
064af421 | 2365 | { |
9e1fd49b | 2366 | return (eth_addr_equals(a->hw_addr, b->hw_addr) |
064af421 | 2367 | && a->state == b->state |
9e1fd49b | 2368 | && !((a->config ^ b->config) & OFPUTIL_PC_PORT_DOWN) |
064af421 BP |
2369 | && a->curr == b->curr |
2370 | && a->advertised == b->advertised | |
2371 | && a->supported == b->supported | |
9e1fd49b BP |
2372 | && a->peer == b->peer |
2373 | && a->curr_speed == b->curr_speed | |
2374 | && a->max_speed == b->max_speed); | |
064af421 BP |
2375 | } |
2376 | ||
b33951b8 BP |
2377 | /* Adds an ofport to 'p' initialized based on the given 'netdev' and 'opp'. |
2378 | * The caller must ensure that 'p' does not have a conflicting ofport (that is, | |
2379 | * one with the same name or port number). */ | |
01c77073 | 2380 | static int |
b33951b8 | 2381 | ofport_install(struct ofproto *p, |
9e1fd49b | 2382 | struct netdev *netdev, const struct ofputil_phy_port *pp) |
064af421 | 2383 | { |
b33951b8 BP |
2384 | const char *netdev_name = netdev_get_name(netdev); |
2385 | struct ofport *ofport; | |
abe529af | 2386 | int error; |
72b06300 | 2387 | |
b33951b8 | 2388 | /* Create ofport. */ |
abe529af BP |
2389 | ofport = p->ofproto_class->port_alloc(); |
2390 | if (!ofport) { | |
2391 | error = ENOMEM; | |
2392 | goto error; | |
2393 | } | |
0f7d71a5 | 2394 | ofport->ofproto = p; |
b33951b8 | 2395 | ofport->netdev = netdev; |
61501798 | 2396 | ofport->change_seq = netdev_get_change_seq(netdev); |
9e1fd49b BP |
2397 | ofport->pp = *pp; |
2398 | ofport->ofp_port = pp->port_no; | |
65e0be10 | 2399 | ofport->created = time_msec(); |
b33951b8 BP |
2400 | |
2401 | /* Add port to 'p'. */ | |
4e022ec0 | 2402 | hmap_insert(&p->ports, &ofport->hmap_node, |
f9c0c3ec | 2403 | hash_ofp_port(ofport->ofp_port)); |
72b06300 | 2404 | shash_add(&p->port_by_name, netdev_name, ofport); |
abe529af | 2405 | |
ada3428f | 2406 | update_mtu(p, ofport); |
9197df76 | 2407 | |
abe529af BP |
2408 | /* Let the ofproto_class initialize its private data. */ |
2409 | error = p->ofproto_class->port_construct(ofport); | |
2410 | if (error) { | |
2411 | goto error; | |
2412 | } | |
2a6f78e0 | 2413 | connmgr_send_port_status(p->connmgr, NULL, pp, OFPPR_ADD); |
01c77073 | 2414 | return 0; |
abe529af BP |
2415 | |
2416 | error: | |
2417 | VLOG_WARN_RL(&rl, "%s: could not add port %s (%s)", | |
10a89ef0 | 2418 | p->name, netdev_name, ovs_strerror(error)); |
abe529af BP |
2419 | if (ofport) { |
2420 | ofport_destroy__(ofport); | |
2421 | } else { | |
2422 | netdev_close(netdev); | |
72b06300 | 2423 | } |
01c77073 | 2424 | return error; |
064af421 BP |
2425 | } |
2426 | ||
b33951b8 | 2427 | /* Removes 'ofport' from 'p' and destroys it. */ |
064af421 | 2428 | static void |
0f7d71a5 | 2429 | ofport_remove(struct ofport *ofport) |
064af421 | 2430 | { |
0670b5d0 | 2431 | struct ofproto *p = ofport->ofproto; |
3a414a0a | 2432 | bool is_mtu_overridden = ofport_is_mtu_overridden(p, ofport); |
0670b5d0 | 2433 | |
2a6f78e0 | 2434 | connmgr_send_port_status(ofport->ofproto->connmgr, NULL, &ofport->pp, |
fa066f01 | 2435 | OFPPR_DELETE); |
9aad5a5a | 2436 | ofport_destroy(ofport, true); |
3a414a0a | 2437 | if (!is_mtu_overridden) { |
0670b5d0 | 2438 | update_mtu_ofproto(p); |
2439 | } | |
b33951b8 BP |
2440 | } |
2441 | ||
2442 | /* If 'ofproto' contains an ofport named 'name', removes it from 'ofproto' and | |
2443 | * destroys it. */ | |
2444 | static void | |
2445 | ofport_remove_with_name(struct ofproto *ofproto, const char *name) | |
2446 | { | |
2447 | struct ofport *port = shash_find_data(&ofproto->port_by_name, name); | |
2448 | if (port) { | |
0f7d71a5 | 2449 | ofport_remove(port); |
b33951b8 BP |
2450 | } |
2451 | } | |
2452 | ||
9e1fd49b | 2453 | /* Updates 'port' with new 'pp' description. |
b33951b8 BP |
2454 | * |
2455 | * Does not handle a name or port number change. The caller must implement | |
2456 | * such a change as a delete followed by an add. */ | |
2457 | static void | |
9e1fd49b | 2458 | ofport_modified(struct ofport *port, struct ofputil_phy_port *pp) |
b33951b8 | 2459 | { |
74ff3298 | 2460 | port->pp.hw_addr = pp->hw_addr; |
9e1fd49b BP |
2461 | port->pp.config = ((port->pp.config & ~OFPUTIL_PC_PORT_DOWN) |
2462 | | (pp->config & OFPUTIL_PC_PORT_DOWN)); | |
53fd5c7c BP |
2463 | port->pp.state = ((port->pp.state & ~OFPUTIL_PS_LINK_DOWN) |
2464 | | (pp->state & OFPUTIL_PS_LINK_DOWN)); | |
9e1fd49b BP |
2465 | port->pp.curr = pp->curr; |
2466 | port->pp.advertised = pp->advertised; | |
2467 | port->pp.supported = pp->supported; | |
2468 | port->pp.peer = pp->peer; | |
2469 | port->pp.curr_speed = pp->curr_speed; | |
2470 | port->pp.max_speed = pp->max_speed; | |
b33951b8 | 2471 | |
2a6f78e0 BP |
2472 | connmgr_send_port_status(port->ofproto->connmgr, NULL, |
2473 | &port->pp, OFPPR_MODIFY); | |
064af421 BP |
2474 | } |
2475 | ||
5a2dfd47 JP |
2476 | /* Update OpenFlow 'state' in 'port' and notify controller. */ |
2477 | void | |
9e1fd49b | 2478 | ofproto_port_set_state(struct ofport *port, enum ofputil_port_state state) |
5a2dfd47 | 2479 | { |
9e1fd49b BP |
2480 | if (port->pp.state != state) { |
2481 | port->pp.state = state; | |
2a6f78e0 BP |
2482 | connmgr_send_port_status(port->ofproto->connmgr, NULL, |
2483 | &port->pp, OFPPR_MODIFY); | |
5a2dfd47 JP |
2484 | } |
2485 | } | |
2486 | ||
abe529af | 2487 | void |
4e022ec0 | 2488 | ofproto_port_unregister(struct ofproto *ofproto, ofp_port_t ofp_port) |
e7934396 | 2489 | { |
abe529af BP |
2490 | struct ofport *port = ofproto_get_port(ofproto, ofp_port); |
2491 | if (port) { | |
b308140a JP |
2492 | if (port->ofproto->ofproto_class->set_stp_port) { |
2493 | port->ofproto->ofproto_class->set_stp_port(port, NULL); | |
2494 | } | |
9efd308e DV |
2495 | if (port->ofproto->ofproto_class->set_rstp_port) { |
2496 | port->ofproto->ofproto_class->set_rstp_port(port, NULL); | |
2497 | } | |
abe529af | 2498 | if (port->ofproto->ofproto_class->set_cfm) { |
a5610457 | 2499 | port->ofproto->ofproto_class->set_cfm(port, NULL); |
abe529af BP |
2500 | } |
2501 | if (port->ofproto->ofproto_class->bundle_remove) { | |
2502 | port->ofproto->ofproto_class->bundle_remove(port); | |
e7934396 BP |
2503 | } |
2504 | } | |
2505 | } | |
2506 | ||
2507 | static void | |
abe529af | 2508 | ofport_destroy__(struct ofport *port) |
e7934396 | 2509 | { |
abe529af BP |
2510 | struct ofproto *ofproto = port->ofproto; |
2511 | const char *name = netdev_get_name(port->netdev); | |
fa066f01 | 2512 | |
abe529af BP |
2513 | hmap_remove(&ofproto->ports, &port->hmap_node); |
2514 | shash_delete(&ofproto->port_by_name, | |
2515 | shash_find(&ofproto->port_by_name, name)); | |
fa066f01 | 2516 | |
abe529af BP |
2517 | netdev_close(port->netdev); |
2518 | ofproto->ofproto_class->port_dealloc(port); | |
e7934396 BP |
2519 | } |
2520 | ||
064af421 | 2521 | static void |
9aad5a5a | 2522 | ofport_destroy(struct ofport *port, bool del) |
064af421 | 2523 | { |
fa066f01 | 2524 | if (port) { |
e1b1d06a | 2525 | dealloc_ofp_port(port->ofproto, port->ofp_port); |
9aad5a5a | 2526 | port->ofproto->ofproto_class->port_destruct(port, del); |
abe529af BP |
2527 | ofport_destroy__(port); |
2528 | } | |
064af421 BP |
2529 | } |
2530 | ||
abe529af | 2531 | struct ofport * |
4e022ec0 | 2532 | ofproto_get_port(const struct ofproto *ofproto, ofp_port_t ofp_port) |
ca0f572c BP |
2533 | { |
2534 | struct ofport *port; | |
2535 | ||
f9c0c3ec | 2536 | HMAP_FOR_EACH_IN_BUCKET (port, hmap_node, hash_ofp_port(ofp_port), |
4e022ec0 | 2537 | &ofproto->ports) { |
abe529af | 2538 | if (port->ofp_port == ofp_port) { |
ca0f572c BP |
2539 | return port; |
2540 | } | |
2541 | } | |
2542 | return NULL; | |
2543 | } | |
2544 | ||
fdcea803 GS |
2545 | static long long int |
2546 | ofport_get_usage(const struct ofproto *ofproto, ofp_port_t ofp_port) | |
2547 | { | |
2548 | struct ofport_usage *usage; | |
2549 | ||
2550 | HMAP_FOR_EACH_IN_BUCKET (usage, hmap_node, hash_ofp_port(ofp_port), | |
2551 | &ofproto->ofport_usage) { | |
2552 | if (usage->ofp_port == ofp_port) { | |
2553 | return usage->last_used; | |
2554 | } | |
2555 | } | |
2556 | return 0; | |
2557 | } | |
2558 | ||
2559 | static void | |
2560 | ofport_set_usage(struct ofproto *ofproto, ofp_port_t ofp_port, | |
2561 | long long int last_used) | |
2562 | { | |
2563 | struct ofport_usage *usage; | |
2564 | HMAP_FOR_EACH_IN_BUCKET (usage, hmap_node, hash_ofp_port(ofp_port), | |
2565 | &ofproto->ofport_usage) { | |
2566 | if (usage->ofp_port == ofp_port) { | |
2567 | usage->last_used = last_used; | |
2568 | return; | |
2569 | } | |
2570 | } | |
2571 | ovs_assert(last_used == LLONG_MAX); | |
2572 | ||
2573 | usage = xmalloc(sizeof *usage); | |
2574 | usage->ofp_port = ofp_port; | |
2575 | usage->last_used = last_used; | |
2576 | hmap_insert(&ofproto->ofport_usage, &usage->hmap_node, | |
2577 | hash_ofp_port(ofp_port)); | |
2578 | } | |
2579 | ||
865b26a4 GS |
2580 | static void |
2581 | ofport_remove_usage(struct ofproto *ofproto, ofp_port_t ofp_port) | |
2582 | { | |
2583 | struct ofport_usage *usage; | |
2584 | HMAP_FOR_EACH_IN_BUCKET (usage, hmap_node, hash_ofp_port(ofp_port), | |
2585 | &ofproto->ofport_usage) { | |
2586 | if (usage->ofp_port == ofp_port) { | |
2587 | hmap_remove(&ofproto->ofport_usage, &usage->hmap_node); | |
2588 | free(usage); | |
2589 | break; | |
2590 | } | |
2591 | } | |
2592 | } | |
2593 | ||
6527c598 PS |
2594 | int |
2595 | ofproto_port_get_stats(const struct ofport *port, struct netdev_stats *stats) | |
2596 | { | |
2597 | struct ofproto *ofproto = port->ofproto; | |
2598 | int error; | |
2599 | ||
2600 | if (ofproto->ofproto_class->port_get_stats) { | |
2601 | error = ofproto->ofproto_class->port_get_stats(port, stats); | |
2602 | } else { | |
2603 | error = EOPNOTSUPP; | |
2604 | } | |
2605 | ||
2606 | return error; | |
2607 | } | |
2608 | ||
01c77073 | 2609 | static int |
b33951b8 | 2610 | update_port(struct ofproto *ofproto, const char *name) |
064af421 | 2611 | { |
abe529af | 2612 | struct ofproto_port ofproto_port; |
9e1fd49b | 2613 | struct ofputil_phy_port pp; |
b33951b8 BP |
2614 | struct netdev *netdev; |
2615 | struct ofport *port; | |
01c77073 | 2616 | int error = 0; |
064af421 BP |
2617 | |
2618 | COVERAGE_INC(ofproto_update_port); | |
c874dc6d | 2619 | |
b33951b8 | 2620 | /* Fetch 'name''s location and properties from the datapath. */ |
abe529af | 2621 | netdev = (!ofproto_port_query_by_name(ofproto, name, &ofproto_port) |
fbfa2911 | 2622 | ? ofport_open(ofproto, &ofproto_port, &pp) |
b33951b8 | 2623 | : NULL); |
4e022ec0 | 2624 | |
b33951b8 | 2625 | if (netdev) { |
abe529af | 2626 | port = ofproto_get_port(ofproto, ofproto_port.ofp_port); |
b33951b8 | 2627 | if (port && !strcmp(netdev_get_name(port->netdev), name)) { |
e65942a0 BP |
2628 | struct netdev *old_netdev = port->netdev; |
2629 | ||
b33951b8 | 2630 | /* 'name' hasn't changed location. Any properties changed? */ |
9e1fd49b BP |
2631 | if (!ofport_equal(&port->pp, &pp)) { |
2632 | ofport_modified(port, &pp); | |
abe529af BP |
2633 | } |
2634 | ||
ada3428f | 2635 | update_mtu(ofproto, port); |
9197df76 | 2636 | |
e65942a0 BP |
2637 | /* Install the newly opened netdev in case it has changed. |
2638 | * Don't close the old netdev yet in case port_modified has to | |
2639 | * remove a retained reference to it.*/ | |
abe529af | 2640 | port->netdev = netdev; |
61501798 | 2641 | port->change_seq = netdev_get_change_seq(netdev); |
abe529af BP |
2642 | |
2643 | if (port->ofproto->ofproto_class->port_modified) { | |
2644 | port->ofproto->ofproto_class->port_modified(port); | |
b33951b8 | 2645 | } |
e65942a0 BP |
2646 | |
2647 | netdev_close(old_netdev); | |
b33951b8 BP |
2648 | } else { |
2649 | /* If 'port' is nonnull then its name differs from 'name' and thus | |
2650 | * we should delete it. If we think there's a port named 'name' | |
2651 | * then its port number must be wrong now so delete it too. */ | |
2652 | if (port) { | |
0f7d71a5 | 2653 | ofport_remove(port); |
b33951b8 BP |
2654 | } |
2655 | ofport_remove_with_name(ofproto, name); | |
01c77073 | 2656 | error = ofport_install(ofproto, netdev, &pp); |
c874dc6d | 2657 | } |
b33951b8 BP |
2658 | } else { |
2659 | /* Any port named 'name' is gone now. */ | |
2660 | ofport_remove_with_name(ofproto, name); | |
c874dc6d | 2661 | } |
abe529af | 2662 | ofproto_port_destroy(&ofproto_port); |
01c77073 BP |
2663 | |
2664 | return error; | |
064af421 BP |
2665 | } |
2666 | ||
2667 | static int | |
2668 | init_ports(struct ofproto *p) | |
2669 | { | |
abe529af BP |
2670 | struct ofproto_port_dump dump; |
2671 | struct ofproto_port ofproto_port; | |
e1b1d06a | 2672 | struct shash_node *node, *next; |
abe529af BP |
2673 | |
2674 | OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) { | |
e1b1d06a JP |
2675 | const char *name = ofproto_port.name; |
2676 | ||
2677 | if (shash_find(&p->port_by_name, name)) { | |
fbfa2911 | 2678 | VLOG_WARN_RL(&rl, "%s: ignoring duplicate device %s in datapath", |
e1b1d06a | 2679 | p->name, name); |
abe529af | 2680 | } else { |
9e1fd49b | 2681 | struct ofputil_phy_port pp; |
b33951b8 BP |
2682 | struct netdev *netdev; |
2683 | ||
e1b1d06a JP |
2684 | /* Check if an OpenFlow port number had been requested. */ |
2685 | node = shash_find(&init_ofp_ports, name); | |
2686 | if (node) { | |
2687 | const struct iface_hint *iface_hint = node->data; | |
4e022ec0 AW |
2688 | simap_put(&p->ofp_requests, name, |
2689 | ofp_to_u16(iface_hint->ofp_port)); | |
e1b1d06a JP |
2690 | } |
2691 | ||
fbfa2911 | 2692 | netdev = ofport_open(p, &ofproto_port, &pp); |
b33951b8 | 2693 | if (netdev) { |
9e1fd49b | 2694 | ofport_install(p, netdev, &pp); |
430dbb14 | 2695 | if (ofp_to_u16(ofproto_port.ofp_port) < p->max_ports) { |
7c35397c | 2696 | p->alloc_port_no = MAX(p->alloc_port_no, |
430dbb14 | 2697 | ofp_to_u16(ofproto_port.ofp_port)); |
7c35397c | 2698 | } |
064af421 BP |
2699 | } |
2700 | } | |
2701 | } | |
b0ec0f27 | 2702 | |
e1b1d06a | 2703 | SHASH_FOR_EACH_SAFE(node, next, &init_ofp_ports) { |
4f9e08a5 | 2704 | struct iface_hint *iface_hint = node->data; |
e1b1d06a JP |
2705 | |
2706 | if (!strcmp(iface_hint->br_name, p->name)) { | |
2707 | free(iface_hint->br_name); | |
2708 | free(iface_hint->br_type); | |
4f9e08a5 | 2709 | free(iface_hint); |
e1b1d06a JP |
2710 | shash_delete(&init_ofp_ports, node); |
2711 | } | |
2712 | } | |
2713 | ||
064af421 BP |
2714 | return 0; |
2715 | } | |
9197df76 | 2716 | |
3a414a0a | 2717 | static bool |
86fce577 | 2718 | ofport_is_internal(const struct ofproto *p, const struct ofport *port) |
0670b5d0 | 2719 | { |
86fce577 DDP |
2720 | return !strcmp(netdev_get_type(port->netdev), |
2721 | ofproto_port_open_type(p->type, "internal")); | |
0670b5d0 | 2722 | } |
2723 | ||
3a414a0a DDP |
2724 | /* If 'port' is internal and if the user didn't explicitly specify an mtu |
2725 | * through the database, we have to override it. */ | |
2726 | static bool | |
2727 | ofport_is_mtu_overridden(const struct ofproto *p, const struct ofport *port) | |
2728 | { | |
2729 | return ofport_is_internal(p, port) | |
2730 | && !netdev_mtu_is_user_config(port->netdev); | |
2731 | } | |
2732 | ||
2733 | /* Find the minimum MTU of all non-overridden devices attached to 'p'. | |
9197df76 JP |
2734 | * Returns ETH_PAYLOAD_MAX or the minimum of the ports. */ |
2735 | static int | |
2736 | find_min_mtu(struct ofproto *p) | |
2737 | { | |
2738 | struct ofport *ofport; | |
2739 | int mtu = 0; | |
2740 | ||
2741 | HMAP_FOR_EACH (ofport, hmap_node, &p->ports) { | |
2742 | struct netdev *netdev = ofport->netdev; | |
2743 | int dev_mtu; | |
2744 | ||
3a414a0a DDP |
2745 | /* Skip any overridden port, since that's what we're trying to set. */ |
2746 | if (ofport_is_mtu_overridden(p, ofport)) { | |
9197df76 JP |
2747 | continue; |
2748 | } | |
2749 | ||
2750 | if (netdev_get_mtu(netdev, &dev_mtu)) { | |
2751 | continue; | |
2752 | } | |
2753 | if (!mtu || dev_mtu < mtu) { | |
2754 | mtu = dev_mtu; | |
2755 | } | |
2756 | } | |
2757 | ||
2758 | return mtu ? mtu: ETH_PAYLOAD_MAX; | |
2759 | } | |
2760 | ||
3a414a0a DDP |
2761 | /* Update MTU of all overridden devices on 'p' to the minimum of the |
2762 | * non-overridden ports in event of 'port' added or changed. */ | |
9197df76 | 2763 | static void |
ada3428f | 2764 | update_mtu(struct ofproto *p, struct ofport *port) |
9197df76 | 2765 | { |
ada3428f | 2766 | struct netdev *netdev = port->netdev; |
0670b5d0 | 2767 | int dev_mtu; |
ada3428f PS |
2768 | |
2769 | if (netdev_get_mtu(netdev, &dev_mtu)) { | |
2770 | port->mtu = 0; | |
2771 | return; | |
2772 | } | |
3a414a0a | 2773 | if (ofport_is_mtu_overridden(p, port)) { |
7478b5a2 | 2774 | if (dev_mtu > p->min_mtu) { |
3a414a0a DDP |
2775 | if (!netdev_set_mtu(port->netdev, p->min_mtu)) { |
2776 | dev_mtu = p->min_mtu; | |
2777 | } | |
ada3428f PS |
2778 | } |
2779 | port->mtu = dev_mtu; | |
2780 | return; | |
2781 | } | |
2782 | ||
ada3428f | 2783 | port->mtu = dev_mtu; |
3a414a0a DDP |
2784 | /* For non-overridden port find new min mtu. */ |
2785 | ||
0670b5d0 | 2786 | update_mtu_ofproto(p); |
2787 | } | |
2788 | ||
2789 | static void | |
2790 | update_mtu_ofproto(struct ofproto *p) | |
2791 | { | |
0670b5d0 | 2792 | struct ofport *ofport; |
2793 | int old_min = p->min_mtu; | |
2794 | ||
ada3428f PS |
2795 | p->min_mtu = find_min_mtu(p); |
2796 | if (p->min_mtu == old_min) { | |
2797 | return; | |
2798 | } | |
9197df76 JP |
2799 | |
2800 | HMAP_FOR_EACH (ofport, hmap_node, &p->ports) { | |
2801 | struct netdev *netdev = ofport->netdev; | |
2802 | ||
3a414a0a | 2803 | if (ofport_is_mtu_overridden(p, ofport)) { |
ada3428f PS |
2804 | if (!netdev_set_mtu(netdev, p->min_mtu)) { |
2805 | ofport->mtu = p->min_mtu; | |
2806 | } | |
9197df76 JP |
2807 | } |
2808 | } | |
2809 | } | |
064af421 | 2810 | \f |
f416c8d6 JR |
2811 | static void |
2812 | ofproto_rule_destroy__(struct rule *rule) | |
2813 | OVS_NO_THREAD_SAFETY_ANALYSIS | |
2814 | { | |
2815 | cls_rule_destroy(CONST_CAST(struct cls_rule *, &rule->cr)); | |
2816 | rule_actions_destroy(rule_get_actions(rule)); | |
2817 | ovs_mutex_destroy(&rule->mutex); | |
2818 | rule->ofproto->ofproto_class->rule_dealloc(rule); | |
2819 | } | |
2820 | ||
2821 | static void | |
2822 | rule_destroy_cb(struct rule *rule) | |
f695ebfa | 2823 | OVS_NO_THREAD_SAFETY_ANALYSIS |
f416c8d6 | 2824 | { |
f695ebfa JR |
2825 | /* Send rule removed if needed. */ |
2826 | if (rule->flags & OFPUTIL_FF_SEND_FLOW_REM | |
2827 | && rule->removed_reason != OVS_OFPRR_NONE | |
2828 | && !rule_is_hidden(rule)) { | |
2829 | ofproto_rule_send_removed(rule); | |
2830 | } | |
f416c8d6 JR |
2831 | rule->ofproto->ofproto_class->rule_destruct(rule); |
2832 | ofproto_rule_destroy__(rule); | |
2833 | } | |
2834 | ||
a2143702 BP |
2835 | void |
2836 | ofproto_rule_ref(struct rule *rule) | |
064af421 | 2837 | { |
1eae3d33 | 2838 | if (rule) { |
37bec3d3 | 2839 | ovs_refcount_ref(&rule->ref_count); |
a2143702 BP |
2840 | } |
2841 | } | |
2842 | ||
f5d16e55 JR |
2843 | bool |
2844 | ofproto_rule_try_ref(struct rule *rule) | |
2845 | { | |
2846 | if (rule) { | |
2847 | return ovs_refcount_try_ref_rcu(&rule->ref_count); | |
2848 | } | |
2849 | return false; | |
2850 | } | |
2851 | ||
f416c8d6 JR |
2852 | /* Decrements 'rule''s ref_count and schedules 'rule' to be destroyed if the |
2853 | * ref_count reaches 0. | |
2854 | * | |
2855 | * Use of RCU allows short term use (between RCU quiescent periods) without | |
2856 | * keeping a reference. A reference must be taken if the rule needs to | |
2857 | * stay around accross the RCU quiescent periods. */ | |
a2143702 BP |
2858 | void |
2859 | ofproto_rule_unref(struct rule *rule) | |
2860 | { | |
24f83812 | 2861 | if (rule && ovs_refcount_unref_relaxed(&rule->ref_count) == 1) { |
2c7ee524 | 2862 | ovs_assert(rule->state != RULE_INSERTED); |
f416c8d6 | 2863 | ovsrcu_postpone(rule_destroy_cb, rule); |
1eae3d33 | 2864 | } |
064af421 BP |
2865 | } |
2866 | ||
39c94593 JR |
2867 | static void |
2868 | remove_rule_rcu__(struct rule *rule) | |
2869 | OVS_REQUIRES(ofproto_mutex) | |
2870 | { | |
2871 | struct ofproto *ofproto = rule->ofproto; | |
2872 | struct oftable *table = &ofproto->tables[rule->table_id]; | |
2873 | ||
44e0c35d | 2874 | ovs_assert(!cls_rule_visible_in_version(&rule->cr, OVS_VERSION_MAX)); |
39c94593 JR |
2875 | if (!classifier_remove(&table->cls, &rule->cr)) { |
2876 | OVS_NOT_REACHED(); | |
2877 | } | |
1fc71871 JR |
2878 | if (ofproto->ofproto_class->rule_delete) { |
2879 | ofproto->ofproto_class->rule_delete(rule); | |
2880 | } | |
39c94593 JR |
2881 | ofproto_rule_unref(rule); |
2882 | } | |
2883 | ||
2884 | static void | |
2885 | remove_rule_rcu(struct rule *rule) | |
2886 | OVS_EXCLUDED(ofproto_mutex) | |
2887 | { | |
2888 | ovs_mutex_lock(&ofproto_mutex); | |
2889 | remove_rule_rcu__(rule); | |
2890 | ovs_mutex_unlock(&ofproto_mutex); | |
2891 | } | |
2892 | ||
2893 | /* Removes and deletes rules from a NULL-terminated array of rule pointers. */ | |
2894 | static void | |
2895 | remove_rules_rcu(struct rule **rules) | |
2896 | OVS_EXCLUDED(ofproto_mutex) | |
2897 | { | |
2898 | struct rule **orig_rules = rules; | |
2899 | ||
2900 | if (*rules) { | |
2901 | struct ofproto *ofproto = rules[0]->ofproto; | |
2902 | unsigned long tables[BITMAP_N_LONGS(256)]; | |
2903 | struct rule *rule; | |
2904 | size_t table_id; | |
2905 | ||
2906 | memset(tables, 0, sizeof tables); | |
2907 | ||
2908 | ovs_mutex_lock(&ofproto_mutex); | |
2909 | while ((rule = *rules++)) { | |
2910 | /* Defer once for each new table. This defers the subtable cleanup | |
2911 | * until later, so that when removing large number of flows the | |
2912 | * operation is faster. */ | |
2913 | if (!bitmap_is_set(tables, rule->table_id)) { | |
2914 | struct classifier *cls = &ofproto->tables[rule->table_id].cls; | |
2915 | ||
2916 | bitmap_set1(tables, rule->table_id); | |
2917 | classifier_defer(cls); | |
2918 | } | |
2919 | remove_rule_rcu__(rule); | |
2920 | } | |
2921 | ||
2922 | BITMAP_FOR_EACH_1(table_id, 256, tables) { | |
2923 | struct classifier *cls = &ofproto->tables[table_id].cls; | |
2924 | ||
2925 | classifier_publish(cls); | |
2926 | } | |
2927 | ovs_mutex_unlock(&ofproto_mutex); | |
2928 | } | |
2929 | ||
2930 | free(orig_rules); | |
2931 | } | |
2932 | ||
809c7548 RW |
2933 | void |
2934 | ofproto_group_ref(struct ofgroup *group) | |
2935 | { | |
2936 | if (group) { | |
2937 | ovs_refcount_ref(&group->ref_count); | |
2938 | } | |
2939 | } | |
2940 | ||
db88b35c JR |
2941 | bool |
2942 | ofproto_group_try_ref(struct ofgroup *group) | |
2943 | { | |
2944 | if (group) { | |
2945 | return ovs_refcount_try_ref_rcu(&group->ref_count); | |
2946 | } | |
2947 | return false; | |
2948 | } | |
2949 | ||
2950 | static void | |
2951 | group_destroy_cb(struct ofgroup *group) | |
2952 | { | |
2953 | group->ofproto->ofproto_class->group_destruct(group); | |
e8dba719 JR |
2954 | ofputil_group_properties_destroy(CONST_CAST(struct ofputil_group_props *, |
2955 | &group->props)); | |
db88b35c JR |
2956 | ofputil_bucket_list_destroy(CONST_CAST(struct ovs_list *, |
2957 | &group->buckets)); | |
2958 | group->ofproto->ofproto_class->group_dealloc(group); | |
2959 | } | |
2960 | ||
809c7548 RW |
2961 | void |
2962 | ofproto_group_unref(struct ofgroup *group) | |
cc099268 | 2963 | OVS_NO_THREAD_SAFETY_ANALYSIS |
809c7548 | 2964 | { |
db88b35c | 2965 | if (group && ovs_refcount_unref_relaxed(&group->ref_count) == 1) { |
fe59694b | 2966 | ovs_assert(rule_collection_n(&group->rules) == 0); |
db88b35c | 2967 | ovsrcu_postpone(group_destroy_cb, group); |
809c7548 RW |
2968 | } |
2969 | } | |
2970 | ||
5d08a275 JR |
2971 | static void |
2972 | remove_group_rcu__(struct ofgroup *group) | |
2973 | OVS_REQUIRES(ofproto_mutex) | |
2974 | { | |
2975 | struct ofproto *ofproto = group->ofproto; | |
2976 | ||
2977 | ovs_assert(!versions_visible_in_version(&group->versions, OVS_VERSION_MAX)); | |
2978 | ||
2979 | cmap_remove(&ofproto->groups, &group->cmap_node, | |
2980 | hash_int(group->group_id, 0)); | |
2981 | ofproto_group_unref(group); | |
2982 | } | |
2983 | ||
2984 | static void | |
2985 | remove_group_rcu(struct ofgroup *group) | |
2986 | OVS_EXCLUDED(ofproto_mutex) | |
2987 | { | |
2988 | ovs_mutex_lock(&ofproto_mutex); | |
2989 | remove_group_rcu__(group); | |
2990 | ovs_mutex_unlock(&ofproto_mutex); | |
2991 | } | |
2992 | ||
2993 | /* Removes and deletes groups from a NULL-terminated array of group | |
2994 | * pointers. */ | |
2995 | static void | |
2996 | remove_groups_rcu(struct ofgroup **groups) | |
2997 | OVS_EXCLUDED(ofproto_mutex) | |
2998 | { | |
2999 | ovs_mutex_lock(&ofproto_mutex); | |
3000 | for (struct ofgroup **g = groups; *g; g++) { | |
3001 | remove_group_rcu__(*g); | |
3002 | } | |
3003 | ovs_mutex_unlock(&ofproto_mutex); | |
3004 | free(groups); | |
3005 | } | |
3006 | ||
076caa2f JR |
3007 | static bool ofproto_fix_meter_action(const struct ofproto *, |
3008 | struct ofpact_meter *); | |
65efd2ab | 3009 | |
dc723c44 JR |
3010 | /* Creates and returns a new 'struct rule_actions', whose actions are a copy |
3011 | * of from the 'ofpacts_len' bytes of 'ofpacts'. */ | |
3012 | const struct rule_actions * | |
4c7562c5 | 3013 | rule_actions_create(const struct ofpact *ofpacts, size_t ofpacts_len) |
6f00e29b BP |
3014 | { |
3015 | struct rule_actions *actions; | |
3016 | ||
dc723c44 | 3017 | actions = xmalloc(sizeof *actions + ofpacts_len); |
6f00e29b | 3018 | actions->ofpacts_len = ofpacts_len; |
dc723c44 | 3019 | memcpy(actions->ofpacts, ofpacts, ofpacts_len); |
cc099268 JR |
3020 | actions->has_meter = ofpacts_get_meter(ofpacts, ofpacts_len) != 0; |
3021 | actions->has_groups = | |
3022 | (ofpact_find_type_flattened(ofpacts, OFPACT_GROUP, | |
3023 | ofpact_end(ofpacts, ofpacts_len)) | |
3024 | != NULL); | |
35f48b8b BP |
3025 | actions->has_learn_with_delete = (next_learn_with_delete(actions, NULL) |
3026 | != NULL); | |
3027 | ||
6f00e29b BP |
3028 | return actions; |
3029 | } | |
3030 | ||
dc723c44 | 3031 | /* Free the actions after the RCU quiescent period is reached. */ |
6f00e29b | 3032 | void |
dc723c44 | 3033 | rule_actions_destroy(const struct rule_actions *actions) |
6f00e29b | 3034 | { |
06a0f3e2 | 3035 | if (actions) { |
dc723c44 | 3036 | ovsrcu_postpone(free, CONST_CAST(struct rule_actions *, actions)); |
6f00e29b BP |
3037 | } |
3038 | } | |
3039 | ||
bcf84111 | 3040 | /* Returns true if 'rule' has an OpenFlow OFPAT_OUTPUT or OFPAT_ENQUEUE action |
f25d0cf3 | 3041 | * that outputs to 'port' (output to OFPP_FLOOD and OFPP_ALL doesn't count). */ |
cdbdeeda | 3042 | bool |
4e022ec0 | 3043 | ofproto_rule_has_out_port(const struct rule *rule, ofp_port_t port) |
15aaf599 | 3044 | OVS_REQUIRES(ofproto_mutex) |
064af421 | 3045 | { |
06a0f3e2 BP |
3046 | if (port == OFPP_ANY) { |
3047 | return true; | |
3048 | } else { | |
3049 | const struct rule_actions *actions = rule_get_actions(rule); | |
3050 | return ofpacts_output_to_port(actions->ofpacts, | |
3051 | actions->ofpacts_len, port); | |
3052 | } | |
064af421 BP |
3053 | } |
3054 | ||
7395c052 | 3055 | /* Returns true if 'rule' has group and equals group_id. */ |
74e79b7c | 3056 | static bool |
7395c052 | 3057 | ofproto_rule_has_out_group(const struct rule *rule, uint32_t group_id) |
15aaf599 | 3058 | OVS_REQUIRES(ofproto_mutex) |
7395c052 | 3059 | { |
06a0f3e2 BP |
3060 | if (group_id == OFPG_ANY) { |
3061 | return true; | |
3062 | } else { | |
3063 | const struct rule_actions *actions = rule_get_actions(rule); | |
3064 | return ofpacts_output_to_group(actions->ofpacts, | |
3065 | actions->ofpacts_len, group_id); | |
3066 | } | |
7395c052 NZ |
3067 | } |
3068 | ||
adcf00ba | 3069 | static bool |
834fe5cb | 3070 | rule_is_readonly(const struct rule *rule) |
5c67e4af | 3071 | { |
834fe5cb BP |
3072 | const struct oftable *table = &rule->ofproto->tables[rule->table_id]; |
3073 | return (table->flags & OFTABLE_READONLY) != 0; | |
5c67e4af | 3074 | } |
fa066f01 | 3075 | \f |
35f48b8b BP |
3076 | static uint32_t |
3077 | hash_learned_cookie(ovs_be64 cookie_, uint8_t table_id) | |
3078 | { | |
3079 | uint64_t cookie = (OVS_FORCE uint64_t) cookie_; | |
3080 | return hash_3words(cookie, cookie >> 32, table_id); | |
3081 | } | |
3082 | ||
3083 | static void | |
3084 | learned_cookies_update_one__(struct ofproto *ofproto, | |
3085 | const struct ofpact_learn *learn, | |
ca6ba700 | 3086 | int delta, struct ovs_list *dead_cookies) |
35f48b8b BP |
3087 | OVS_REQUIRES(ofproto_mutex) |
3088 | { | |
3089 | uint32_t hash = hash_learned_cookie(learn->cookie, learn->table_id); | |
3090 | struct learned_cookie *c; | |
3091 | ||
3092 | HMAP_FOR_EACH_WITH_HASH (c, u.hmap_node, hash, &ofproto->learned_cookies) { | |
3093 | if (c->cookie == learn->cookie && c->table_id == learn->table_id) { | |
3094 | c->n += delta; | |
3095 | ovs_assert(c->n >= 0); | |
3096 | ||
3097 | if (!c->n) { | |
3098 | hmap_remove(&ofproto->learned_cookies, &c->u.hmap_node); | |
417e7e66 | 3099 | ovs_list_push_back(dead_cookies, &c->u.list_node); |
35f48b8b BP |
3100 | } |
3101 | ||
3102 | return; | |
3103 | } | |
3104 | } | |
3105 | ||
3106 | ovs_assert(delta > 0); | |
3107 | c = xmalloc(sizeof *c); | |
3108 | hmap_insert(&ofproto->learned_cookies, &c->u.hmap_node, hash); | |
3109 | c->cookie = learn->cookie; | |
3110 | c->table_id = learn->table_id; | |
3111 | c->n = delta; | |
3112 | } | |
3113 | ||
3114 | static const struct ofpact_learn * | |
3115 | next_learn_with_delete(const struct rule_actions *actions, | |
3116 | const struct ofpact_learn *start) | |
3117 | { | |
3118 | const struct ofpact *pos; | |
3119 | ||
3120 | for (pos = start ? ofpact_next(&start->ofpact) : actions->ofpacts; | |
3121 | pos < ofpact_end(actions->ofpacts, actions->ofpacts_len); | |
3122 | pos = ofpact_next(pos)) { | |
3123 | if (pos->type == OFPACT_LEARN) { | |
3124 | const struct ofpact_learn *learn = ofpact_get_LEARN(pos); | |
3125 | if (learn->flags & NX_LEARN_F_DELETE_LEARNED) { | |
3126 | return learn; | |
3127 | } | |
3128 | } | |
3129 | } | |
3130 | ||
3131 | return NULL; | |
3132 | } | |
3133 | ||
3134 | static void | |
3135 | learned_cookies_update__(struct ofproto *ofproto, | |
3136 | const struct rule_actions *actions, | |
ca6ba700 | 3137 | int delta, struct ovs_list *dead_cookies) |
35f48b8b BP |
3138 | OVS_REQUIRES(ofproto_mutex) |
3139 | { | |
3140 | if (actions->has_learn_with_delete) { | |
3141 | const struct ofpact_learn *learn; | |
3142 | ||
3143 | for (learn = next_learn_with_delete(actions, NULL); learn; | |
3144 | learn = next_learn_with_delete(actions, learn)) { | |
3145 | learned_cookies_update_one__(ofproto, learn, delta, dead_cookies); | |
3146 | } | |
3147 | } | |
3148 | } | |
3149 | ||
3150 | static void | |
3151 | learned_cookies_inc(struct ofproto *ofproto, | |
3152 | const struct rule_actions *actions) | |
3153 | OVS_REQUIRES(ofproto_mutex) | |
3154 | { | |
3155 | learned_cookies_update__(ofproto, actions, +1, NULL); | |
3156 | } | |
3157 | ||
3158 | static void | |
3159 | learned_cookies_dec(struct ofproto *ofproto, | |
3160 | const struct rule_actions *actions, | |
ca6ba700 | 3161 | struct ovs_list *dead_cookies) |
35f48b8b BP |
3162 | OVS_REQUIRES(ofproto_mutex) |
3163 | { | |
3164 | learned_cookies_update__(ofproto, actions, -1, dead_cookies); | |
3165 | } | |
3166 | ||
3167 | static void | |
ca6ba700 | 3168 | learned_cookies_flush(struct ofproto *ofproto, struct ovs_list *dead_cookies) |
35f48b8b BP |
3169 | OVS_REQUIRES(ofproto_mutex) |
3170 | { | |
5f03c983 | 3171 | struct learned_cookie *c; |
35f48b8b | 3172 | |
5f03c983 | 3173 | LIST_FOR_EACH_POP (c, u.list_node, dead_cookies) { |
35f48b8b BP |
3174 | struct rule_criteria criteria; |
3175 | struct rule_collection rules; | |
3176 | struct match match; | |
3177 | ||
3178 | match_init_catchall(&match); | |
44e0c35d | 3179 | rule_criteria_init(&criteria, c->table_id, &match, 0, OVS_VERSION_MAX, |
35f48b8b BP |
3180 | c->cookie, OVS_BE64_MAX, OFPP_ANY, OFPG_ANY); |
3181 | rule_criteria_require_rw(&criteria, false); | |
3182 | collect_rules_loose(ofproto, &criteria, &rules); | |
35f48b8b | 3183 | rule_criteria_destroy(&criteria); |
39c94593 | 3184 | delete_flows__(&rules, OFPRR_DELETE, NULL); |
35f48b8b | 3185 | |
35f48b8b BP |
3186 | free(c); |
3187 | } | |
3188 | } | |
3189 | \f | |
90bf1e07 | 3190 | static enum ofperr |
d1e2cf21 | 3191 | handle_echo_request(struct ofconn *ofconn, const struct ofp_header *oh) |
064af421 | 3192 | { |
b0421aa2 | 3193 | ofconn_send_reply(ofconn, make_echo_reply(oh)); |
064af421 | 3194 | return 0; |
064af421 BP |
3195 | } |
3196 | ||
3c1bb396 BP |
3197 | static void |
3198 | query_tables(struct ofproto *ofproto, | |
3199 | struct ofputil_table_features **featuresp, | |
3200 | struct ofputil_table_stats **statsp) | |
3201 | { | |
178742f9 BP |
3202 | struct mf_bitmap rw_fields = oxm_writable_fields(); |
3203 | struct mf_bitmap match = oxm_matchable_fields(); | |
3204 | struct mf_bitmap mask = oxm_maskable_fields(); | |
3c1bb396 BP |
3205 | |
3206 | struct ofputil_table_features *features; | |
3207 | struct ofputil_table_stats *stats; | |
3208 | int i; | |
3209 | ||
3c1bb396 BP |
3210 | features = *featuresp = xcalloc(ofproto->n_tables, sizeof *features); |
3211 | for (i = 0; i < ofproto->n_tables; i++) { | |
3212 | struct ofputil_table_features *f = &features[i]; | |
3213 | ||
3214 | f->table_id = i; | |
3215 | sprintf(f->name, "table%d", i); | |
3216 | f->metadata_match = OVS_BE64_MAX; | |
3217 | f->metadata_write = OVS_BE64_MAX; | |
afcea9ea | 3218 | atomic_read_relaxed(&ofproto->tables[i].miss_config, &f->miss_config); |
3c1bb396 BP |
3219 | f->max_entries = 1000000; |
3220 | ||
5dc7516b BP |
3221 | bool more_tables = false; |
3222 | for (int j = i + 1; j < ofproto->n_tables; j++) { | |
3223 | if (!(ofproto->tables[j].flags & OFTABLE_HIDDEN)) { | |
3224 | bitmap_set1(f->nonmiss.next, j); | |
3225 | more_tables = true; | |
3226 | } | |
3227 | } | |
3c1bb396 | 3228 | f->nonmiss.instructions = (1u << N_OVS_INSTRUCTIONS) - 1; |
5dc7516b | 3229 | if (!more_tables) { |
3c1bb396 BP |
3230 | f->nonmiss.instructions &= ~(1u << OVSINST_OFPIT11_GOTO_TABLE); |
3231 | } | |
3232 | f->nonmiss.write.ofpacts = (UINT64_C(1) << N_OFPACTS) - 1; | |
3233 | f->nonmiss.write.set_fields = rw_fields; | |
3234 | f->nonmiss.apply = f->nonmiss.write; | |
3235 | f->miss = f->nonmiss; | |
3236 | ||
3237 | f->match = match; | |
3238 | f->mask = mask; | |
3239 | f->wildcard = match; | |
3240 | } | |
3241 | ||
3242 | if (statsp) { | |
3243 | stats = *statsp = xcalloc(ofproto->n_tables, sizeof *stats); | |
3244 | for (i = 0; i < ofproto->n_tables; i++) { | |
3245 | struct ofputil_table_stats *s = &stats[i]; | |
3c1bb396 BP |
3246 | |
3247 | s->table_id = i; | |
d79e3d70 | 3248 | s->active_count = ofproto->tables[i].n_flows; |
3fbbcba7 BP |
3249 | if (i == 0) { |
3250 | s->active_count -= connmgr_count_hidden_rules( | |
3251 | ofproto->connmgr); | |
3252 | } | |
3c1bb396 BP |
3253 | } |
3254 | } else { | |
3255 | stats = NULL; | |
3256 | } | |
3257 | ||
3258 | ofproto->ofproto_class->query_tables(ofproto, features, stats); | |
3259 | ||
3260 | for (i = 0; i < ofproto->n_tables; i++) { | |
3261 | const struct oftable *table = &ofproto->tables[i]; | |
3262 | struct ofputil_table_features *f = &features[i]; | |
3263 | ||
3264 | if (table->name) { | |
3265 | ovs_strzcpy(f->name, table->name, sizeof f->name); | |
3266 | } | |
3267 | ||
3268 | if (table->max_flows < f->max_entries) { | |
3269 | f->max_entries = table->max_flows; | |
3270 | } | |
3271 | } | |
3272 | } | |
3273 | ||
3274 | static void | |
3275 | query_switch_features(struct ofproto *ofproto, | |
3276 | bool *arp_match_ip, uint64_t *ofpacts) | |
3277 | { | |
3278 | struct ofputil_table_features *features, *f; | |
3279 | ||
3280 | *arp_match_ip = false; | |
3281 | *ofpacts = 0; | |
3282 | ||
3283 | query_tables(ofproto, &features, NULL); | |
3284 | for (f = features; f < &features[ofproto->n_tables]; f++) { | |
3285 | *ofpacts |= f->nonmiss.apply.ofpacts | f->miss.apply.ofpacts; | |
3286 | if (bitmap_is_set(f->match.bm, MFF_ARP_SPA) || | |
3287 | bitmap_is_set(f->match.bm, MFF_ARP_TPA)) { | |
3288 | *arp_match_ip = true; | |
3289 | } | |
3290 | } | |
3291 | free(features); | |
3292 | ||
3293 | /* Sanity check. */ | |
3294 | ovs_assert(*ofpacts & (UINT64_C(1) << OFPACT_OUTPUT)); | |
3295 | } | |
3296 | ||
90bf1e07 | 3297 | static enum ofperr |
d1e2cf21 | 3298 | handle_features_request(struct ofconn *ofconn, const struct ofp_header *oh) |
064af421 | 3299 | { |
64ff1d96 | 3300 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
9e1fd49b | 3301 | struct ofputil_switch_features features; |
064af421 | 3302 | struct ofport *port; |
6c1491fb | 3303 | bool arp_match_ip; |
9e1fd49b | 3304 | struct ofpbuf *b; |
064af421 | 3305 | |
3c1bb396 | 3306 | query_switch_features(ofproto, &arp_match_ip, &features.ofpacts); |
064af421 | 3307 | |
9e1fd49b | 3308 | features.datapath_id = ofproto->datapath_id; |
c184807c | 3309 | features.n_buffers = 0; |
adcf00ba | 3310 | features.n_tables = ofproto_get_n_visible_tables(ofproto); |
9e1fd49b | 3311 | features.capabilities = (OFPUTIL_C_FLOW_STATS | OFPUTIL_C_TABLE_STATS | |
4efe455d | 3312 | OFPUTIL_C_PORT_STATS | OFPUTIL_C_QUEUE_STATS | |
50b73fe1 | 3313 | OFPUTIL_C_GROUP_STATS | OFPUTIL_C_BUNDLES); |
6c1491fb | 3314 | if (arp_match_ip) { |
9e1fd49b | 3315 | features.capabilities |= OFPUTIL_C_ARP_MATCH_IP; |
6c1491fb | 3316 | } |
2e1ae200 JR |
3317 | /* FIXME: Fill in proper features.auxiliary_id for auxiliary connections */ |
3318 | features.auxiliary_id = 0; | |
9e1fd49b BP |
3319 | b = ofputil_encode_switch_features(&features, ofconn_get_protocol(ofconn), |
3320 | oh->xid); | |
64ff1d96 | 3321 | HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) { |
9e1fd49b | 3322 | ofputil_put_switch_features_port(&port->pp, b); |
064af421 | 3323 | } |
064af421 | 3324 | |
9e1fd49b | 3325 | ofconn_send_reply(ofconn, b); |
064af421 BP |
3326 | return 0; |
3327 | } | |
064af421 | 3328 | |
90bf1e07 | 3329 | static enum ofperr |
d1e2cf21 | 3330 | handle_get_config_request(struct ofconn *ofconn, const struct ofp_header *oh) |
064af421 | 3331 | { |
ad99e2ed BP |
3332 | struct ofputil_switch_config config; |
3333 | config.frag = ofconn_get_ofproto(ofconn)->frag_handling; | |
3334 | config.invalid_ttl_to_controller | |
3335 | = ofconn_get_invalid_ttl_to_controller(ofconn); | |
3336 | config.miss_send_len = ofconn_get_miss_send_len(ofconn); | |
3337 | ||
3338 | ofconn_send_reply(ofconn, ofputil_encode_get_config_reply(oh, &config)); | |
2d70a31a | 3339 | |
064af421 BP |
3340 | return 0; |
3341 | } | |
064af421 | 3342 | |
90bf1e07 | 3343 | static enum ofperr |
982697a4 | 3344 | handle_set_config(struct ofconn *ofconn, const struct ofp_header *oh) |
064af421 | 3345 | { |
64ff1d96 | 3346 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
ad99e2ed BP |
3347 | struct ofputil_switch_config config; |
3348 | enum ofperr error; | |
3349 | ||
3350 | error = ofputil_decode_set_config(oh, &config); | |
3351 | if (error) { | |
3352 | return error; | |
3353 | } | |
2d70a31a | 3354 | |
7257b535 | 3355 | if (ofconn_get_type(ofconn) != OFCONN_PRIMARY |
f4f1ea7e | 3356 | || ofconn_get_role(ofconn) != OFPCR12_ROLE_SLAVE) { |
ad99e2ed BP |
3357 | enum ofputil_frag_handling cur = ofproto->frag_handling; |
3358 | enum ofputil_frag_handling next = config.frag; | |
7257b535 | 3359 | |
7257b535 BP |
3360 | if (cur != next) { |
3361 | if (ofproto->ofproto_class->set_frag_handling(ofproto, next)) { | |
3362 | ofproto->frag_handling = next; | |
3363 | } else { | |
3364 | VLOG_WARN_RL(&rl, "%s: unsupported fragment handling mode %s", | |
3365 | ofproto->name, | |
3366 | ofputil_frag_handling_to_string(next)); | |
3367 | } | |
064af421 BP |
3368 | } |
3369 | } | |
ebe482fd | 3370 | |
ad99e2ed BP |
3371 | if (config.invalid_ttl_to_controller >= 0) { |
3372 | ofconn_set_invalid_ttl_to_controller(ofconn, | |
3373 | config.invalid_ttl_to_controller); | |
3374 | } | |
3375 | ||
3376 | ofconn_set_miss_send_len(ofconn, config.miss_send_len); | |
0ad9b732 | 3377 | |
064af421 | 3378 | return 0; |
064af421 BP |
3379 | } |
3380 | ||
9deba63b | 3381 | /* Checks whether 'ofconn' is a slave controller. If so, returns an OpenFlow |
90bf1e07 BP |
3382 | * error message code for the caller to propagate upward. Otherwise, returns |
3383 | * 0. | |
3384 | * | |
3385 | * The log message mentions 'msg_type'. */ | |
3386 | static enum ofperr | |
3387 | reject_slave_controller(struct ofconn *ofconn) | |
9deba63b | 3388 | { |
1ce0a5fa | 3389 | if (ofconn_get_type(ofconn) == OFCONN_PRIMARY |
f4f1ea7e | 3390 | && ofconn_get_role(ofconn) == OFPCR12_ROLE_SLAVE) { |
20e4ec1a | 3391 | return OFPERR_OFPBRC_IS_SLAVE; |
9deba63b BP |
3392 | } else { |
3393 | return 0; | |
3394 | } | |
3395 | } | |
3396 | ||
7e9f8266 BP |
3397 | /* Checks that the 'ofpacts_len' bytes of action in 'ofpacts' are appropriate |
3398 | * for 'ofproto': | |
3399 | * | |
3400 | * - If they use a meter, then 'ofproto' has that meter configured. | |
076caa2f | 3401 | * Updates the meter action with ofproto's datapath's provider_meter_id. |
7e9f8266 BP |
3402 | * |
3403 | * - If they use any groups, then 'ofproto' has that group configured. | |
3404 | * | |
1f4a8933 JR |
3405 | * Returns 0 if successful, otherwise an OpenFlow error. Caller must hold |
3406 | * 'ofproto_mutex' for the result to be valid also after this function | |
3407 | * returns. */ | |
89108874 | 3408 | enum ofperr |
9cae45dc | 3409 | ofproto_check_ofpacts(struct ofproto *ofproto, |
7e9f8266 | 3410 | const struct ofpact ofpacts[], size_t ofpacts_len) |
1f4a8933 | 3411 | OVS_REQUIRES(ofproto_mutex) |
9cae45dc | 3412 | { |
076caa2f | 3413 | const struct ofpact *a; |
9cae45dc | 3414 | |
076caa2f JR |
3415 | OFPACT_FOR_EACH_FLATTENED (a, ofpacts, ofpacts_len) { |
3416 | if (a->type == OFPACT_METER && | |
3417 | !ofproto_fix_meter_action(ofproto, ofpact_get_METER(a))) { | |
3418 | return OFPERR_OFPMMFC_INVALID_METER; | |
3419 | } | |
9cae45dc | 3420 | |
076caa2f JR |
3421 | if (a->type == OFPACT_GROUP |
3422 | && !ofproto_group_exists(ofproto, ofpact_get_GROUP(a)->group_id)) { | |
7e9f8266 | 3423 | return OFPERR_OFPBAC_BAD_OUT_GROUP; |
84d68566 SH |
3424 | } |
3425 | } | |
3426 | ||
9cae45dc JR |
3427 | return 0; |
3428 | } | |
3429 | ||
1f4a8933 JR |
3430 | void |
3431 | ofproto_packet_out_uninit(struct ofproto_packet_out *opo) | |
3432 | { | |
3433 | dp_packet_delete(opo->packet); | |
3434 | opo->packet = NULL; | |
3435 | free(opo->flow); | |
3436 | opo->flow = NULL; | |
3437 | free(opo->ofpacts); | |
3438 | opo->ofpacts = NULL; | |
3439 | opo->ofpacts_len = 0; | |
3440 | ovs_assert(!opo->aux); | |
3441 | } | |
3442 | ||
3443 | /* Takes ownership of po->ofpacts, which must have been malloc'ed. */ | |
3444 | static enum ofperr | |
3445 | ofproto_packet_out_init(struct ofproto *ofproto, | |
3446 | struct ofconn *ofconn, | |
3447 | struct ofproto_packet_out *opo, | |
3448 | const struct ofputil_packet_out *po) | |
3449 | { | |
3450 | enum ofperr error; | |
67210a55 | 3451 | struct match match; |
1f4a8933 JR |
3452 | |
3453 | if (ofp_to_u16(po->in_port) >= ofproto->max_ports | |
3454 | && ofp_to_u16(po->in_port) < ofp_to_u16(OFPP_MAX)) { | |
3455 | return OFPERR_OFPBRC_BAD_PORT; | |
3456 | } | |
3457 | ||
3458 | /* Get payload. */ | |
3459 | if (po->buffer_id != UINT32_MAX) { | |
3460 | return OFPERR_OFPBRC_BUFFER_UNKNOWN; | |
3461 | } | |
3462 | ||
3463 | /* Ensure that the L3 header is 32-bit aligned. */ | |
3464 | opo->packet = dp_packet_clone_data_with_headroom(po->packet, | |
3465 | po->packet_len, 2); | |
3466 | /* Store struct flow. */ | |
3467 | opo->flow = xmalloc(sizeof *opo->flow); | |
3468 | flow_extract(opo->packet, opo->flow); | |
3469 | opo->flow->in_port.ofp_port = po->in_port; | |
3470 | ||
3471 | /* Check actions like for flow mods. We pass a 'table_id' of 0 to | |
3472 | * ofproto_check_consistency(), which isn't strictly correct because these | |
3473 | * actions aren't in any table. This is OK as 'table_id' is only used to | |
3474 | * check instructions (e.g., goto-table), which can't appear on the action | |
3475 | * list of a packet-out. */ | |
67210a55 JR |
3476 | match_wc_init(&match, opo->flow); |
3477 | error = ofpacts_check_consistency(po->ofpacts, po->ofpacts_len, &match, | |
1f4a8933 JR |
3478 | u16_to_ofp(ofproto->max_ports), 0, |
3479 | ofproto->n_tables, | |
3480 | ofconn_get_protocol(ofconn)); | |
3481 | if (error) { | |
3482 | dp_packet_delete(opo->packet); | |
3483 | free(opo->flow); | |
3484 | return error; | |
3485 | } | |
3486 | ||
3487 | opo->ofpacts = po->ofpacts; | |
3488 | opo->ofpacts_len = po->ofpacts_len; | |
3489 | ||
3490 | opo->aux = NULL; | |
3491 | return 0; | |
3492 | } | |
3493 | ||
3494 | static enum ofperr | |
3495 | ofproto_packet_out_start(struct ofproto *ofproto, | |
3496 | struct ofproto_packet_out *opo) | |
3497 | OVS_REQUIRES(ofproto_mutex) | |
3498 | { | |
3499 | enum ofperr error; | |
3500 | ||
3501 | error = ofproto_check_ofpacts(ofproto, opo->ofpacts, opo->ofpacts_len); | |
3502 | if (error) { | |
3503 | return error; | |
3504 | } | |
3505 | ||
3506 | return ofproto->ofproto_class->packet_xlate(ofproto, opo); | |
3507 | } | |
3508 | ||
6dd3c787 JR |
3509 | static void |
3510 | ofproto_packet_out_revert(struct ofproto *ofproto, | |
3511 | struct ofproto_packet_out *opo) | |
3512 | OVS_REQUIRES(ofproto_mutex) | |
3513 | { | |
3514 | ofproto->ofproto_class->packet_xlate_revert(ofproto, opo); | |
3515 | } | |
3516 | ||
1f4a8933 JR |
3517 | static void |
3518 | ofproto_packet_out_finish(struct ofproto *ofproto, | |
3519 | struct ofproto_packet_out *opo) | |
3520 | OVS_REQUIRES(ofproto_mutex) | |
3521 | { | |
3522 | ofproto->ofproto_class->packet_execute(ofproto, opo); | |
3523 | } | |
3524 | ||
90bf1e07 | 3525 | static enum ofperr |
982697a4 | 3526 | handle_packet_out(struct ofconn *ofconn, const struct ofp_header *oh) |
1f4a8933 | 3527 | OVS_EXCLUDED(ofproto_mutex) |
064af421 | 3528 | { |
64ff1d96 | 3529 | struct ofproto *p = ofconn_get_ofproto(ofconn); |
c6a93eb7 | 3530 | struct ofputil_packet_out po; |
1f4a8933 | 3531 | struct ofproto_packet_out opo; |
f25d0cf3 BP |
3532 | uint64_t ofpacts_stub[1024 / 8]; |
3533 | struct ofpbuf ofpacts; | |
90bf1e07 | 3534 | enum ofperr error; |
064af421 | 3535 | |
ac51afaf BP |
3536 | COVERAGE_INC(ofproto_packet_out); |
3537 | ||
76589937 | 3538 | error = reject_slave_controller(ofconn); |
9deba63b | 3539 | if (error) { |
1f4a8933 | 3540 | return error; |
9deba63b BP |
3541 | } |
3542 | ||
c6a93eb7 | 3543 | /* Decode message. */ |
f25d0cf3 | 3544 | ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub); |
982697a4 | 3545 | error = ofputil_decode_packet_out(&po, oh, &ofpacts); |
064af421 | 3546 | if (error) { |
1f4a8933 JR |
3547 | ofpbuf_uninit(&ofpacts); |
3548 | return error; | |
91858960 | 3549 | } |
064af421 | 3550 | |
1f4a8933 JR |
3551 | po.ofpacts = ofpbuf_steal_data(&ofpacts); /* Move to heap. */ |
3552 | error = ofproto_packet_out_init(p, ofconn, &opo, &po); | |
3553 | if (error) { | |
3554 | free(po.ofpacts); | |
3555 | return error; | |
e1154f71 | 3556 | } |
89108874 | 3557 | |
1f4a8933 JR |
3558 | ovs_mutex_lock(&ofproto_mutex); |
3559 | opo.version = p->tables_version; | |
3560 | error = ofproto_packet_out_start(p, &opo); | |
548de4dd | 3561 | if (!error) { |
1f4a8933 | 3562 | ofproto_packet_out_finish(p, &opo); |
548de4dd | 3563 | } |
1f4a8933 | 3564 | ovs_mutex_unlock(&ofproto_mutex); |
abe529af | 3565 | |
1f4a8933 | 3566 | ofproto_packet_out_uninit(&opo); |
abe529af | 3567 | return error; |
064af421 BP |
3568 | } |
3569 | ||
77ab5fd2 BP |
3570 | static enum ofperr |
3571 | handle_nxt_resume(struct ofconn *ofconn, const struct ofp_header *oh) | |
3572 | { | |
3573 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
3574 | struct ofputil_packet_in_private pin; | |
3575 | enum ofperr error; | |
3576 | ||
8d8ab6c2 JG |
3577 | error = ofputil_decode_packet_in_private(oh, false, |
3578 | ofproto_get_tun_tab(ofproto), &pin, | |
3579 | NULL, NULL); | |
77ab5fd2 BP |
3580 | if (error) { |
3581 | return error; | |
3582 | } | |
3583 | ||
3584 | error = (ofproto->ofproto_class->nxt_resume | |
3585 | ? ofproto->ofproto_class->nxt_resume(ofproto, &pin) | |
3586 | : OFPERR_NXR_NOT_SUPPORTED); | |
3587 | ||
3588 | ofputil_packet_in_private_destroy(&pin); | |
3589 | ||
3590 | return error; | |
3591 | } | |
3592 | ||
064af421 | 3593 | static void |
2a6f78e0 | 3594 | update_port_config(struct ofconn *ofconn, struct ofport *port, |
9e1fd49b BP |
3595 | enum ofputil_port_config config, |
3596 | enum ofputil_port_config mask) | |
064af421 | 3597 | { |
2a6f78e0 | 3598 | enum ofputil_port_config toggle = (config ^ port->pp.config) & mask; |
abe529af | 3599 | |
2a6f78e0 BP |
3600 | if (toggle & OFPUTIL_PC_PORT_DOWN |
3601 | && (config & OFPUTIL_PC_PORT_DOWN | |
3602 | ? netdev_turn_flags_off(port->netdev, NETDEV_UP, NULL) | |
3603 | : netdev_turn_flags_on(port->netdev, NETDEV_UP, NULL))) { | |
3604 | /* We tried to bring the port up or down, but it failed, so don't | |
3605 | * update the "down" bit. */ | |
9e1fd49b | 3606 | toggle &= ~OFPUTIL_PC_PORT_DOWN; |
064af421 | 3607 | } |
abe529af | 3608 | |
2a6f78e0 BP |
3609 | if (toggle) { |
3610 | enum ofputil_port_config old_config = port->pp.config; | |
3611 | port->pp.config ^= toggle; | |
abe529af | 3612 | port->ofproto->ofproto_class->port_reconfigured(port, old_config); |
2a6f78e0 BP |
3613 | connmgr_send_port_status(port->ofproto->connmgr, ofconn, &port->pp, |
3614 | OFPPR_MODIFY); | |
064af421 BP |
3615 | } |
3616 | } | |
3617 | ||
90bf1e07 | 3618 | static enum ofperr |
1c38055d JR |
3619 | port_mod_start(struct ofconn *ofconn, struct ofputil_port_mod *pm, |
3620 | struct ofport **port) | |
064af421 | 3621 | { |
64ff1d96 | 3622 | struct ofproto *p = ofconn_get_ofproto(ofconn); |
1c38055d JR |
3623 | |
3624 | *port = ofproto_get_port(p, pm->port_no); | |
3625 | if (!*port) { | |
3626 | return OFPERR_OFPPMFC_BAD_PORT; | |
3627 | } | |
3628 | if (!eth_addr_equals((*port)->pp.hw_addr, pm->hw_addr)) { | |
3629 | return OFPERR_OFPPMFC_BAD_HW_ADDR; | |
3630 | } | |
3631 | return 0; | |
3632 | } | |
3633 | ||
3634 | static void | |
3635 | port_mod_finish(struct ofconn *ofconn, struct ofputil_port_mod *pm, | |
3636 | struct ofport *port) | |
3637 | { | |
3638 | update_port_config(ofconn, port, pm->config, pm->mask); | |
3639 | if (pm->advertise) { | |
3640 | netdev_set_advertisements(port->netdev, pm->advertise); | |
3641 | } | |
3642 | } | |
3643 | ||
3644 | static enum ofperr | |
3645 | handle_port_mod(struct ofconn *ofconn, const struct ofp_header *oh) | |
3646 | { | |
9e1fd49b | 3647 | struct ofputil_port_mod pm; |
064af421 | 3648 | struct ofport *port; |
9e1fd49b | 3649 | enum ofperr error; |
064af421 | 3650 | |
76589937 | 3651 | error = reject_slave_controller(ofconn); |
9deba63b BP |
3652 | if (error) { |
3653 | return error; | |
3654 | } | |
064af421 | 3655 | |
18cc69d9 | 3656 | error = ofputil_decode_port_mod(oh, &pm, false); |
9e1fd49b BP |
3657 | if (error) { |
3658 | return error; | |
3659 | } | |
3660 | ||
1c38055d JR |
3661 | error = port_mod_start(ofconn, &pm, &port); |
3662 | if (!error) { | |
3663 | port_mod_finish(ofconn, &pm, port); | |
064af421 | 3664 | } |
1c38055d | 3665 | return error; |
064af421 BP |
3666 | } |
3667 | ||
90bf1e07 | 3668 | static enum ofperr |
3269c562 | 3669 | handle_desc_stats_request(struct ofconn *ofconn, |
982697a4 | 3670 | const struct ofp_header *request) |
064af421 | 3671 | { |
061bfea4 BP |
3672 | static const char *default_mfr_desc = "Nicira, Inc."; |
3673 | static const char *default_hw_desc = "Open vSwitch"; | |
3674 | static const char *default_sw_desc = VERSION; | |
3675 | static const char *default_serial_desc = "None"; | |
3676 | static const char *default_dp_desc = "None"; | |
3677 | ||
64ff1d96 | 3678 | struct ofproto *p = ofconn_get_ofproto(ofconn); |
064af421 BP |
3679 | struct ofp_desc_stats *ods; |
3680 | struct ofpbuf *msg; | |
3681 | ||
982697a4 BP |
3682 | msg = ofpraw_alloc_stats_reply(request, 0); |
3683 | ods = ofpbuf_put_zeros(msg, sizeof *ods); | |
061bfea4 BP |
3684 | ovs_strlcpy(ods->mfr_desc, p->mfr_desc ? p->mfr_desc : default_mfr_desc, |
3685 | sizeof ods->mfr_desc); | |
3686 | ovs_strlcpy(ods->hw_desc, p->hw_desc ? p->hw_desc : default_hw_desc, | |
3687 | sizeof ods->hw_desc); | |
3688 | ovs_strlcpy(ods->sw_desc, p->sw_desc ? p->sw_desc : default_sw_desc, | |
3689 | sizeof ods->sw_desc); | |
3690 | ovs_strlcpy(ods->serial_num, | |
3691 | p->serial_desc ? p->serial_desc : default_serial_desc, | |
3692 | sizeof ods->serial_num); | |
3693 | ovs_strlcpy(ods->dp_desc, p->dp_desc ? p->dp_desc : default_dp_desc, | |
3694 | sizeof ods->dp_desc); | |
b0421aa2 | 3695 | ofconn_send_reply(ofconn, msg); |
064af421 BP |
3696 | |
3697 | return 0; | |
3698 | } | |
3699 | ||
90bf1e07 | 3700 | static enum ofperr |
3269c562 | 3701 | handle_table_stats_request(struct ofconn *ofconn, |
982697a4 | 3702 | const struct ofp_header *request) |
064af421 | 3703 | { |
3c1bb396 BP |
3704 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
3705 | struct ofputil_table_features *features; | |
08d1e234 | 3706 | struct ofputil_table_stats *stats; |
3c1bb396 | 3707 | struct ofpbuf *reply; |
6c1491fb | 3708 | size_t i; |
064af421 | 3709 | |
3c1bb396 | 3710 | query_tables(ofproto, &features, &stats); |
254750ce | 3711 | |
3c1bb396 BP |
3712 | reply = ofputil_encode_table_stats_reply(request); |
3713 | for (i = 0; i < ofproto->n_tables; i++) { | |
3714 | if (!(ofproto->tables[i].flags & OFTABLE_HIDDEN)) { | |
3715 | ofputil_append_table_stats_reply(reply, &stats[i], &features[i]); | |
254750ce BP |
3716 | } |
3717 | } | |
3c1bb396 | 3718 | ofconn_send_reply(ofconn, reply); |
254750ce | 3719 | |
3c1bb396 | 3720 | free(features); |
08d1e234 | 3721 | free(stats); |
307975da | 3722 | |
064af421 BP |
3723 | return 0; |
3724 | } | |
3725 | ||
3c4e10fb BP |
3726 | static enum ofperr |
3727 | handle_table_features_request(struct ofconn *ofconn, | |
3728 | const struct ofp_header *request) | |
3729 | { | |
3730 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
0a2869d5 BP |
3731 | struct ofpbuf msg = ofpbuf_const_initializer(request, |
3732 | ntohs(request->length)); | |
3c4e10fb | 3733 | ofpraw_pull_assert(&msg); |
6fd6ed71 | 3734 | if (msg.size || ofpmp_more(request)) { |
3c4e10fb BP |
3735 | return OFPERR_OFPTFFC_EPERM; |
3736 | } | |
3737 | ||
0a2869d5 | 3738 | struct ofputil_table_features *features; |
3c4e10fb BP |
3739 | query_tables(ofproto, &features, NULL); |
3740 | ||
0a2869d5 | 3741 | struct ovs_list replies; |
3c4e10fb | 3742 | ofpmp_init(&replies, request); |
0a2869d5 | 3743 | for (size_t i = 0; i < ofproto->n_tables; i++) { |
3c4e10fb BP |
3744 | if (!(ofproto->tables[i].flags & OFTABLE_HIDDEN)) { |
3745 | ofputil_append_table_features_reply(&features[i], &replies); | |
3746 | } | |
3747 | } | |
3748 | ofconn_send_replies(ofconn, &replies); | |
3749 | ||
3750 | free(features); | |
3751 | ||
3752 | return 0; | |
3753 | } | |
3754 | ||
6c6eedc5 SJ |
3755 | /* Returns the vacancy of 'oftable', a number that ranges from 0 (if the table |
3756 | * is full) to 100 (if the table is empty). | |
3757 | * | |
3758 | * A table without a limit on flows is considered to be empty. */ | |
3759 | static uint8_t | |
3760 | oftable_vacancy(const struct oftable *t) | |
3761 | { | |
3762 | return (!t->max_flows ? 100 | |
3763 | : t->n_flows >= t->max_flows ? 0 | |
3764 | : (t->max_flows - t->n_flows) * 100.0 / t->max_flows); | |
3765 | } | |
3766 | ||
bab86012 SJ |
3767 | static void |
3768 | query_table_desc__(struct ofputil_table_desc *td, | |
3769 | struct ofproto *ofproto, uint8_t table_id) | |
3770 | { | |
6c6eedc5 | 3771 | const struct oftable *t = &ofproto->tables[table_id]; |
bab86012 SJ |
3772 | |
3773 | td->table_id = table_id; | |
6c6eedc5 | 3774 | td->eviction = (t->eviction & EVICTION_OPENFLOW |
bab86012 SJ |
3775 | ? OFPUTIL_TABLE_EVICTION_ON |
3776 | : OFPUTIL_TABLE_EVICTION_OFF); | |
3777 | td->eviction_flags = OFPROTO_EVICTION_FLAGS; | |
6c6eedc5 | 3778 | td->vacancy = (t->vacancy_event |
bab86012 SJ |
3779 | ? OFPUTIL_TABLE_VACANCY_ON |
3780 | : OFPUTIL_TABLE_VACANCY_OFF); | |
6c6eedc5 SJ |
3781 | td->table_vacancy.vacancy_down = t->vacancy_down; |
3782 | td->table_vacancy.vacancy_up = t->vacancy_up; | |
3783 | td->table_vacancy.vacancy = oftable_vacancy(t); | |
bab86012 SJ |
3784 | } |
3785 | ||
03c72922 BP |
3786 | /* This function queries the database for dumping table-desc. */ |
3787 | static void | |
3788 | query_tables_desc(struct ofproto *ofproto, struct ofputil_table_desc **descp) | |
3789 | { | |
3790 | struct ofputil_table_desc *table_desc; | |
3791 | size_t i; | |
3792 | ||
3793 | table_desc = *descp = xcalloc(ofproto->n_tables, sizeof *table_desc); | |
3794 | for (i = 0; i < ofproto->n_tables; i++) { | |
3795 | struct ofputil_table_desc *td = &table_desc[i]; | |
bab86012 | 3796 | query_table_desc__(td, ofproto, i); |
03c72922 BP |
3797 | } |
3798 | } | |
3799 | ||
3800 | /* Function to handle dump-table-desc request. */ | |
3801 | static enum ofperr | |
3802 | handle_table_desc_request(struct ofconn *ofconn, | |
3803 | const struct ofp_header *request) | |
3804 | { | |
3805 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
3806 | struct ofputil_table_desc *table_desc; | |
3807 | struct ovs_list replies; | |
3808 | size_t i; | |
3809 | ||
3810 | query_tables_desc(ofproto, &table_desc); | |
3811 | ofpmp_init(&replies, request); | |
3812 | for (i = 0; i < ofproto->n_tables; i++) { | |
3813 | if (!(ofproto->tables[i].flags & OFTABLE_HIDDEN)) { | |
3814 | ofputil_append_table_desc_reply(&table_desc[i], &replies, | |
3815 | request->version); | |
3816 | } | |
3817 | } | |
3818 | ofconn_send_replies(ofconn, &replies); | |
3819 | free(table_desc); | |
3820 | return 0; | |
3821 | } | |
3822 | ||
6c6eedc5 SJ |
3823 | /* This function determines and sends the vacancy event, based on the value |
3824 | * of current vacancy and threshold vacancy. If the current vacancy is less | |
3825 | * than or equal to vacancy_down, vacancy up events must be enabled, and when | |
3826 | * the current vacancy is greater or equal to vacancy_up, vacancy down events | |
3827 | * must be enabled. */ | |
3828 | static void | |
3829 | send_table_status(struct ofproto *ofproto, uint8_t table_id) | |
3830 | { | |
3831 | struct oftable *t = &ofproto->tables[table_id]; | |
3832 | if (!t->vacancy_event) { | |
3833 | return; | |
3834 | } | |
3835 | ||
3836 | uint8_t vacancy = oftable_vacancy(t); | |
3837 | enum ofp14_table_reason event; | |
3838 | if (vacancy < t->vacancy_down) { | |
3839 | event = OFPTR_VACANCY_DOWN; | |
3840 | } else if (vacancy > t->vacancy_up) { | |
3841 | event = OFPTR_VACANCY_UP; | |
3842 | } else { | |
3843 | return; | |
3844 | } | |
3845 | ||
3846 | if (event == t->vacancy_event) { | |
3847 | struct ofputil_table_desc td; | |
3848 | query_table_desc__(&td, ofproto, table_id); | |
3849 | connmgr_send_table_status(ofproto->connmgr, &td, event); | |
3850 | ||
3851 | t->vacancy_event = (event == OFPTR_VACANCY_DOWN | |
3852 | ? OFPTR_VACANCY_UP | |
3853 | : OFPTR_VACANCY_DOWN); | |
3854 | } | |
3855 | } | |
3856 | ||
abaad8cf | 3857 | static void |
ca6ba700 | 3858 | append_port_stat(struct ofport *port, struct ovs_list *replies) |
abaad8cf | 3859 | { |
f8e4867e | 3860 | struct ofputil_port_stats ops = { .port_no = port->pp.port_no }; |
abaad8cf | 3861 | |
65e0be10 BP |
3862 | calc_duration(port->created, time_msec(), |
3863 | &ops.duration_sec, &ops.duration_nsec); | |
3864 | ||
d295e8e9 JP |
3865 | /* Intentionally ignore return value, since errors will set |
3866 | * 'stats' to all-1s, which is correct for OpenFlow, and | |
abaad8cf | 3867 | * netdev_get_stats() will log errors. */ |
f8e4867e SH |
3868 | ofproto_port_get_stats(port, &ops.stats); |
3869 | ||
3870 | ofputil_append_port_stat(replies, &ops); | |
abaad8cf JP |
3871 | } |
3872 | ||
70ae4f93 BP |
3873 | static void |
3874 | handle_port_request(struct ofconn *ofconn, | |
3875 | const struct ofp_header *request, ofp_port_t port_no, | |
ca6ba700 | 3876 | void (*cb)(struct ofport *, struct ovs_list *replies)) |
064af421 | 3877 | { |
70ae4f93 | 3878 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
064af421 | 3879 | struct ofport *port; |
ca6ba700 | 3880 | struct ovs_list replies; |
064af421 | 3881 | |
982697a4 | 3882 | ofpmp_init(&replies, request); |
7f05e7ab | 3883 | if (port_no != OFPP_ANY) { |
70ae4f93 | 3884 | port = ofproto_get_port(ofproto, port_no); |
abaad8cf | 3885 | if (port) { |
70ae4f93 | 3886 | cb(port, &replies); |
abaad8cf JP |
3887 | } |
3888 | } else { | |
70ae4f93 BP |
3889 | HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) { |
3890 | cb(port, &replies); | |
abaad8cf | 3891 | } |
064af421 BP |
3892 | } |
3893 | ||
63f2140a | 3894 | ofconn_send_replies(ofconn, &replies); |
70ae4f93 BP |
3895 | } |
3896 | ||
3897 | static enum ofperr | |
3898 | handle_port_stats_request(struct ofconn *ofconn, | |
3899 | const struct ofp_header *request) | |
3900 | { | |
3901 | ofp_port_t port_no; | |
3902 | enum ofperr error; | |
3903 | ||
3904 | error = ofputil_decode_port_stats_request(request, &port_no); | |
3905 | if (!error) { | |
3906 | handle_port_request(ofconn, request, port_no, append_port_stat); | |
3907 | } | |
3908 | return error; | |
3909 | } | |
3910 | ||
3911 | static void | |
ca6ba700 | 3912 | append_port_desc(struct ofport *port, struct ovs_list *replies) |
70ae4f93 BP |
3913 | { |
3914 | ofputil_append_port_desc_stats_reply(&port->pp, replies); | |
064af421 BP |
3915 | } |
3916 | ||
2be393ed JP |
3917 | static enum ofperr |
3918 | handle_port_desc_stats_request(struct ofconn *ofconn, | |
982697a4 | 3919 | const struct ofp_header *request) |
2be393ed | 3920 | { |
70ae4f93 BP |
3921 | ofp_port_t port_no; |
3922 | enum ofperr error; | |
2be393ed | 3923 | |
70ae4f93 BP |
3924 | error = ofputil_decode_port_desc_stats_request(request, &port_no); |
3925 | if (!error) { | |
3926 | handle_port_request(ofconn, request, port_no, append_port_desc); | |
2be393ed | 3927 | } |
70ae4f93 | 3928 | return error; |
2be393ed JP |
3929 | } |
3930 | ||
98eaac36 JR |
3931 | static uint32_t |
3932 | hash_cookie(ovs_be64 cookie) | |
3933 | { | |
965607c8 | 3934 | return hash_uint64((OVS_FORCE uint64_t)cookie); |
98eaac36 JR |
3935 | } |
3936 | ||
3937 | static void | |
3938 | cookies_insert(struct ofproto *ofproto, struct rule *rule) | |
2c916028 | 3939 | OVS_REQUIRES(ofproto_mutex) |
98eaac36 JR |
3940 | { |
3941 | hindex_insert(&ofproto->cookies, &rule->cookie_node, | |
3942 | hash_cookie(rule->flow_cookie)); | |
3943 | } | |
3944 | ||
3945 | static void | |
3946 | cookies_remove(struct ofproto *ofproto, struct rule *rule) | |
2c916028 | 3947 | OVS_REQUIRES(ofproto_mutex) |
98eaac36 JR |
3948 | { |
3949 | hindex_remove(&ofproto->cookies, &rule->cookie_node); | |
3950 | } | |
3951 | ||
c6ebb8fb | 3952 | static void |
65e0be10 BP |
3953 | calc_duration(long long int start, long long int now, |
3954 | uint32_t *sec, uint32_t *nsec) | |
c6ebb8fb | 3955 | { |
f27f2134 | 3956 | long long int msecs = now - start; |
588cd7b5 BP |
3957 | *sec = msecs / 1000; |
3958 | *nsec = (msecs % 1000) * (1000 * 1000); | |
3959 | } | |
3960 | ||
48266274 | 3961 | /* Checks whether 'table_id' is 0xff or a valid table ID in 'ofproto'. Returns |
554764d1 SH |
3962 | * true if 'table_id' is OK, false otherwise. */ |
3963 | static bool | |
48266274 BP |
3964 | check_table_id(const struct ofproto *ofproto, uint8_t table_id) |
3965 | { | |
554764d1 | 3966 | return table_id == OFPTT_ALL || table_id < ofproto->n_tables; |
48266274 BP |
3967 | } |
3968 | ||
5c67e4af | 3969 | static struct oftable * |
d4ce8a49 | 3970 | next_visible_table(const struct ofproto *ofproto, uint8_t table_id) |
5c67e4af BP |
3971 | { |
3972 | struct oftable *table; | |
3973 | ||
3974 | for (table = &ofproto->tables[table_id]; | |
3975 | table < &ofproto->tables[ofproto->n_tables]; | |
3976 | table++) { | |
3977 | if (!(table->flags & OFTABLE_HIDDEN)) { | |
3978 | return table; | |
3979 | } | |
3980 | } | |
3981 | ||
3982 | return NULL; | |
3983 | } | |
3984 | ||
d0918789 | 3985 | static struct oftable * |
d4ce8a49 | 3986 | first_matching_table(const struct ofproto *ofproto, uint8_t table_id) |
064af421 | 3987 | { |
6c1491fb | 3988 | if (table_id == 0xff) { |
5c67e4af | 3989 | return next_visible_table(ofproto, 0); |
6c1491fb BP |
3990 | } else if (table_id < ofproto->n_tables) { |
3991 | return &ofproto->tables[table_id]; | |
a02e5331 | 3992 | } else { |
6c1491fb | 3993 | return NULL; |
a02e5331 | 3994 | } |
064af421 BP |
3995 | } |
3996 | ||
d0918789 | 3997 | static struct oftable * |
d4ce8a49 BP |
3998 | next_matching_table(const struct ofproto *ofproto, |
3999 | const struct oftable *table, uint8_t table_id) | |
6c1491fb | 4000 | { |
5c67e4af BP |
4001 | return (table_id == 0xff |
4002 | ? next_visible_table(ofproto, (table - ofproto->tables) + 1) | |
6c1491fb BP |
4003 | : NULL); |
4004 | } | |
4005 | ||
d0918789 | 4006 | /* Assigns TABLE to each oftable, in turn, that matches TABLE_ID in OFPROTO: |
6c1491fb BP |
4007 | * |
4008 | * - If TABLE_ID is 0xff, this iterates over every classifier table in | |
5c67e4af | 4009 | * OFPROTO, skipping tables marked OFTABLE_HIDDEN. |
6c1491fb BP |
4010 | * |
4011 | * - If TABLE_ID is the number of a table in OFPROTO, then the loop iterates | |
5c67e4af BP |
4012 | * only once, for that table. (This can be used to access tables marked |
4013 | * OFTABLE_HIDDEN.) | |
6c1491fb | 4014 | * |
48266274 BP |
4015 | * - Otherwise, TABLE_ID isn't valid for OFPROTO, so the loop won't be |
4016 | * entered at all. (Perhaps you should have validated TABLE_ID with | |
4017 | * check_table_id().) | |
6c1491fb BP |
4018 | * |
4019 | * All parameters are evaluated multiple times. | |
4020 | */ | |
d0918789 BP |
4021 | #define FOR_EACH_MATCHING_TABLE(TABLE, TABLE_ID, OFPROTO) \ |
4022 | for ((TABLE) = first_matching_table(OFPROTO, TABLE_ID); \ | |
4023 | (TABLE) != NULL; \ | |
4024 | (TABLE) = next_matching_table(OFPROTO, TABLE, TABLE_ID)) | |
6c1491fb | 4025 | |
a9b22b7f BP |
4026 | /* Initializes 'criteria' in a straightforward way based on the other |
4027 | * parameters. | |
4028 | * | |
dd51dae2 BP |
4029 | * By default, the criteria include flows that are read-only, on the assumption |
4030 | * that the collected flows won't be modified. Call rule_criteria_require_rw() | |
4031 | * if flows will be modified. | |
4032 | * | |
a9b22b7f BP |
4033 | * For "loose" matching, the 'priority' parameter is unimportant and may be |
4034 | * supplied as 0. */ | |
4035 | static void | |
4036 | rule_criteria_init(struct rule_criteria *criteria, uint8_t table_id, | |
18721c4a | 4037 | const struct match *match, int priority, |
44e0c35d | 4038 | ovs_version_t version, ovs_be64 cookie, |
18721c4a JR |
4039 | ovs_be64 cookie_mask, ofp_port_t out_port, |
4040 | uint32_t out_group) | |
a9b22b7f BP |
4041 | { |
4042 | criteria->table_id = table_id; | |
bd53aa17 JR |
4043 | cls_rule_init(&criteria->cr, match, priority); |
4044 | criteria->version = version; | |
a9b22b7f BP |
4045 | criteria->cookie = cookie; |
4046 | criteria->cookie_mask = cookie_mask; | |
4047 | criteria->out_port = out_port; | |
4048 | criteria->out_group = out_group; | |
dd51dae2 BP |
4049 | |
4050 | /* We ordinarily want to skip hidden rules, but there has to be a way for | |
4051 | * code internal to OVS to modify and delete them, so if the criteria | |
4052 | * specify a priority that can only be for a hidden flow, then allow hidden | |
4053 | * rules to be selected. (This doesn't allow OpenFlow clients to meddle | |
4054 | * with hidden flows because OpenFlow uses only a 16-bit field to specify | |
4055 | * priority.) */ | |
4056 | criteria->include_hidden = priority > UINT16_MAX; | |
4057 | ||
4058 | /* We assume that the criteria are being used to collect flows for reading | |
4059 | * but not modification. Thus, we should collect read-only flows. */ | |
4060 | criteria->include_readonly = true; | |
4061 | } | |
4062 | ||
4063 | /* By default, criteria initialized by rule_criteria_init() will match flows | |
4064 | * that are read-only, on the assumption that the collected flows won't be | |
4065 | * modified. Call this function to match only flows that are be modifiable. | |
4066 | * | |
4067 | * Specify 'can_write_readonly' as false in ordinary circumstances, true if the | |
4068 | * caller has special privileges that allow it to modify even "read-only" | |
4069 | * flows. */ | |
4070 | static void | |
4071 | rule_criteria_require_rw(struct rule_criteria *criteria, | |
4072 | bool can_write_readonly) | |
4073 | { | |
4074 | criteria->include_readonly = can_write_readonly; | |
a9b22b7f BP |
4075 | } |
4076 | ||
4077 | static void | |
4078 | rule_criteria_destroy(struct rule_criteria *criteria) | |
4079 | { | |
4080 | cls_rule_destroy(&criteria->cr); | |
5bacd5cd | 4081 | criteria->version = OVS_VERSION_NOT_REMOVED; /* Mark as destroyed. */ |
a9b22b7f BP |
4082 | } |
4083 | ||
39c94593 JR |
4084 | /* Schedules postponed removal of rules, destroys 'rules'. */ |
4085 | static void | |
cc099268 | 4086 | remove_rules_postponed(struct rule_collection *rules) |
39c94593 JR |
4087 | OVS_REQUIRES(ofproto_mutex) |
4088 | { | |
fe59694b JR |
4089 | if (rule_collection_n(rules) > 0) { |
4090 | if (rule_collection_n(rules) == 1) { | |
4091 | ovsrcu_postpone(remove_rule_rcu, rule_collection_rules(rules)[0]); | |
4092 | rule_collection_init(rules); | |
39c94593 JR |
4093 | } else { |
4094 | ovsrcu_postpone(remove_rules_rcu, rule_collection_detach(rules)); | |
4095 | } | |
4096 | } | |
4097 | } | |
4098 | ||
5d08a275 JR |
4099 | /* Schedules postponed removal of groups, destroys 'groups'. */ |
4100 | static void | |
4101 | remove_groups_postponed(struct group_collection *groups) | |
4102 | OVS_REQUIRES(ofproto_mutex) | |
4103 | { | |
4104 | if (group_collection_n(groups) > 0) { | |
4105 | if (group_collection_n(groups) == 1) { | |
4106 | ovsrcu_postpone(remove_group_rcu, | |
4107 | group_collection_groups(groups)[0]); | |
4108 | group_collection_init(groups); | |
4109 | } else { | |
4110 | ovsrcu_postpone(remove_groups_rcu, | |
4111 | group_collection_detach(groups)); | |
4112 | } | |
4113 | } | |
4114 | } | |
4115 | ||
dd51dae2 BP |
4116 | /* Checks whether 'rule' matches 'c' and, if so, adds it to 'rules'. This |
4117 | * function verifies most of the criteria in 'c' itself, but the caller must | |
4118 | * check 'c->cr' itself. | |
4119 | * | |
2b7b1427 | 4120 | * Rules that have already been marked for removal are not collected. |
ce59413f | 4121 | * |
dd51dae2 | 4122 | * Increments '*n_readonly' if 'rule' wasn't added because it's read-only (and |
b20f4073 BP |
4123 | * 'c' only includes modifiable rules). */ |
4124 | static void | |
a9b22b7f | 4125 | collect_rule(struct rule *rule, const struct rule_criteria *c, |
dd51dae2 | 4126 | struct rule_collection *rules, size_t *n_readonly) |
15aaf599 | 4127 | OVS_REQUIRES(ofproto_mutex) |
6c1d7642 | 4128 | { |
dd51dae2 BP |
4129 | if ((c->table_id == rule->table_id || c->table_id == 0xff) |
4130 | && ofproto_rule_has_out_port(rule, c->out_port) | |
4131 | && ofproto_rule_has_out_group(rule, c->out_group) | |
4132 | && !((rule->flow_cookie ^ c->cookie) & c->cookie_mask) | |
ce59413f | 4133 | && (!rule_is_hidden(rule) || c->include_hidden) |
bd53aa17 | 4134 | && cls_rule_visible_in_version(&rule->cr, c->version)) { |
dd51dae2 | 4135 | /* Rule matches all the criteria... */ |
834fe5cb | 4136 | if (!rule_is_readonly(rule) || c->include_readonly) { |
dd51dae2 | 4137 | /* ...add it. */ |
a8e547c1 | 4138 | rule_collection_add(rules, rule); |
dd51dae2 BP |
4139 | } else { |
4140 | /* ...except it's read-only. */ | |
4141 | ++*n_readonly; | |
6c1d7642 | 4142 | } |
6c1d7642 BP |
4143 | } |
4144 | } | |
4145 | ||
a9b22b7f BP |
4146 | /* Searches 'ofproto' for rules that match the criteria in 'criteria'. Matches |
4147 | * on classifiers rules are done in the "loose" way required for OpenFlow | |
4148 | * OFPFC_MODIFY and OFPFC_DELETE requests. Puts the selected rules on list | |
9ed18e46 BP |
4149 | * 'rules'. |
4150 | * | |
9ed18e46 | 4151 | * Returns 0 on success, otherwise an OpenFlow error code. */ |
90bf1e07 | 4152 | static enum ofperr |
a9b22b7f | 4153 | collect_rules_loose(struct ofproto *ofproto, |
a8e547c1 BP |
4154 | const struct rule_criteria *criteria, |
4155 | struct rule_collection *rules) | |
15aaf599 | 4156 | OVS_REQUIRES(ofproto_mutex) |
9ed18e46 | 4157 | { |
d0918789 | 4158 | struct oftable *table; |
554764d1 | 4159 | enum ofperr error = 0; |
dd51dae2 | 4160 | size_t n_readonly = 0; |
48266274 | 4161 | |
a8e547c1 BP |
4162 | rule_collection_init(rules); |
4163 | ||
554764d1 SH |
4164 | if (!check_table_id(ofproto, criteria->table_id)) { |
4165 | error = OFPERR_OFPBRC_BAD_TABLE_ID; | |
a8e547c1 | 4166 | goto exit; |
48266274 | 4167 | } |
9ed18e46 | 4168 | |
b8266395 | 4169 | if (criteria->cookie_mask == OVS_BE64_MAX) { |
98eaac36 JR |
4170 | struct rule *rule; |
4171 | ||
a9b22b7f BP |
4172 | HINDEX_FOR_EACH_WITH_HASH (rule, cookie_node, |
4173 | hash_cookie(criteria->cookie), | |
98eaac36 | 4174 | &ofproto->cookies) { |
a9b22b7f | 4175 | if (cls_rule_is_loose_match(&rule->cr, &criteria->cr.match)) { |
b20f4073 | 4176 | collect_rule(rule, criteria, rules, &n_readonly); |
98eaac36 JR |
4177 | } |
4178 | } | |
6c1d7642 | 4179 | } else { |
a9b22b7f | 4180 | FOR_EACH_MATCHING_TABLE (table, criteria->table_id, ofproto) { |
6c1d7642 | 4181 | struct rule *rule; |
9ed18e46 | 4182 | |
bd53aa17 JR |
4183 | CLS_FOR_EACH_TARGET (rule, cr, &table->cls, &criteria->cr, |
4184 | criteria->version) { | |
b20f4073 | 4185 | collect_rule(rule, criteria, rules, &n_readonly); |
9ed18e46 BP |
4186 | } |
4187 | } | |
4188 | } | |
48d28ac1 | 4189 | |
a8e547c1 | 4190 | exit: |
fe59694b | 4191 | if (!error && !rule_collection_n(rules) && n_readonly) { |
dd51dae2 BP |
4192 | /* We didn't find any rules to modify. We did find some read-only |
4193 | * rules that we're not allowed to modify, so report that. */ | |
4194 | error = OFPERR_OFPBRC_EPERM; | |
4195 | } | |
a8e547c1 BP |
4196 | if (error) { |
4197 | rule_collection_destroy(rules); | |
4198 | } | |
48d28ac1 | 4199 | return error; |
9ed18e46 BP |
4200 | } |
4201 | ||
a9b22b7f BP |
4202 | /* Searches 'ofproto' for rules that match the criteria in 'criteria'. Matches |
4203 | * on classifiers rules are done in the "strict" way required for OpenFlow | |
4204 | * OFPFC_MODIFY_STRICT and OFPFC_DELETE_STRICT requests. Puts the selected | |
4205 | * rules on list 'rules'. | |
9ed18e46 | 4206 | * |
9ed18e46 | 4207 | * Returns 0 on success, otherwise an OpenFlow error code. */ |
90bf1e07 | 4208 | static enum ofperr |
a9b22b7f | 4209 | collect_rules_strict(struct ofproto *ofproto, |
a8e547c1 BP |
4210 | const struct rule_criteria *criteria, |
4211 | struct rule_collection *rules) | |
15aaf599 | 4212 | OVS_REQUIRES(ofproto_mutex) |
9ed18e46 | 4213 | { |
d0918789 | 4214 | struct oftable *table; |
dd51dae2 | 4215 | size_t n_readonly = 0; |
d51c8b71 | 4216 | enum ofperr error = 0; |
48266274 | 4217 | |
a8e547c1 BP |
4218 | rule_collection_init(rules); |
4219 | ||
554764d1 SH |
4220 | if (!check_table_id(ofproto, criteria->table_id)) { |
4221 | error = OFPERR_OFPBRC_BAD_TABLE_ID; | |
a8e547c1 | 4222 | goto exit; |
48266274 | 4223 | } |
9ed18e46 | 4224 | |
b8266395 | 4225 | if (criteria->cookie_mask == OVS_BE64_MAX) { |
98eaac36 JR |
4226 | struct rule *rule; |
4227 | ||
a9b22b7f BP |
4228 | HINDEX_FOR_EACH_WITH_HASH (rule, cookie_node, |
4229 | hash_cookie(criteria->cookie), | |
98eaac36 | 4230 | &ofproto->cookies) { |
a9b22b7f | 4231 | if (cls_rule_equal(&rule->cr, &criteria->cr)) { |
b20f4073 | 4232 | collect_rule(rule, criteria, rules, &n_readonly); |
98eaac36 JR |
4233 | } |
4234 | } | |
6c1d7642 | 4235 | } else { |
a9b22b7f | 4236 | FOR_EACH_MATCHING_TABLE (table, criteria->table_id, ofproto) { |
6c1d7642 | 4237 | struct rule *rule; |
9ed18e46 | 4238 | |
a9b22b7f | 4239 | rule = rule_from_cls_rule(classifier_find_rule_exactly( |
bd53aa17 JR |
4240 | &table->cls, &criteria->cr, |
4241 | criteria->version)); | |
6c1d7642 | 4242 | if (rule) { |
b20f4073 | 4243 | collect_rule(rule, criteria, rules, &n_readonly); |
9ed18e46 BP |
4244 | } |
4245 | } | |
4246 | } | |
48d28ac1 | 4247 | |
a8e547c1 | 4248 | exit: |
fe59694b | 4249 | if (!error && !rule_collection_n(rules) && n_readonly) { |
b20f4073 BP |
4250 | /* We didn't find any rules to modify. We did find some read-only |
4251 | * rules that we're not allowed to modify, so report that. */ | |
4252 | error = OFPERR_OFPBRC_EPERM; | |
4253 | } | |
a8e547c1 BP |
4254 | if (error) { |
4255 | rule_collection_destroy(rules); | |
4256 | } | |
6c1d7642 | 4257 | return error; |
9ed18e46 BP |
4258 | } |
4259 | ||
f27f2134 BP |
4260 | /* Returns 'age_ms' (a duration in milliseconds), converted to seconds and |
4261 | * forced into the range of a uint16_t. */ | |
4262 | static int | |
4263 | age_secs(long long int age_ms) | |
4264 | { | |
4265 | return (age_ms < 0 ? 0 | |
4266 | : age_ms >= UINT16_MAX * 1000 ? UINT16_MAX | |
4267 | : (unsigned int) age_ms / 1000); | |
4268 | } | |
4269 | ||
90bf1e07 | 4270 | static enum ofperr |
63f2140a | 4271 | handle_flow_stats_request(struct ofconn *ofconn, |
982697a4 | 4272 | const struct ofp_header *request) |
15aaf599 | 4273 | OVS_EXCLUDED(ofproto_mutex) |
064af421 | 4274 | { |
64ff1d96 | 4275 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
81d1ea94 | 4276 | struct ofputil_flow_stats_request fsr; |
a9b22b7f | 4277 | struct rule_criteria criteria; |
a8e547c1 | 4278 | struct rule_collection rules; |
ca6ba700 | 4279 | struct ovs_list replies; |
90bf1e07 | 4280 | enum ofperr error; |
09246b99 | 4281 | |
8d8ab6c2 JG |
4282 | error = ofputil_decode_flow_stats_request(&fsr, request, |
4283 | ofproto_get_tun_tab(ofproto)); | |
09246b99 BP |
4284 | if (error) { |
4285 | return error; | |
4286 | } | |
4287 | ||
44e0c35d | 4288 | rule_criteria_init(&criteria, fsr.table_id, &fsr.match, 0, OVS_VERSION_MAX, |
2b7b1427 JR |
4289 | fsr.cookie, fsr.cookie_mask, fsr.out_port, |
4290 | fsr.out_group); | |
15aaf599 BP |
4291 | |
4292 | ovs_mutex_lock(&ofproto_mutex); | |
a9b22b7f BP |
4293 | error = collect_rules_loose(ofproto, &criteria, &rules); |
4294 | rule_criteria_destroy(&criteria); | |
15aaf599 BP |
4295 | if (!error) { |
4296 | rule_collection_ref(&rules); | |
4297 | } | |
4298 | ovs_mutex_unlock(&ofproto_mutex); | |
4299 | ||
9ed18e46 BP |
4300 | if (error) { |
4301 | return error; | |
4302 | } | |
5ecc9d81 | 4303 | |
982697a4 | 4304 | ofpmp_init(&replies, request); |
fe59694b JR |
4305 | struct rule *rule; |
4306 | RULE_COLLECTION_FOR_EACH (rule, &rules) { | |
f27f2134 | 4307 | long long int now = time_msec(); |
9ed18e46 | 4308 | struct ofputil_flow_stats fs; |
15aaf599 | 4309 | long long int created, used, modified; |
dc723c44 | 4310 | const struct rule_actions *actions; |
15aaf599 | 4311 | enum ofputil_flow_mod_flags flags; |
a3779dbc | 4312 | |
d10976b7 | 4313 | ovs_mutex_lock(&rule->mutex); |
15aaf599 | 4314 | fs.cookie = rule->flow_cookie; |
a3779dbc EJ |
4315 | fs.idle_timeout = rule->idle_timeout; |
4316 | fs.hard_timeout = rule->hard_timeout; | |
ca26eb44 | 4317 | fs.importance = rule->importance; |
15aaf599 | 4318 | created = rule->created; |
15aaf599 | 4319 | modified = rule->modified; |
06a0f3e2 | 4320 | actions = rule_get_actions(rule); |
15aaf599 | 4321 | flags = rule->flags; |
d10976b7 | 4322 | ovs_mutex_unlock(&rule->mutex); |
a3779dbc | 4323 | |
dc437090 JR |
4324 | ofproto->ofproto_class->rule_get_stats(rule, &fs.packet_count, |
4325 | &fs.byte_count, &used); | |
4326 | ||
15aaf599 BP |
4327 | minimatch_expand(&rule->cr.match, &fs.match); |
4328 | fs.table_id = rule->table_id; | |
4329 | calc_duration(created, now, &fs.duration_sec, &fs.duration_nsec); | |
4330 | fs.priority = rule->cr.priority; | |
4331 | fs.idle_age = age_secs(now - used); | |
4332 | fs.hard_age = age_secs(now - modified); | |
15aaf599 BP |
4333 | fs.ofpacts = actions->ofpacts; |
4334 | fs.ofpacts_len = actions->ofpacts_len; | |
3f517bcd | 4335 | |
15aaf599 | 4336 | fs.flags = flags; |
8d8ab6c2 JG |
4337 | ofputil_append_flow_stats_reply(&fs, &replies, |
4338 | ofproto_get_tun_tab(ofproto)); | |
3c4486a5 | 4339 | } |
15aaf599 BP |
4340 | |
4341 | rule_collection_unref(&rules); | |
a8e547c1 BP |
4342 | rule_collection_destroy(&rules); |
4343 | ||
63f2140a | 4344 | ofconn_send_replies(ofconn, &replies); |
5ecc9d81 | 4345 | |
09246b99 BP |
4346 | return 0; |
4347 | } | |
4348 | ||
4f2cad2c | 4349 | static void |
8d8ab6c2 | 4350 | flow_stats_ds(struct ofproto *ofproto, struct rule *rule, struct ds *results) |
4f2cad2c | 4351 | { |
4f2cad2c | 4352 | uint64_t packet_count, byte_count; |
dc723c44 | 4353 | const struct rule_actions *actions; |
dc437090 | 4354 | long long int created, used; |
4f2cad2c | 4355 | |
dc437090 JR |
4356 | rule->ofproto->ofproto_class->rule_get_stats(rule, &packet_count, |
4357 | &byte_count, &used); | |
4f2cad2c | 4358 | |
15aaf599 | 4359 | ovs_mutex_lock(&rule->mutex); |
06a0f3e2 | 4360 | actions = rule_get_actions(rule); |
15aaf599 BP |
4361 | created = rule->created; |
4362 | ovs_mutex_unlock(&rule->mutex); | |
4363 | ||
6c1491fb BP |
4364 | if (rule->table_id != 0) { |
4365 | ds_put_format(results, "table_id=%"PRIu8", ", rule->table_id); | |
4366 | } | |
15aaf599 | 4367 | ds_put_format(results, "duration=%llds, ", (time_msec() - created) / 1000); |
4f2cad2c JP |
4368 | ds_put_format(results, "n_packets=%"PRIu64", ", packet_count); |
4369 | ds_put_format(results, "n_bytes=%"PRIu64", ", byte_count); | |
8d8ab6c2 | 4370 | cls_rule_format(&rule->cr, ofproto_get_tun_tab(ofproto), results); |
a5df0e72 | 4371 | ds_put_char(results, ','); |
15aaf599 | 4372 | |
455ecd77 | 4373 | ds_put_cstr(results, "actions="); |
15aaf599 BP |
4374 | ofpacts_format(actions->ofpacts, actions->ofpacts_len, results); |
4375 | ||
4f2cad2c JP |
4376 | ds_put_cstr(results, "\n"); |
4377 | } | |
4378 | ||
d295e8e9 | 4379 | /* Adds a pretty-printed description of all flows to 'results', including |
ee8b231c | 4380 | * hidden flows (e.g., set up by in-band control). */ |
4f2cad2c JP |
4381 | void |
4382 | ofproto_get_all_flows(struct ofproto *p, struct ds *results) | |
4383 | { | |
d0918789 | 4384 | struct oftable *table; |
6c1491fb | 4385 | |
d0918789 | 4386 | OFPROTO_FOR_EACH_TABLE (table, p) { |
6c1491fb | 4387 | struct rule *rule; |
064af421 | 4388 | |
5f0476ce | 4389 | CLS_FOR_EACH (rule, cr, &table->cls) { |
8d8ab6c2 | 4390 | flow_stats_ds(p, rule, results); |
6c1491fb | 4391 | } |
064af421 | 4392 | } |
064af421 BP |
4393 | } |
4394 | ||
b5827b24 BP |
4395 | /* Obtains the NetFlow engine type and engine ID for 'ofproto' into |
4396 | * '*engine_type' and '*engine_id', respectively. */ | |
4397 | void | |
4398 | ofproto_get_netflow_ids(const struct ofproto *ofproto, | |
4399 | uint8_t *engine_type, uint8_t *engine_id) | |
4400 | { | |
abe529af | 4401 | ofproto->ofproto_class->get_netflow_ids(ofproto, engine_type, engine_id); |
b5827b24 BP |
4402 | } |
4403 | ||
8f5514fe AW |
4404 | /* Checks the status change of CFM on 'ofport'. |
4405 | * | |
4406 | * Returns true if 'ofproto_class' does not support 'cfm_status_changed'. */ | |
4407 | bool | |
4408 | ofproto_port_cfm_status_changed(struct ofproto *ofproto, ofp_port_t ofp_port) | |
4409 | { | |
4410 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); | |
4411 | return (ofport && ofproto->ofproto_class->cfm_status_changed | |
4412 | ? ofproto->ofproto_class->cfm_status_changed(ofport) | |
4413 | : true); | |
4414 | } | |
4415 | ||
88bf179a AW |
4416 | /* Checks the status of CFM configured on 'ofp_port' within 'ofproto'. |
4417 | * Returns 0 if the port's CFM status was successfully stored into | |
4418 | * '*status'. Returns positive errno if the port did not have CFM | |
8f5514fe | 4419 | * configured. |
9a9e3786 | 4420 | * |
88bf179a AW |
4421 | * The caller must provide and own '*status', and must free 'status->rmps'. |
4422 | * '*status' is indeterminate if the return value is non-zero. */ | |
4423 | int | |
4e022ec0 | 4424 | ofproto_port_get_cfm_status(const struct ofproto *ofproto, ofp_port_t ofp_port, |
685acfd9 | 4425 | struct cfm_status *status) |
3967a833 MM |
4426 | { |
4427 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); | |
88bf179a AW |
4428 | return (ofport && ofproto->ofproto_class->get_cfm_status |
4429 | ? ofproto->ofproto_class->get_cfm_status(ofport, status) | |
4430 | : EOPNOTSUPP); | |
3967a833 MM |
4431 | } |
4432 | ||
90bf1e07 | 4433 | static enum ofperr |
76c93b22 | 4434 | handle_aggregate_stats_request(struct ofconn *ofconn, |
982697a4 | 4435 | const struct ofp_header *oh) |
15aaf599 | 4436 | OVS_EXCLUDED(ofproto_mutex) |
27d34fce | 4437 | { |
76c93b22 | 4438 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
81d1ea94 | 4439 | struct ofputil_flow_stats_request request; |
76c93b22 | 4440 | struct ofputil_aggregate_stats stats; |
5e9d0469 | 4441 | bool unknown_packets, unknown_bytes; |
a9b22b7f | 4442 | struct rule_criteria criteria; |
a8e547c1 | 4443 | struct rule_collection rules; |
76c93b22 | 4444 | struct ofpbuf *reply; |
90bf1e07 | 4445 | enum ofperr error; |
27d34fce | 4446 | |
8d8ab6c2 JG |
4447 | error = ofputil_decode_flow_stats_request(&request, oh, |
4448 | ofproto_get_tun_tab(ofproto)); | |
76c93b22 BP |
4449 | if (error) { |
4450 | return error; | |
4451 | } | |
5ecc9d81 | 4452 | |
a9b22b7f | 4453 | rule_criteria_init(&criteria, request.table_id, &request.match, 0, |
44e0c35d | 4454 | OVS_VERSION_MAX, request.cookie, request.cookie_mask, |
a9b22b7f | 4455 | request.out_port, request.out_group); |
15aaf599 BP |
4456 | |
4457 | ovs_mutex_lock(&ofproto_mutex); | |
a9b22b7f BP |
4458 | error = collect_rules_loose(ofproto, &criteria, &rules); |
4459 | rule_criteria_destroy(&criteria); | |
15aaf599 BP |
4460 | if (!error) { |
4461 | rule_collection_ref(&rules); | |
4462 | } | |
4463 | ovs_mutex_unlock(&ofproto_mutex); | |
4464 | ||
9ed18e46 BP |
4465 | if (error) { |
4466 | return error; | |
4467 | } | |
3c4486a5 | 4468 | |
9ed18e46 | 4469 | memset(&stats, 0, sizeof stats); |
5e9d0469 | 4470 | unknown_packets = unknown_bytes = false; |
fe59694b JR |
4471 | |
4472 | struct rule *rule; | |
4473 | RULE_COLLECTION_FOR_EACH (rule, &rules) { | |
9ed18e46 BP |
4474 | uint64_t packet_count; |
4475 | uint64_t byte_count; | |
dc437090 | 4476 | long long int used; |
5ecc9d81 | 4477 | |
9ed18e46 | 4478 | ofproto->ofproto_class->rule_get_stats(rule, &packet_count, |
dc437090 | 4479 | &byte_count, &used); |
5ecc9d81 | 4480 | |
5e9d0469 BP |
4481 | if (packet_count == UINT64_MAX) { |
4482 | unknown_packets = true; | |
4483 | } else { | |
4484 | stats.packet_count += packet_count; | |
4485 | } | |
4486 | ||
4487 | if (byte_count == UINT64_MAX) { | |
4488 | unknown_bytes = true; | |
4489 | } else { | |
4490 | stats.byte_count += byte_count; | |
4491 | } | |
4492 | ||
9ed18e46 | 4493 | stats.flow_count++; |
3c4486a5 | 4494 | } |
5e9d0469 BP |
4495 | if (unknown_packets) { |
4496 | stats.packet_count = UINT64_MAX; | |
4497 | } | |
4498 | if (unknown_bytes) { | |
4499 | stats.byte_count = UINT64_MAX; | |
4500 | } | |
27d34fce | 4501 | |
15aaf599 | 4502 | rule_collection_unref(&rules); |
a8e547c1 BP |
4503 | rule_collection_destroy(&rules); |
4504 | ||
982697a4 | 4505 | reply = ofputil_encode_aggregate_stats_reply(&stats, oh); |
76c93b22 | 4506 | ofconn_send_reply(ofconn, reply); |
09246b99 BP |
4507 | |
4508 | return 0; | |
4509 | } | |
4510 | ||
c1c9c9c4 | 4511 | struct queue_stats_cbdata { |
ca0f572c | 4512 | struct ofport *ofport; |
ca6ba700 | 4513 | struct ovs_list replies; |
6dc34a0d | 4514 | long long int now; |
c1c9c9c4 BP |
4515 | }; |
4516 | ||
4517 | static void | |
db9220c3 | 4518 | put_queue_stats(struct queue_stats_cbdata *cbdata, uint32_t queue_id, |
c1c9c9c4 BP |
4519 | const struct netdev_queue_stats *stats) |
4520 | { | |
6dc34a0d | 4521 | struct ofputil_queue_stats oqs; |
c1c9c9c4 | 4522 | |
6dc34a0d BP |
4523 | oqs.port_no = cbdata->ofport->pp.port_no; |
4524 | oqs.queue_id = queue_id; | |
4525 | oqs.tx_bytes = stats->tx_bytes; | |
4526 | oqs.tx_packets = stats->tx_packets; | |
4527 | oqs.tx_errors = stats->tx_errors; | |
4528 | if (stats->created != LLONG_MIN) { | |
4529 | calc_duration(stats->created, cbdata->now, | |
4530 | &oqs.duration_sec, &oqs.duration_nsec); | |
4531 | } else { | |
4532 | oqs.duration_sec = oqs.duration_nsec = UINT32_MAX; | |
4533 | } | |
64626975 | 4534 | ofputil_append_queue_stat(&cbdata->replies, &oqs); |
c1c9c9c4 BP |
4535 | } |
4536 | ||
4537 | static void | |
db9220c3 | 4538 | handle_queue_stats_dump_cb(uint32_t queue_id, |
c1c9c9c4 BP |
4539 | struct netdev_queue_stats *stats, |
4540 | void *cbdata_) | |
4541 | { | |
4542 | struct queue_stats_cbdata *cbdata = cbdata_; | |
4543 | ||
4544 | put_queue_stats(cbdata, queue_id, stats); | |
4545 | } | |
4546 | ||
0414d158 | 4547 | static enum ofperr |
ca0f572c | 4548 | handle_queue_stats_for_port(struct ofport *port, uint32_t queue_id, |
c1c9c9c4 BP |
4549 | struct queue_stats_cbdata *cbdata) |
4550 | { | |
ca0f572c | 4551 | cbdata->ofport = port; |
c1c9c9c4 BP |
4552 | if (queue_id == OFPQ_ALL) { |
4553 | netdev_dump_queue_stats(port->netdev, | |
4554 | handle_queue_stats_dump_cb, cbdata); | |
4555 | } else { | |
4556 | struct netdev_queue_stats stats; | |
4557 | ||
1ac788f6 BP |
4558 | if (!netdev_get_queue_stats(port->netdev, queue_id, &stats)) { |
4559 | put_queue_stats(cbdata, queue_id, &stats); | |
0414d158 BP |
4560 | } else { |
4561 | return OFPERR_OFPQOFC_BAD_QUEUE; | |
1ac788f6 | 4562 | } |
c1c9c9c4 | 4563 | } |
0414d158 | 4564 | return 0; |
c1c9c9c4 BP |
4565 | } |
4566 | ||
90bf1e07 | 4567 | static enum ofperr |
63f2140a | 4568 | handle_queue_stats_request(struct ofconn *ofconn, |
982697a4 | 4569 | const struct ofp_header *rq) |
c1c9c9c4 | 4570 | { |
64ff1d96 | 4571 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
c1c9c9c4 | 4572 | struct queue_stats_cbdata cbdata; |
0414d158 | 4573 | struct ofport *port; |
0414d158 | 4574 | enum ofperr error; |
64626975 | 4575 | struct ofputil_queue_stats_request oqsr; |
c1c9c9c4 | 4576 | |
c1c9c9c4 BP |
4577 | COVERAGE_INC(ofproto_queue_req); |
4578 | ||
982697a4 | 4579 | ofpmp_init(&cbdata.replies, rq); |
6dc34a0d | 4580 | cbdata.now = time_msec(); |
c1c9c9c4 | 4581 | |
64626975 SH |
4582 | error = ofputil_decode_queue_stats_request(rq, &oqsr); |
4583 | if (error) { | |
4584 | return error; | |
4585 | } | |
4586 | ||
7f05e7ab | 4587 | if (oqsr.port_no == OFPP_ANY) { |
0414d158 | 4588 | error = OFPERR_OFPQOFC_BAD_QUEUE; |
4e8e4213 | 4589 | HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) { |
64626975 | 4590 | if (!handle_queue_stats_for_port(port, oqsr.queue_id, &cbdata)) { |
0414d158 BP |
4591 | error = 0; |
4592 | } | |
c1c9c9c4 | 4593 | } |
0414d158 | 4594 | } else { |
64626975 | 4595 | port = ofproto_get_port(ofproto, oqsr.port_no); |
0414d158 | 4596 | error = (port |
64626975 | 4597 | ? handle_queue_stats_for_port(port, oqsr.queue_id, &cbdata) |
0414d158 BP |
4598 | : OFPERR_OFPQOFC_BAD_PORT); |
4599 | } | |
4600 | if (!error) { | |
4601 | ofconn_send_replies(ofconn, &cbdata.replies); | |
c1c9c9c4 | 4602 | } else { |
63f2140a | 4603 | ofpbuf_list_delete(&cbdata.replies); |
c1c9c9c4 | 4604 | } |
c1c9c9c4 | 4605 | |
0414d158 | 4606 | return error; |
c1c9c9c4 | 4607 | } |
7ee20df1 | 4608 | |
2e31a9b8 | 4609 | static enum ofperr |
6787a49f | 4610 | evict_rules_from_table(struct oftable *table) |
15aaf599 | 4611 | OVS_REQUIRES(ofproto_mutex) |
2e31a9b8 | 4612 | { |
802f84ff JR |
4613 | enum ofperr error = 0; |
4614 | struct rule_collection rules; | |
6787a49f | 4615 | unsigned int count = table->n_flows; |
802f84ff JR |
4616 | unsigned int max_flows = table->max_flows; |
4617 | ||
4618 | rule_collection_init(&rules); | |
4619 | ||
4620 | while (count-- > max_flows) { | |
2e31a9b8 | 4621 | struct rule *rule; |
8037acb4 | 4622 | |
2e31a9b8 | 4623 | if (!choose_rule_to_evict(table, &rule)) { |
802f84ff JR |
4624 | error = OFPERR_OFPFMFC_TABLE_FULL; |
4625 | break; | |
2e31a9b8 | 4626 | } else { |
802f84ff JR |
4627 | eviction_group_remove_rule(rule); |
4628 | rule_collection_add(&rules, rule); | |
2e31a9b8 | 4629 | } |
8037acb4 | 4630 | } |
802f84ff | 4631 | delete_flows__(&rules, OFPRR_EVICTION, NULL); |
2e31a9b8 | 4632 | |
802f84ff | 4633 | return error; |
8037acb4 BP |
4634 | } |
4635 | ||
18080541 BP |
4636 | static void |
4637 | get_conjunctions(const struct ofputil_flow_mod *fm, | |
4638 | struct cls_conjunction **conjsp, size_t *n_conjsp) | |
18080541 BP |
4639 | { |
4640 | struct cls_conjunction *conjs = NULL; | |
4641 | int n_conjs = 0; | |
4642 | ||
f08e39dd BP |
4643 | const struct ofpact *ofpact; |
4644 | OFPACT_FOR_EACH (ofpact, fm->ofpacts, fm->ofpacts_len) { | |
4645 | if (ofpact->type == OFPACT_CONJUNCTION) { | |
18080541 | 4646 | n_conjs++; |
f08e39dd BP |
4647 | } else if (ofpact->type != OFPACT_NOTE) { |
4648 | /* "conjunction" may appear with "note" actions but not with any | |
4649 | * other type of actions. */ | |
4650 | ovs_assert(!n_conjs); | |
4651 | break; | |
18080541 | 4652 | } |
f08e39dd BP |
4653 | } |
4654 | if (n_conjs) { | |
4655 | int i = 0; | |
18080541 BP |
4656 | |
4657 | conjs = xzalloc(n_conjs * sizeof *conjs); | |
18080541 | 4658 | OFPACT_FOR_EACH (ofpact, fm->ofpacts, fm->ofpacts_len) { |
f08e39dd BP |
4659 | if (ofpact->type == OFPACT_CONJUNCTION) { |
4660 | struct ofpact_conjunction *oc = ofpact_get_CONJUNCTION(ofpact); | |
4661 | conjs[i].clause = oc->clause; | |
4662 | conjs[i].n_clauses = oc->n_clauses; | |
4663 | conjs[i].id = oc->id; | |
4664 | i++; | |
4665 | } | |
18080541 BP |
4666 | } |
4667 | } | |
4668 | ||
4669 | *conjsp = conjs; | |
4670 | *n_conjsp = n_conjs; | |
4671 | } | |
4672 | ||
5bacd5cd JR |
4673 | /* add_flow_init(), add_flow_start(), add_flow_revert(), and add_flow_finish() |
4674 | * implement OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT | |
1f42be1c JR |
4675 | * in which no matching flow already exists in the flow table. |
4676 | * | |
5bacd5cd JR |
4677 | * add_flow_init() creates a new flow according to 'fm' and stores it to 'ofm' |
4678 | * for later reference. If the flow replaces other flow, it will be updated to | |
4679 | * match modify semantics later by add_flow_start() (by calling | |
4680 | * replace_rule_start()). | |
1f42be1c | 4681 | * |
5bacd5cd | 4682 | * Returns 0 on success, or an OpenFlow error code on failure. |
1f42be1c | 4683 | * |
5bacd5cd JR |
4684 | * On successful return the caller must complete the operation by calling |
4685 | * add_flow_start(), and if that succeeds, then either add_flow_finish(), or | |
4686 | * add_flow_revert() if the operation needs to be reverted due to a later | |
4687 | * failure. | |
4688 | */ | |
90bf1e07 | 4689 | static enum ofperr |
5bacd5cd JR |
4690 | add_flow_init(struct ofproto *ofproto, struct ofproto_flow_mod *ofm, |
4691 | const struct ofputil_flow_mod *fm) | |
4692 | OVS_EXCLUDED(ofproto_mutex) | |
064af421 | 4693 | { |
d0918789 | 4694 | struct oftable *table; |
8037acb4 | 4695 | struct cls_rule cr; |
5a361239 | 4696 | uint8_t table_id; |
39c94593 | 4697 | enum ofperr error; |
064af421 | 4698 | |
554764d1 | 4699 | if (!check_table_id(ofproto, fm->table_id)) { |
5bacd5cd | 4700 | return OFPERR_OFPBRC_BAD_TABLE_ID; |
48266274 BP |
4701 | } |
4702 | ||
7ee20df1 BP |
4703 | /* Pick table. */ |
4704 | if (fm->table_id == 0xff) { | |
13521ff5 | 4705 | if (ofproto->ofproto_class->rule_choose_table) { |
81a76618 BP |
4706 | error = ofproto->ofproto_class->rule_choose_table(ofproto, |
4707 | &fm->match, | |
7ee20df1 BP |
4708 | &table_id); |
4709 | if (error) { | |
4710 | return error; | |
4711 | } | |
cb22974d | 4712 | ovs_assert(table_id < ofproto->n_tables); |
7ee20df1 | 4713 | } else { |
5a361239 | 4714 | table_id = 0; |
7ee20df1 BP |
4715 | } |
4716 | } else if (fm->table_id < ofproto->n_tables) { | |
5a361239 | 4717 | table_id = fm->table_id; |
7ee20df1 | 4718 | } else { |
c22c56bd | 4719 | return OFPERR_OFPBRC_BAD_TABLE_ID; |
064af421 BP |
4720 | } |
4721 | ||
5a361239 | 4722 | table = &ofproto->tables[table_id]; |
834fe5cb BP |
4723 | if (table->flags & OFTABLE_READONLY |
4724 | && !(fm->flags & OFPUTIL_FF_NO_READONLY)) { | |
5c67e4af BP |
4725 | return OFPERR_OFPBRC_EPERM; |
4726 | } | |
4727 | ||
c84d8691 JR |
4728 | if (!(fm->flags & OFPUTIL_FF_HIDDEN_FIELDS) |
4729 | && !match_has_default_hidden_fields(&fm->match)) { | |
4730 | VLOG_WARN_RL(&rl, "%s: (add_flow) only internal flows can set " | |
4731 | "non-default values to hidden fields", ofproto->name); | |
4732 | return OFPERR_OFPBRC_EPERM; | |
adcf00ba AZ |
4733 | } |
4734 | ||
2c7ee524 JR |
4735 | if (!ofm->temp_rule) { |
4736 | cls_rule_init(&cr, &fm->match, fm->priority); | |
8037acb4 | 4737 | |
2c7ee524 JR |
4738 | /* Allocate new rule. Destroys 'cr'. */ |
4739 | error = ofproto_rule_create(ofproto, &cr, table - ofproto->tables, | |
4740 | fm->new_cookie, fm->idle_timeout, | |
4741 | fm->hard_timeout, fm->flags, | |
4742 | fm->importance, fm->ofpacts, | |
4743 | fm->ofpacts_len, &ofm->temp_rule); | |
4744 | if (error) { | |
4745 | return error; | |
4746 | } | |
5bacd5cd | 4747 | |
2c7ee524 JR |
4748 | get_conjunctions(fm, &ofm->conjs, &ofm->n_conjs); |
4749 | } | |
5bacd5cd JR |
4750 | return 0; |
4751 | } | |
4752 | ||
2c7ee524 | 4753 | /* ofm->temp_rule is consumed only in the successful case. */ |
5bacd5cd JR |
4754 | static enum ofperr |
4755 | add_flow_start(struct ofproto *ofproto, struct ofproto_flow_mod *ofm) | |
4756 | OVS_REQUIRES(ofproto_mutex) | |
4757 | { | |
4758 | struct rule *old_rule = NULL; | |
4759 | struct rule *new_rule = ofm->temp_rule; | |
4760 | const struct rule_actions *actions = rule_get_actions(new_rule); | |
4761 | struct oftable *table = &ofproto->tables[new_rule->table_id]; | |
4762 | enum ofperr error; | |
4763 | ||
4764 | /* Must check actions while holding ofproto_mutex to avoid a race. */ | |
4765 | error = ofproto_check_ofpacts(ofproto, actions->ofpacts, | |
4766 | actions->ofpacts_len); | |
4767 | if (error) { | |
4768 | return error; | |
4769 | } | |
4770 | ||
1f42be1c | 4771 | /* Check for the existence of an identical rule. |
2b7b1427 | 4772 | * This will not return rules earlier marked for removal. */ |
5bacd5cd JR |
4773 | old_rule = rule_from_cls_rule(classifier_find_rule_exactly(&table->cls, |
4774 | &new_rule->cr, | |
4775 | ofm->version)); | |
4776 | if (!old_rule) { | |
c84d8691 | 4777 | /* Check for overlap, if requested. */ |
5bacd5cd JR |
4778 | if (new_rule->flags & OFPUTIL_FF_CHECK_OVERLAP |
4779 | && classifier_rule_overlaps(&table->cls, &new_rule->cr, | |
4780 | ofm->version)) { | |
c84d8691 | 4781 | return OFPERR_OFPFMFC_OVERLAP; |
dd27be82 | 4782 | } |
b277c7cc | 4783 | |
c84d8691 | 4784 | /* If necessary, evict an existing rule to clear out space. */ |
6787a49f | 4785 | if (table->n_flows >= table->max_flows) { |
5bacd5cd JR |
4786 | if (!choose_rule_to_evict(table, &old_rule)) { |
4787 | return OFPERR_OFPFMFC_TABLE_FULL; | |
6787a49f | 4788 | } |
5bacd5cd JR |
4789 | eviction_group_remove_rule(old_rule); |
4790 | /* Marks 'old_rule' as an evicted rule rather than replaced rule. | |
6787a49f | 4791 | */ |
5bacd5cd | 4792 | old_rule->removed_reason = OFPRR_EVICTION; |
c09f755d | 4793 | } |
39c94593 | 4794 | } else { |
7338102b | 4795 | ofm->modify_cookie = true; |
39c94593 | 4796 | } |
81a76618 | 4797 | |
5bacd5cd JR |
4798 | if (old_rule) { |
4799 | rule_collection_add(&ofm->old_rules, old_rule); | |
064af421 | 4800 | } |
5bacd5cd | 4801 | /* Take ownership of the temp_rule. */ |
58026109 | 4802 | rule_collection_add(&ofm->new_rules, new_rule); |
5bacd5cd | 4803 | ofm->temp_rule = NULL; |
8037acb4 | 4804 | |
5bacd5cd | 4805 | replace_rule_start(ofproto, ofm, old_rule, new_rule); |
c84d8691 JR |
4806 | return 0; |
4807 | } | |
1ebeaaa7 | 4808 | |
6787a49f | 4809 | /* Revert the effects of add_flow_start(). */ |
1f42be1c | 4810 | static void |
8be00367 | 4811 | add_flow_revert(struct ofproto *ofproto, struct ofproto_flow_mod *ofm) |
1f42be1c JR |
4812 | OVS_REQUIRES(ofproto_mutex) |
4813 | { | |
5bacd5cd | 4814 | struct rule *old_rule = rule_collection_n(&ofm->old_rules) |
58026109 JR |
4815 | ? rule_collection_rules(&ofm->old_rules)[0] : NULL; |
4816 | struct rule *new_rule = rule_collection_rules(&ofm->new_rules)[0]; | |
8be00367 | 4817 | |
39c94593 | 4818 | replace_rule_revert(ofproto, old_rule, new_rule); |
1f42be1c JR |
4819 | } |
4820 | ||
39c94593 | 4821 | /* To be called after version bump. */ |
c84d8691 | 4822 | static void |
8be00367 | 4823 | add_flow_finish(struct ofproto *ofproto, struct ofproto_flow_mod *ofm, |
0a0d9385 | 4824 | const struct openflow_mod_requester *req) |
c84d8691 JR |
4825 | OVS_REQUIRES(ofproto_mutex) |
4826 | { | |
5bacd5cd | 4827 | struct rule *old_rule = rule_collection_n(&ofm->old_rules) |
58026109 JR |
4828 | ? rule_collection_rules(&ofm->old_rules)[0] : NULL; |
4829 | struct rule *new_rule = rule_collection_rules(&ofm->new_rules)[0]; | |
39c94593 | 4830 | struct ovs_list dead_cookies = OVS_LIST_INITIALIZER(&dead_cookies); |
8037acb4 | 4831 | |
81dee635 | 4832 | replace_rule_finish(ofproto, ofm, req, old_rule, new_rule, &dead_cookies); |
39c94593 | 4833 | learned_cookies_flush(ofproto, &dead_cookies); |
802f84ff | 4834 | |
39c94593 JR |
4835 | if (old_rule) { |
4836 | ovsrcu_postpone(remove_rule_rcu, old_rule); | |
4837 | } else { | |
39c94593 | 4838 | ofmonitor_report(ofproto->connmgr, new_rule, NXFME_ADDED, 0, |
c84d8691 JR |
4839 | req ? req->ofconn : NULL, |
4840 | req ? req->request->xid : 0, NULL); | |
6c6eedc5 SJ |
4841 | |
4842 | /* Send Vacancy Events for OF1.4+. */ | |
4843 | send_table_status(ofproto, new_rule->table_id); | |
c84d8691 | 4844 | } |
c84d8691 | 4845 | } |
79eee1eb BP |
4846 | \f |
4847 | /* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */ | |
064af421 | 4848 | |
5bacd5cd | 4849 | /* Create a new rule. Note that the rule is NOT inserted into a any data |
2c7ee524 JR |
4850 | * structures yet. Takes ownership of 'cr'. Only assigns '*new_rule' if |
4851 | * successful. */ | |
90bf1e07 | 4852 | static enum ofperr |
5bacd5cd JR |
4853 | ofproto_rule_create(struct ofproto *ofproto, struct cls_rule *cr, |
4854 | uint8_t table_id, ovs_be64 new_cookie, | |
4855 | uint16_t idle_timeout, uint16_t hard_timeout, | |
4856 | enum ofputil_flow_mod_flags flags, uint16_t importance, | |
4857 | const struct ofpact *ofpacts, size_t ofpacts_len, | |
4858 | struct rule **new_rule) | |
4859 | OVS_NO_THREAD_SAFETY_ANALYSIS | |
79eee1eb | 4860 | { |
39c94593 | 4861 | struct rule *rule; |
dd27be82 | 4862 | enum ofperr error; |
79eee1eb | 4863 | |
39c94593 JR |
4864 | /* Allocate new rule. */ |
4865 | rule = ofproto->ofproto_class->rule_alloc(); | |
4866 | if (!rule) { | |
4867 | cls_rule_destroy(cr); | |
4868 | VLOG_WARN_RL(&rl, "%s: failed to allocate a rule.", ofproto->name); | |
4869 | return OFPERR_OFPFMFC_UNKNOWN; | |
af822017 BP |
4870 | } |
4871 | ||
39c94593 JR |
4872 | /* Initialize base state. */ |
4873 | *CONST_CAST(struct ofproto **, &rule->ofproto) = ofproto; | |
4874 | cls_rule_move(CONST_CAST(struct cls_rule *, &rule->cr), cr); | |
4875 | ovs_refcount_init(&rule->ref_count); | |
834fe5cb | 4876 | |
39c94593 JR |
4877 | ovs_mutex_init(&rule->mutex); |
4878 | ovs_mutex_lock(&rule->mutex); | |
82a3160e | 4879 | *CONST_CAST(ovs_be64 *, &rule->flow_cookie) = new_cookie; |
5bacd5cd JR |
4880 | rule->created = rule->modified = time_msec(); |
4881 | rule->idle_timeout = idle_timeout; | |
4882 | rule->hard_timeout = hard_timeout; | |
4883 | *CONST_CAST(uint16_t *, &rule->importance) = importance; | |
f695ebfa | 4884 | rule->removed_reason = OVS_OFPRR_NONE; |
834fe5cb | 4885 | |
39c94593 | 4886 | *CONST_CAST(uint8_t *, &rule->table_id) = table_id; |
5bacd5cd JR |
4887 | rule->flags = flags & OFPUTIL_FF_STATE; |
4888 | ||
39c94593 | 4889 | *CONST_CAST(const struct rule_actions **, &rule->actions) |
5bacd5cd JR |
4890 | = rule_actions_create(ofpacts, ofpacts_len); |
4891 | ||
417e7e66 | 4892 | ovs_list_init(&rule->meter_list_node); |
39c94593 | 4893 | rule->eviction_group = NULL; |
39c94593 JR |
4894 | rule->monitor_flags = 0; |
4895 | rule->add_seqno = 0; | |
4896 | rule->modify_seqno = 0; | |
5bacd5cd | 4897 | ovs_list_init(&rule->expirable); |
dd27be82 JR |
4898 | ovs_mutex_unlock(&rule->mutex); |
4899 | ||
39c94593 JR |
4900 | /* Construct rule, initializing derived state. */ |
4901 | error = ofproto->ofproto_class->rule_construct(rule); | |
4902 | if (error) { | |
4903 | ofproto_rule_destroy__(rule); | |
4904 | return error; | |
dd27be82 | 4905 | } |
b277c7cc | 4906 | |
30d0452c | 4907 | rule->state = RULE_INITIALIZED; |
dd27be82 | 4908 | |
39c94593 JR |
4909 | *new_rule = rule; |
4910 | return 0; | |
4911 | } | |
884e1dc4 | 4912 | |
2c7ee524 JR |
4913 | /* Initialize 'ofm' for a learn action. If the rule already existed, reference |
4914 | * to that rule is taken, otherwise a new rule is created. 'ofm' keeps the | |
4915 | * rule reference in both. This does not take the global 'ofproto_mutex'. */ | |
4916 | enum ofperr | |
4917 | ofproto_flow_mod_init_for_learn(struct ofproto *ofproto, | |
4918 | const struct ofputil_flow_mod *fm, | |
4919 | struct ofproto_flow_mod *ofm) | |
4920 | OVS_EXCLUDED(ofproto_mutex) | |
4921 | { | |
4922 | enum ofperr error; | |
4923 | ||
4924 | /* Reject flow mods that do not look like they were generated by a learn | |
4925 | * action. */ | |
4926 | if (fm->command != OFPFC_MODIFY_STRICT || fm->table_id == OFPTT_ALL | |
4927 | || fm->flags & OFPUTIL_FF_RESET_COUNTS | |
4928 | || fm->buffer_id != UINT32_MAX) { | |
4929 | return OFPERR_OFPFMFC_UNKNOWN; | |
4930 | } | |
4931 | ||
4932 | /* Check if the rule already exists, and we can get a reference to it. */ | |
4933 | struct oftable *table = &ofproto->tables[fm->table_id]; | |
4934 | struct rule *rule; | |
4935 | ||
4936 | rule = rule_from_cls_rule(classifier_find_match_exactly( | |
4937 | &table->cls, &fm->match, fm->priority, | |
4938 | OVS_VERSION_MAX)); | |
4939 | if (rule) { | |
4940 | /* Check if the rule's attributes match as well. */ | |
4941 | const struct rule_actions *actions; | |
4942 | ||
4943 | ovs_mutex_lock(&rule->mutex); | |
4944 | actions = rule_get_actions(rule); | |
4945 | if (rule->idle_timeout == fm->idle_timeout | |
4946 | && rule->hard_timeout == fm->hard_timeout | |
4947 | && rule->importance == fm->importance | |
4948 | && rule->flags == (fm->flags & OFPUTIL_FF_STATE) | |
4949 | && (!fm->modify_cookie || (fm->new_cookie == rule->flow_cookie)) | |
4950 | && ofpacts_equal(fm->ofpacts, fm->ofpacts_len, | |
4951 | actions->ofpacts, actions->ofpacts_len)) { | |
4952 | /* Rule already exists and need not change, except for the modified | |
4953 | * timestamp. Get a reference to the existing rule. */ | |
4954 | ovs_mutex_unlock(&rule->mutex); | |
4955 | if (!ofproto_rule_try_ref(rule)) { | |
4956 | rule = NULL; /* Pretend it did not exist. */ | |
4957 | } | |
4958 | } else { | |
4959 | ovs_mutex_unlock(&rule->mutex); | |
4960 | rule = NULL; | |
4961 | } | |
4962 | } | |
4963 | ||
4964 | /* Initialize ofproto_flow_mod for future use. */ | |
4965 | error = ofproto_flow_mod_init(ofproto, ofm, fm, rule); | |
4966 | if (error) { | |
4967 | ofproto_rule_unref(rule); | |
4968 | return error; | |
4969 | } | |
4970 | return 0; | |
4971 | } | |
4972 | ||
2c7ee524 | 4973 | enum ofperr |
1f4a8933 | 4974 | ofproto_flow_mod_learn_refresh(struct ofproto_flow_mod *ofm) |
2c7ee524 JR |
4975 | { |
4976 | enum ofperr error = 0; | |
4977 | ||
4978 | /* ofm->temp_rule is our reference to the learned rule. We have a | |
4979 | * reference to an existing rule, if it already was in the classifier, | |
4980 | * otherwise we may have a fresh rule that we need to insert. */ | |
4981 | struct rule *rule = ofm->temp_rule; | |
4982 | if (!rule) { | |
4983 | return OFPERR_OFPFMFC_UNKNOWN; | |
4984 | } | |
2c7ee524 JR |
4985 | |
4986 | /* Create a new rule if the current one has been removed from the | |
4987 | * classifier. We need to do this since RCU does not allow a current rule | |
4988 | * to be reinserted before all threads have quiesced. | |
4989 | * | |
4990 | * It is possible that the rule is removed asynchronously, e.g., right | |
1f4a8933 | 4991 | * after we have read the 'rule->state' below. In this case the next time |
2c7ee524 | 4992 | * this function is executed the rule will be reinstated. */ |
1f4a8933 | 4993 | if (rule->state == RULE_REMOVED) { |
2c7ee524 JR |
4994 | struct cls_rule cr; |
4995 | ||
4996 | cls_rule_clone(&cr, &rule->cr); | |
4997 | ovs_mutex_lock(&rule->mutex); | |
1f4a8933 | 4998 | error = ofproto_rule_create(rule->ofproto, &cr, rule->table_id, |
2c7ee524 JR |
4999 | rule->flow_cookie, |
5000 | rule->idle_timeout, | |
5001 | rule->hard_timeout, rule->flags, | |
5002 | rule->importance, | |
5003 | rule->actions->ofpacts, | |
5004 | rule->actions->ofpacts_len, | |
5005 | &ofm->temp_rule); | |
5006 | ovs_mutex_unlock(&rule->mutex); | |
2c7ee524 | 5007 | if (!error) { |
1f4a8933 | 5008 | ofproto_rule_unref(rule); /* Release old reference. */ |
2c7ee524 | 5009 | } |
2c7ee524 JR |
5010 | } else { |
5011 | /* Refresh the existing rule. */ | |
5012 | ovs_mutex_lock(&rule->mutex); | |
5013 | rule->modified = time_msec(); | |
5014 | ovs_mutex_unlock(&rule->mutex); | |
5015 | } | |
1f4a8933 JR |
5016 | return error; |
5017 | } | |
5018 | ||
5019 | enum ofperr | |
5020 | ofproto_flow_mod_learn_start(struct ofproto_flow_mod *ofm) | |
5021 | OVS_REQUIRES(ofproto_mutex) | |
5022 | { | |
5023 | struct rule *rule = ofm->temp_rule; | |
5024 | ||
5025 | /* ofproto_flow_mod_start() consumes the reference, so we | |
5026 | * take a new one. */ | |
5027 | ofproto_rule_ref(rule); | |
5028 | enum ofperr error = ofproto_flow_mod_start(rule->ofproto, ofm); | |
5029 | ofm->temp_rule = rule; | |
5030 | ||
5031 | return error; | |
5032 | } | |
5033 | ||
6dd3c787 JR |
5034 | void |
5035 | ofproto_flow_mod_learn_revert(struct ofproto_flow_mod *ofm) | |
5036 | OVS_REQUIRES(ofproto_mutex) | |
5037 | { | |
5038 | struct rule *rule = rule_collection_rules(&ofm->new_rules)[0]; | |
5039 | ofproto_flow_mod_revert(rule->ofproto, ofm); | |
5040 | } | |
5041 | ||
1f4a8933 JR |
5042 | void |
5043 | ofproto_flow_mod_learn_finish(struct ofproto_flow_mod *ofm, | |
5044 | struct ofproto *orig_ofproto) | |
5045 | OVS_REQUIRES(ofproto_mutex) | |
5046 | { | |
5047 | struct rule *rule = rule_collection_rules(&ofm->new_rules)[0]; | |
5048 | ||
5049 | /* If learning on a different bridge, must bump its version | |
5050 | * number and flush connmgr afterwards. */ | |
5051 | if (rule->ofproto != orig_ofproto) { | |
5052 | ofproto_bump_tables_version(rule->ofproto); | |
5053 | } | |
5054 | ofproto_flow_mod_finish(rule->ofproto, ofm, NULL); | |
5055 | if (rule->ofproto != orig_ofproto) { | |
5056 | ofmonitor_flush(rule->ofproto->connmgr); | |
5057 | } | |
5058 | } | |
5059 | ||
5060 | /* Refresh 'ofm->temp_rule', for which the caller holds a reference, if already | |
5061 | * in the classifier, insert it otherwise. If the rule has already been | |
5062 | * removed from the classifier, a new rule is created using 'ofm->temp_rule' as | |
5063 | * a template and the reference to the old 'ofm->temp_rule' is freed. If | |
5064 | * 'keep_ref' is true, then a reference to the current rule is held, otherwise | |
5065 | * it is released and 'ofm->temp_rule' is set to NULL. | |
5066 | * | |
5067 | * Caller needs to be the exclusive owner of 'ofm' as it is being manipulated | |
5068 | * during the call. */ | |
5069 | enum ofperr | |
5070 | ofproto_flow_mod_learn(struct ofproto_flow_mod *ofm, bool keep_ref) | |
5071 | OVS_EXCLUDED(ofproto_mutex) | |
5072 | { | |
5073 | enum ofperr error = ofproto_flow_mod_learn_refresh(ofm); | |
5074 | struct rule *rule = ofm->temp_rule; | |
5075 | ||
5076 | /* Do we need to insert the rule? */ | |
5077 | if (!error && rule->state == RULE_INITIALIZED) { | |
5078 | ovs_mutex_lock(&ofproto_mutex); | |
5079 | ofm->version = rule->ofproto->tables_version + 1; | |
5080 | error = ofproto_flow_mod_learn_start(ofm); | |
5081 | if (!error) { | |
5082 | ofproto_flow_mod_learn_finish(ofm, NULL); | |
5083 | } | |
5084 | ovs_mutex_unlock(&ofproto_mutex); | |
5085 | } | |
2c7ee524 | 5086 | |
2c7ee524 JR |
5087 | if (!keep_ref) { |
5088 | ofproto_rule_unref(rule); | |
5089 | ofm->temp_rule = NULL; | |
5090 | } | |
5091 | return error; | |
5092 | } | |
5093 | ||
39c94593 | 5094 | static void |
5bacd5cd JR |
5095 | replace_rule_start(struct ofproto *ofproto, struct ofproto_flow_mod *ofm, |
5096 | struct rule *old_rule, struct rule *new_rule) | |
39c94593 JR |
5097 | { |
5098 | struct oftable *table = &ofproto->tables[new_rule->table_id]; | |
834fe5cb | 5099 | |
6787a49f | 5100 | /* 'old_rule' may be either an evicted rule or replaced rule. */ |
39c94593 | 5101 | if (old_rule) { |
5bacd5cd JR |
5102 | /* Copy values from old rule for modify semantics. */ |
5103 | if (old_rule->removed_reason != OFPRR_EVICTION) { | |
5104 | bool change_cookie = (ofm->modify_cookie | |
5105 | && new_rule->flow_cookie != OVS_BE64_MAX | |
5106 | && new_rule->flow_cookie != old_rule->flow_cookie); | |
5107 | ||
5108 | ovs_mutex_lock(&new_rule->mutex); | |
5109 | ovs_mutex_lock(&old_rule->mutex); | |
5110 | if (ofm->command != OFPFC_ADD) { | |
5111 | new_rule->idle_timeout = old_rule->idle_timeout; | |
5112 | new_rule->hard_timeout = old_rule->hard_timeout; | |
5113 | *CONST_CAST(uint16_t *, &new_rule->importance) = old_rule->importance; | |
5114 | new_rule->flags = old_rule->flags; | |
5115 | new_rule->created = old_rule->created; | |
5116 | } | |
5117 | if (!change_cookie) { | |
82a3160e BP |
5118 | *CONST_CAST(ovs_be64 *, &new_rule->flow_cookie) |
5119 | = old_rule->flow_cookie; | |
5bacd5cd JR |
5120 | } |
5121 | ovs_mutex_unlock(&old_rule->mutex); | |
5122 | ovs_mutex_unlock(&new_rule->mutex); | |
5123 | } | |
5124 | ||
39c94593 | 5125 | /* Mark the old rule for removal in the next version. */ |
5bacd5cd | 5126 | cls_rule_make_invisible_in_version(&old_rule->cr, ofm->version); |
9bab38ff JR |
5127 | |
5128 | /* Remove the old rule from data structures. */ | |
5129 | ofproto_rule_remove__(ofproto, old_rule); | |
d79e3d70 JR |
5130 | } else { |
5131 | table->n_flows++; | |
dd27be82 | 5132 | } |
cc099268 JR |
5133 | /* Insert flow to ofproto data structures, so that later flow_mods may |
5134 | * relate to it. This is reversible, in case later errors require this to | |
39c94593 JR |
5135 | * be reverted. */ |
5136 | ofproto_rule_insert__(ofproto, new_rule); | |
5137 | /* Make the new rule visible for classifier lookups only from the next | |
5138 | * version. */ | |
5bacd5cd JR |
5139 | classifier_insert(&table->cls, &new_rule->cr, ofm->version, ofm->conjs, |
5140 | ofm->n_conjs); | |
39c94593 JR |
5141 | } |
5142 | ||
cc099268 JR |
5143 | static void |
5144 | replace_rule_revert(struct ofproto *ofproto, | |
5145 | struct rule *old_rule, struct rule *new_rule) | |
39c94593 JR |
5146 | { |
5147 | struct oftable *table = &ofproto->tables[new_rule->table_id]; | |
35f48b8b | 5148 | |
39c94593 | 5149 | if (old_rule) { |
5bacd5cd JR |
5150 | if (old_rule->removed_reason == OFPRR_EVICTION) { |
5151 | /* Revert the eviction. */ | |
5152 | eviction_group_add_rule(old_rule); | |
5153 | } | |
5154 | ||
9bab38ff JR |
5155 | /* Restore the old rule to data structures. */ |
5156 | ofproto_rule_insert__(ofproto, old_rule); | |
5157 | ||
d79e3d70 | 5158 | /* Restore the original visibility of the old rule. */ |
39c94593 | 5159 | cls_rule_restore_visibility(&old_rule->cr); |
d79e3d70 JR |
5160 | } else { |
5161 | /* Restore table's rule count. */ | |
5162 | table->n_flows--; | |
834fe5cb BP |
5163 | } |
5164 | ||
39c94593 JR |
5165 | /* Remove the new rule immediately. It was never visible to lookups. */ |
5166 | if (!classifier_remove(&table->cls, &new_rule->cr)) { | |
5167 | OVS_NOT_REACHED(); | |
5ecc9d81 | 5168 | } |
39c94593 | 5169 | ofproto_rule_remove__(ofproto, new_rule); |
39c94593 | 5170 | ofproto_rule_unref(new_rule); |
dd27be82 | 5171 | } |
79eee1eb | 5172 | |
39c94593 | 5173 | /* Adds the 'new_rule', replacing the 'old_rule'. */ |
dd27be82 | 5174 | static void |
81dee635 | 5175 | replace_rule_finish(struct ofproto *ofproto, struct ofproto_flow_mod *ofm, |
0a0d9385 | 5176 | const struct openflow_mod_requester *req, |
39c94593 JR |
5177 | struct rule *old_rule, struct rule *new_rule, |
5178 | struct ovs_list *dead_cookies) | |
dd27be82 JR |
5179 | OVS_REQUIRES(ofproto_mutex) |
5180 | { | |
6787a49f JR |
5181 | struct rule *replaced_rule; |
5182 | ||
5aacc3e2 JR |
5183 | replaced_rule = (old_rule && old_rule->removed_reason != OFPRR_EVICTION) |
5184 | ? old_rule : NULL; | |
dd27be82 | 5185 | |
6787a49f JR |
5186 | /* Insert the new flow to the ofproto provider. A non-NULL 'replaced_rule' |
5187 | * is a duplicate rule the 'new_rule' is replacing. The provider should | |
748eb2f5 | 5188 | * link the packet and byte counts from the old rule to the new one if |
8b6987d7 JR |
5189 | * 'modify_keep_counts' is 'true'. The 'replaced_rule' will be deleted |
5190 | * right after this call. */ | |
6787a49f | 5191 | ofproto->ofproto_class->rule_insert(new_rule, replaced_rule, |
8b6987d7 | 5192 | ofm->modify_keep_counts); |
39c94593 JR |
5193 | learned_cookies_inc(ofproto, rule_get_actions(new_rule)); |
5194 | ||
5195 | if (old_rule) { | |
5196 | const struct rule_actions *old_actions = rule_get_actions(old_rule); | |
81dee635 | 5197 | const struct rule_actions *new_actions = rule_get_actions(new_rule); |
39c94593 | 5198 | |
39c94593 JR |
5199 | learned_cookies_dec(ofproto, old_actions, dead_cookies); |
5200 | ||
6787a49f | 5201 | if (replaced_rule) { |
5bacd5cd JR |
5202 | enum nx_flow_update_event event = ofm->command == OFPFC_ADD |
5203 | ? NXFME_ADDED : NXFME_MODIFIED; | |
5204 | ||
81dee635 JR |
5205 | bool changed_cookie = (new_rule->flow_cookie |
5206 | != old_rule->flow_cookie); | |
6787a49f | 5207 | |
81dee635 JR |
5208 | bool changed_actions = !ofpacts_equal(new_actions->ofpacts, |
5209 | new_actions->ofpacts_len, | |
5210 | old_actions->ofpacts, | |
5211 | old_actions->ofpacts_len); | |
6787a49f | 5212 | |
5bacd5cd | 5213 | if (event != NXFME_MODIFIED || changed_actions |
81dee635 | 5214 | || changed_cookie) { |
5bacd5cd | 5215 | ofmonitor_report(ofproto->connmgr, new_rule, event, 0, |
6787a49f JR |
5216 | req ? req->ofconn : NULL, |
5217 | req ? req->request->xid : 0, | |
81dee635 | 5218 | changed_actions ? old_actions : NULL); |
6787a49f JR |
5219 | } |
5220 | } else { | |
5221 | /* XXX: This is slight duplication with delete_flows_finish__() */ | |
6787a49f JR |
5222 | ofmonitor_report(ofproto->connmgr, old_rule, NXFME_DELETED, |
5223 | OFPRR_EVICTION, | |
39c94593 | 5224 | req ? req->ofconn : NULL, |
6787a49f | 5225 | req ? req->request->xid : 0, NULL); |
39c94593 | 5226 | } |
dd27be82 | 5227 | } |
9ed18e46 BP |
5228 | } |
5229 | ||
2c7ee524 | 5230 | /* ofm->temp_rule is consumed only in the successful case. */ |
9387b970 | 5231 | static enum ofperr |
8be00367 | 5232 | modify_flows_start__(struct ofproto *ofproto, struct ofproto_flow_mod *ofm) |
dd27be82 JR |
5233 | OVS_REQUIRES(ofproto_mutex) |
5234 | { | |
8be00367 JR |
5235 | struct rule_collection *old_rules = &ofm->old_rules; |
5236 | struct rule_collection *new_rules = &ofm->new_rules; | |
dd27be82 JR |
5237 | enum ofperr error; |
5238 | ||
fe59694b | 5239 | if (rule_collection_n(old_rules) > 0) { |
39c94593 | 5240 | /* Create a new 'modified' rule for each old rule. */ |
5bacd5cd JR |
5241 | struct rule *old_rule, *new_rule; |
5242 | const struct rule_actions *actions = rule_get_actions(ofm->temp_rule); | |
39c94593 | 5243 | |
5bacd5cd JR |
5244 | /* Must check actions while holding ofproto_mutex to avoid a race. */ |
5245 | error = ofproto_check_ofpacts(ofproto, actions->ofpacts, | |
5246 | actions->ofpacts_len); | |
5247 | if (error) { | |
5248 | return error; | |
5249 | } | |
5250 | ||
5251 | /* Use the temp rule as the first new rule, and as the template for | |
5252 | * the rest. */ | |
5253 | struct rule *temp = ofm->temp_rule; | |
5254 | ofm->temp_rule = NULL; /* We consume the template. */ | |
5255 | ||
5256 | bool first = true; | |
5257 | RULE_COLLECTION_FOR_EACH (old_rule, old_rules) { | |
5258 | if (first) { | |
5259 | /* The template rule's match is possibly a loose one, so it | |
5260 | * must be replaced with the old rule's match so that the new | |
5261 | * rule actually replaces the old one. */ | |
5262 | cls_rule_destroy(CONST_CAST(struct cls_rule *, &temp->cr)); | |
5263 | cls_rule_clone(CONST_CAST(struct cls_rule *, &temp->cr), | |
5264 | &old_rule->cr); | |
5265 | *CONST_CAST(uint8_t *, &temp->table_id) = old_rule->table_id; | |
5266 | rule_collection_add(new_rules, temp); | |
5267 | first = false; | |
39c94593 | 5268 | } else { |
5bacd5cd JR |
5269 | struct cls_rule cr; |
5270 | cls_rule_clone(&cr, &old_rule->cr); | |
5271 | error = ofproto_rule_create(ofproto, &cr, old_rule->table_id, | |
5272 | temp->flow_cookie, | |
5273 | temp->idle_timeout, | |
5274 | temp->hard_timeout, temp->flags, | |
5275 | temp->importance, | |
5276 | temp->actions->ofpacts, | |
5277 | temp->actions->ofpacts_len, | |
5278 | &new_rule); | |
5279 | if (!error) { | |
5280 | rule_collection_add(new_rules, new_rule); | |
5281 | } else { | |
2c7ee524 JR |
5282 | /* Return the template rule in place in the error case. */ |
5283 | ofm->temp_rule = temp; | |
5284 | rule_collection_rules(new_rules)[0] = NULL; | |
5285 | ||
5bacd5cd JR |
5286 | rule_collection_unref(new_rules); |
5287 | rule_collection_destroy(new_rules); | |
5288 | return error; | |
5289 | } | |
39c94593 JR |
5290 | } |
5291 | } | |
fe59694b JR |
5292 | ovs_assert(rule_collection_n(new_rules) |
5293 | == rule_collection_n(old_rules)); | |
39c94593 | 5294 | |
fe59694b | 5295 | RULE_COLLECTIONS_FOR_EACH (old_rule, new_rule, old_rules, new_rules) { |
5bacd5cd | 5296 | replace_rule_start(ofproto, ofm, old_rule, new_rule); |
39c94593 | 5297 | } |
81dee635 | 5298 | } else if (ofm->modify_may_add_flow) { |
5bacd5cd | 5299 | /* No match, add a new flow, consumes 'temp'. */ |
8be00367 | 5300 | error = add_flow_start(ofproto, ofm); |
dd27be82 | 5301 | } else { |
2c7ee524 JR |
5302 | /* No flow to modify and may not add a flow. */ |
5303 | ofproto_rule_unref(ofm->temp_rule); | |
5304 | ofm->temp_rule = NULL; /* We consume the template. */ | |
d99f64d7 | 5305 | error = 0; |
dd27be82 | 5306 | } |
39c94593 | 5307 | |
dd27be82 JR |
5308 | return error; |
5309 | } | |
5310 | ||
5bacd5cd JR |
5311 | static enum ofperr |
5312 | modify_flows_init_loose(struct ofproto *ofproto, | |
5313 | struct ofproto_flow_mod *ofm, | |
5314 | const struct ofputil_flow_mod *fm) | |
5315 | OVS_EXCLUDED(ofproto_mutex) | |
5316 | { | |
5317 | rule_criteria_init(&ofm->criteria, fm->table_id, &fm->match, 0, | |
5318 | OVS_VERSION_MAX, fm->cookie, fm->cookie_mask, OFPP_ANY, | |
5319 | OFPG_ANY); | |
5320 | rule_criteria_require_rw(&ofm->criteria, | |
5321 | (fm->flags & OFPUTIL_FF_NO_READONLY) != 0); | |
5322 | /* Must create a new flow in advance for the case that no matches are | |
5323 | * found. Also used for template for multiple modified flows. */ | |
5324 | add_flow_init(ofproto, ofm, fm); | |
5325 | ||
5326 | return 0; | |
5327 | } | |
5328 | ||
90bf1e07 | 5329 | /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code on |
c184807c | 5330 | * failure. */ |
90bf1e07 | 5331 | static enum ofperr |
8be00367 | 5332 | modify_flows_start_loose(struct ofproto *ofproto, struct ofproto_flow_mod *ofm) |
15aaf599 | 5333 | OVS_REQUIRES(ofproto_mutex) |
9ed18e46 | 5334 | { |
8be00367 | 5335 | struct rule_collection *old_rules = &ofm->old_rules; |
d51c8b71 | 5336 | enum ofperr error; |
9ed18e46 | 5337 | |
5bacd5cd | 5338 | error = collect_rules_loose(ofproto, &ofm->criteria, old_rules); |
a9b22b7f | 5339 | |
a8e547c1 | 5340 | if (!error) { |
8be00367 | 5341 | error = modify_flows_start__(ofproto, ofm); |
d99f64d7 JR |
5342 | } |
5343 | ||
5344 | if (error) { | |
39c94593 | 5345 | rule_collection_destroy(old_rules); |
d99f64d7 | 5346 | } |
5bacd5cd | 5347 | |
d99f64d7 JR |
5348 | return error; |
5349 | } | |
5350 | ||
1f42be1c | 5351 | static void |
8be00367 | 5352 | modify_flows_revert(struct ofproto *ofproto, struct ofproto_flow_mod *ofm) |
1f42be1c JR |
5353 | OVS_REQUIRES(ofproto_mutex) |
5354 | { | |
8be00367 JR |
5355 | struct rule_collection *old_rules = &ofm->old_rules; |
5356 | struct rule_collection *new_rules = &ofm->new_rules; | |
5357 | ||
39c94593 | 5358 | /* Old rules were not changed yet, only need to revert new rules. */ |
5bacd5cd | 5359 | if (rule_collection_n(old_rules) > 0) { |
fe59694b JR |
5360 | struct rule *old_rule, *new_rule; |
5361 | RULE_COLLECTIONS_FOR_EACH (old_rule, new_rule, old_rules, new_rules) { | |
5362 | replace_rule_revert(ofproto, old_rule, new_rule); | |
39c94593 JR |
5363 | } |
5364 | rule_collection_destroy(new_rules); | |
5365 | rule_collection_destroy(old_rules); | |
1f42be1c | 5366 | } |
1f42be1c JR |
5367 | } |
5368 | ||
d99f64d7 | 5369 | static void |
8be00367 | 5370 | modify_flows_finish(struct ofproto *ofproto, struct ofproto_flow_mod *ofm, |
0a0d9385 | 5371 | const struct openflow_mod_requester *req) |
d99f64d7 JR |
5372 | OVS_REQUIRES(ofproto_mutex) |
5373 | { | |
8be00367 JR |
5374 | struct rule_collection *old_rules = &ofm->old_rules; |
5375 | struct rule_collection *new_rules = &ofm->new_rules; | |
5376 | ||
fe59694b JR |
5377 | if (rule_collection_n(old_rules) == 0 |
5378 | && rule_collection_n(new_rules) == 1) { | |
8be00367 | 5379 | add_flow_finish(ofproto, ofm, req); |
fe59694b | 5380 | } else if (rule_collection_n(old_rules) > 0) { |
39c94593 JR |
5381 | struct ovs_list dead_cookies = OVS_LIST_INITIALIZER(&dead_cookies); |
5382 | ||
fe59694b JR |
5383 | ovs_assert(rule_collection_n(new_rules) |
5384 | == rule_collection_n(old_rules)); | |
39c94593 | 5385 | |
fe59694b JR |
5386 | struct rule *old_rule, *new_rule; |
5387 | RULE_COLLECTIONS_FOR_EACH (old_rule, new_rule, old_rules, new_rules) { | |
81dee635 | 5388 | replace_rule_finish(ofproto, ofm, req, old_rule, new_rule, |
fe59694b | 5389 | &dead_cookies); |
39c94593 JR |
5390 | } |
5391 | learned_cookies_flush(ofproto, &dead_cookies); | |
cc099268 | 5392 | remove_rules_postponed(old_rules); |
d99f64d7 | 5393 | } |
d99f64d7 JR |
5394 | } |
5395 | ||
5bacd5cd JR |
5396 | static enum ofperr |
5397 | modify_flow_init_strict(struct ofproto *ofproto OVS_UNUSED, | |
5398 | struct ofproto_flow_mod *ofm, | |
5399 | const struct ofputil_flow_mod *fm) | |
5400 | OVS_EXCLUDED(ofproto_mutex) | |
5401 | { | |
5402 | rule_criteria_init(&ofm->criteria, fm->table_id, &fm->match, fm->priority, | |
5403 | OVS_VERSION_MAX, fm->cookie, fm->cookie_mask, OFPP_ANY, | |
5404 | OFPG_ANY); | |
5405 | rule_criteria_require_rw(&ofm->criteria, | |
5406 | (fm->flags & OFPUTIL_FF_NO_READONLY) != 0); | |
5407 | /* Must create a new flow in advance for the case that no matches are | |
5408 | * found. Also used for template for multiple modified flows. */ | |
5409 | add_flow_init(ofproto, ofm, fm); | |
5410 | ||
5411 | return 0; | |
5412 | } | |
5413 | ||
79eee1eb | 5414 | /* Implements OFPFC_MODIFY_STRICT. Returns 0 on success or an OpenFlow error |
baae3d02 | 5415 | * code on failure. */ |
90bf1e07 | 5416 | static enum ofperr |
8be00367 | 5417 | modify_flow_start_strict(struct ofproto *ofproto, struct ofproto_flow_mod *ofm) |
15aaf599 | 5418 | OVS_REQUIRES(ofproto_mutex) |
79eee1eb | 5419 | { |
8be00367 | 5420 | struct rule_collection *old_rules = &ofm->old_rules; |
d51c8b71 | 5421 | enum ofperr error; |
6c1491fb | 5422 | |
5bacd5cd | 5423 | error = collect_rules_strict(ofproto, &ofm->criteria, old_rules); |
a9b22b7f | 5424 | |
a8e547c1 | 5425 | if (!error) { |
dd27be82 | 5426 | /* collect_rules_strict() can return max 1 rule. */ |
8be00367 | 5427 | error = modify_flows_start__(ofproto, ofm); |
d99f64d7 JR |
5428 | } |
5429 | ||
d99f64d7 JR |
5430 | return error; |
5431 | } | |
9ed18e46 BP |
5432 | \f |
5433 | /* OFPFC_DELETE implementation. */ | |
79eee1eb | 5434 | |
254750ce | 5435 | static void |
44e0c35d | 5436 | delete_flows_start__(struct ofproto *ofproto, ovs_version_t version, |
39c94593 JR |
5437 | const struct rule_collection *rules) |
5438 | OVS_REQUIRES(ofproto_mutex) | |
5439 | { | |
fe59694b JR |
5440 | struct rule *rule; |
5441 | ||
5442 | RULE_COLLECTION_FOR_EACH (rule, rules) { | |
d79e3d70 JR |
5443 | struct oftable *table = &ofproto->tables[rule->table_id]; |
5444 | ||
5445 | table->n_flows--; | |
8be00367 | 5446 | cls_rule_make_invisible_in_version(&rule->cr, version); |
9bab38ff JR |
5447 | |
5448 | /* Remove rule from ofproto data structures. */ | |
5449 | ofproto_rule_remove__(ofproto, rule); | |
39c94593 JR |
5450 | } |
5451 | } | |
5452 | ||
25070e04 JR |
5453 | static void |
5454 | delete_flows_revert__(struct ofproto *ofproto, | |
5455 | const struct rule_collection *rules) | |
5456 | OVS_REQUIRES(ofproto_mutex) | |
5457 | { | |
5458 | struct rule *rule; | |
5459 | ||
5460 | RULE_COLLECTION_FOR_EACH (rule, rules) { | |
5461 | struct oftable *table = &ofproto->tables[rule->table_id]; | |
5462 | ||
5463 | /* Add rule back to ofproto data structures. */ | |
5464 | ofproto_rule_insert__(ofproto, rule); | |
5465 | ||
5466 | /* Restore table's rule count. */ | |
5467 | table->n_flows++; | |
5468 | ||
5469 | /* Restore the original visibility of the rule. */ | |
5470 | cls_rule_restore_visibility(&rule->cr); | |
5471 | } | |
5472 | } | |
5473 | ||
39c94593 JR |
5474 | static void |
5475 | delete_flows_finish__(struct ofproto *ofproto, | |
5476 | struct rule_collection *rules, | |
5477 | enum ofp_flow_removed_reason reason, | |
0a0d9385 | 5478 | const struct openflow_mod_requester *req) |
15aaf599 | 5479 | OVS_REQUIRES(ofproto_mutex) |
064af421 | 5480 | { |
fe59694b | 5481 | if (rule_collection_n(rules)) { |
55951e15 | 5482 | struct ovs_list dead_cookies = OVS_LIST_INITIALIZER(&dead_cookies); |
fe59694b | 5483 | struct rule *rule; |
79eee1eb | 5484 | |
fe59694b | 5485 | RULE_COLLECTION_FOR_EACH (rule, rules) { |
f695ebfa JR |
5486 | /* This value will be used to send the flow removed message right |
5487 | * before the rule is actually destroyed. */ | |
5488 | rule->removed_reason = reason; | |
5489 | ||
9ca4a86f | 5490 | ofmonitor_report(ofproto->connmgr, rule, NXFME_DELETED, reason, |
c84d8691 JR |
5491 | req ? req->ofconn : NULL, |
5492 | req ? req->request->xid : 0, NULL); | |
6c6eedc5 SJ |
5493 | |
5494 | /* Send Vacancy Event for OF1.4+. */ | |
5495 | send_table_status(ofproto, rule->table_id); | |
5496 | ||
802f84ff JR |
5497 | learned_cookies_dec(ofproto, rule_get_actions(rule), |
5498 | &dead_cookies); | |
9ca4a86f | 5499 | } |
cc099268 | 5500 | remove_rules_postponed(rules); |
39c94593 | 5501 | |
35f48b8b | 5502 | learned_cookies_flush(ofproto, &dead_cookies); |
39c94593 JR |
5503 | } |
5504 | } | |
5505 | ||
5506 | /* Deletes the rules listed in 'rules'. | |
5507 | * The deleted rules will become invisible to the lookups in the next version. | |
5508 | * Destroys 'rules'. */ | |
5509 | static void | |
5510 | delete_flows__(struct rule_collection *rules, | |
5511 | enum ofp_flow_removed_reason reason, | |
0a0d9385 | 5512 | const struct openflow_mod_requester *req) |
39c94593 JR |
5513 | OVS_REQUIRES(ofproto_mutex) |
5514 | { | |
fe59694b JR |
5515 | if (rule_collection_n(rules)) { |
5516 | struct ofproto *ofproto = rule_collection_rules(rules)[0]->ofproto; | |
39c94593 | 5517 | |
8be00367 | 5518 | delete_flows_start__(ofproto, ofproto->tables_version + 1, rules); |
39c94593 JR |
5519 | ofproto_bump_tables_version(ofproto); |
5520 | delete_flows_finish__(ofproto, rules, reason, req); | |
9ca4a86f BP |
5521 | ofmonitor_flush(ofproto->connmgr); |
5522 | } | |
79eee1eb | 5523 | } |
79eee1eb | 5524 | |
5bacd5cd JR |
5525 | static enum ofperr |
5526 | delete_flows_init_loose(struct ofproto *ofproto OVS_UNUSED, | |
5527 | struct ofproto_flow_mod *ofm, | |
5528 | const struct ofputil_flow_mod *fm) | |
5529 | OVS_EXCLUDED(ofproto_mutex) | |
5530 | { | |
5531 | rule_criteria_init(&ofm->criteria, fm->table_id, &fm->match, 0, | |
5532 | OVS_VERSION_MAX, fm->cookie, fm->cookie_mask, | |
5533 | fm->out_port, fm->out_group); | |
5534 | rule_criteria_require_rw(&ofm->criteria, | |
5535 | (fm->flags & OFPUTIL_FF_NO_READONLY) != 0); | |
5536 | return 0; | |
5537 | } | |
5538 | ||
79eee1eb | 5539 | /* Implements OFPFC_DELETE. */ |
90bf1e07 | 5540 | static enum ofperr |
8be00367 | 5541 | delete_flows_start_loose(struct ofproto *ofproto, struct ofproto_flow_mod *ofm) |
15aaf599 | 5542 | OVS_REQUIRES(ofproto_mutex) |
79eee1eb | 5543 | { |
8be00367 | 5544 | struct rule_collection *rules = &ofm->old_rules; |
90bf1e07 | 5545 | enum ofperr error; |
064af421 | 5546 | |
5bacd5cd | 5547 | error = collect_rules_loose(ofproto, &ofm->criteria, rules); |
a9b22b7f | 5548 | |
802f84ff | 5549 | if (!error) { |
8be00367 | 5550 | delete_flows_start__(ofproto, ofm->version, rules); |
a8e547c1 | 5551 | } |
a8e547c1 BP |
5552 | |
5553 | return error; | |
064af421 BP |
5554 | } |
5555 | ||
ce59413f | 5556 | static void |
8be00367 | 5557 | delete_flows_revert(struct ofproto *ofproto, struct ofproto_flow_mod *ofm) |
ce59413f JR |
5558 | OVS_REQUIRES(ofproto_mutex) |
5559 | { | |
25070e04 | 5560 | delete_flows_revert__(ofproto, &ofm->old_rules); |
ce59413f JR |
5561 | } |
5562 | ||
1f42be1c | 5563 | static void |
2c7ee524 | 5564 | delete_flows_finish(struct ofproto *ofproto, struct ofproto_flow_mod *ofm, |
0a0d9385 | 5565 | const struct openflow_mod_requester *req) |
15aaf599 | 5566 | OVS_REQUIRES(ofproto_mutex) |
79eee1eb | 5567 | { |
5aacc3e2 | 5568 | delete_flows_finish__(ofproto, &ofm->old_rules, OFPRR_DELETE, req); |
ce59413f JR |
5569 | } |
5570 | ||
5bacd5cd JR |
5571 | static enum ofperr |
5572 | delete_flows_init_strict(struct ofproto *ofproto OVS_UNUSED, | |
5573 | struct ofproto_flow_mod *ofm, | |
5574 | const struct ofputil_flow_mod *fm) | |
5575 | OVS_EXCLUDED(ofproto_mutex) | |
5576 | { | |
5577 | rule_criteria_init(&ofm->criteria, fm->table_id, &fm->match, fm->priority, | |
5578 | OVS_VERSION_MAX, fm->cookie, fm->cookie_mask, | |
5579 | fm->out_port, fm->out_group); | |
5580 | rule_criteria_require_rw(&ofm->criteria, | |
5581 | (fm->flags & OFPUTIL_FF_NO_READONLY) != 0); | |
5582 | return 0; | |
5583 | } | |
5584 | ||
ce59413f JR |
5585 | /* Implements OFPFC_DELETE_STRICT. */ |
5586 | static enum ofperr | |
2c7ee524 | 5587 | delete_flow_start_strict(struct ofproto *ofproto, struct ofproto_flow_mod *ofm) |
ce59413f JR |
5588 | OVS_REQUIRES(ofproto_mutex) |
5589 | { | |
8be00367 | 5590 | struct rule_collection *rules = &ofm->old_rules; |
ce59413f JR |
5591 | enum ofperr error; |
5592 | ||
5bacd5cd | 5593 | error = collect_rules_strict(ofproto, &ofm->criteria, rules); |
a9b22b7f | 5594 | |
802f84ff | 5595 | if (!error) { |
8be00367 | 5596 | delete_flows_start__(ofproto, ofm->version, rules); |
ce59413f JR |
5597 | } |
5598 | ||
5599 | return error; | |
5600 | } | |
5601 | ||
f695ebfa | 5602 | /* This may only be called by rule_destroy_cb()! */ |
abe529af | 5603 | static void |
f695ebfa JR |
5604 | ofproto_rule_send_removed(struct rule *rule) |
5605 | OVS_EXCLUDED(ofproto_mutex) | |
abe529af BP |
5606 | { |
5607 | struct ofputil_flow_removed fr; | |
dc437090 | 5608 | long long int used; |
abe529af | 5609 | |
5cb7a798 | 5610 | minimatch_expand(&rule->cr.match, &fr.match); |
81a76618 | 5611 | fr.priority = rule->cr.priority; |
f695ebfa | 5612 | |
25010c68 JR |
5613 | /* Synchronize with connmgr_destroy() calls to prevent connmgr disappearing |
5614 | * while we use it. */ | |
f695ebfa | 5615 | ovs_mutex_lock(&ofproto_mutex); |
25010c68 JR |
5616 | struct connmgr *connmgr = rule->ofproto->connmgr; |
5617 | if (!connmgr) { | |
5618 | ovs_mutex_unlock(&ofproto_mutex); | |
5619 | return; | |
5620 | } | |
5621 | ||
abe529af | 5622 | fr.cookie = rule->flow_cookie; |
f695ebfa | 5623 | fr.reason = rule->removed_reason; |
95216219 | 5624 | fr.table_id = rule->table_id; |
65e0be10 BP |
5625 | calc_duration(rule->created, time_msec(), |
5626 | &fr.duration_sec, &fr.duration_nsec); | |
d10976b7 | 5627 | ovs_mutex_lock(&rule->mutex); |
abe529af | 5628 | fr.idle_timeout = rule->idle_timeout; |
fa2bad0f | 5629 | fr.hard_timeout = rule->hard_timeout; |
d10976b7 | 5630 | ovs_mutex_unlock(&rule->mutex); |
abe529af | 5631 | rule->ofproto->ofproto_class->rule_get_stats(rule, &fr.packet_count, |
dc437090 | 5632 | &fr.byte_count, &used); |
25010c68 | 5633 | connmgr_send_flow_removed(connmgr, &fr); |
f695ebfa | 5634 | ovs_mutex_unlock(&ofproto_mutex); |
abe529af BP |
5635 | } |
5636 | ||
5637 | /* Sends an OpenFlow "flow removed" message with the given 'reason' (either | |
5638 | * OFPRR_HARD_TIMEOUT or OFPRR_IDLE_TIMEOUT), and then removes 'rule' from its | |
5639 | * ofproto. | |
5640 | * | |
5641 | * ofproto implementation ->run() functions should use this function to expire | |
5642 | * OpenFlow flows. */ | |
5643 | void | |
5644 | ofproto_rule_expire(struct rule *rule, uint8_t reason) | |
15aaf599 | 5645 | OVS_REQUIRES(ofproto_mutex) |
abe529af | 5646 | { |
802f84ff JR |
5647 | struct rule_collection rules; |
5648 | ||
fe59694b JR |
5649 | rule_collection_init(&rules); |
5650 | rule_collection_add(&rules, rule); | |
802f84ff | 5651 | delete_flows__(&rules, reason, NULL); |
79eee1eb | 5652 | } |
994c9973 BP |
5653 | |
5654 | /* Reduces '*timeout' to no more than 'max'. A value of zero in either case | |
5655 | * means "infinite". */ | |
5656 | static void | |
5657 | reduce_timeout(uint16_t max, uint16_t *timeout) | |
5658 | { | |
5659 | if (max && (!*timeout || *timeout > max)) { | |
5660 | *timeout = max; | |
5661 | } | |
5662 | } | |
5663 | ||
5664 | /* If 'idle_timeout' is nonzero, and 'rule' has no idle timeout or an idle | |
5665 | * timeout greater than 'idle_timeout', lowers 'rule''s idle timeout to | |
5666 | * 'idle_timeout' seconds. Similarly for 'hard_timeout'. | |
5667 | * | |
5668 | * Suitable for implementing OFPACT_FIN_TIMEOUT. */ | |
1f4a8933 JR |
5669 | void |
5670 | ofproto_rule_reduce_timeouts__(struct rule *rule, | |
5671 | uint16_t idle_timeout, uint16_t hard_timeout) | |
5672 | OVS_REQUIRES(ofproto_mutex) | |
5673 | OVS_EXCLUDED(rule->mutex) | |
5674 | { | |
5675 | if (!idle_timeout && !hard_timeout) { | |
5676 | return; | |
5677 | } | |
5678 | ||
5679 | if (ovs_list_is_empty(&rule->expirable)) { | |
5680 | ovs_list_insert(&rule->ofproto->expirable, &rule->expirable); | |
5681 | } | |
5682 | ||
5683 | ovs_mutex_lock(&rule->mutex); | |
5684 | reduce_timeout(idle_timeout, &rule->idle_timeout); | |
5685 | reduce_timeout(hard_timeout, &rule->hard_timeout); | |
5686 | ovs_mutex_unlock(&rule->mutex); | |
5687 | } | |
5688 | ||
994c9973 BP |
5689 | void |
5690 | ofproto_rule_reduce_timeouts(struct rule *rule, | |
5691 | uint16_t idle_timeout, uint16_t hard_timeout) | |
d10976b7 | 5692 | OVS_EXCLUDED(ofproto_mutex, rule->mutex) |
994c9973 BP |
5693 | { |
5694 | if (!idle_timeout && !hard_timeout) { | |
5695 | return; | |
5696 | } | |
5697 | ||
abe7b10f | 5698 | ovs_mutex_lock(&ofproto_mutex); |
417e7e66 BW |
5699 | if (ovs_list_is_empty(&rule->expirable)) { |
5700 | ovs_list_insert(&rule->ofproto->expirable, &rule->expirable); | |
994c9973 | 5701 | } |
abe7b10f | 5702 | ovs_mutex_unlock(&ofproto_mutex); |
994c9973 | 5703 | |
d10976b7 | 5704 | ovs_mutex_lock(&rule->mutex); |
994c9973 BP |
5705 | reduce_timeout(idle_timeout, &rule->idle_timeout); |
5706 | reduce_timeout(hard_timeout, &rule->hard_timeout); | |
d10976b7 | 5707 | ovs_mutex_unlock(&rule->mutex); |
994c9973 | 5708 | } |
79eee1eb | 5709 | \f |
90bf1e07 | 5710 | static enum ofperr |
2e4f5fcf | 5711 | handle_flow_mod(struct ofconn *ofconn, const struct ofp_header *oh) |
15aaf599 | 5712 | OVS_EXCLUDED(ofproto_mutex) |
064af421 | 5713 | { |
a5b8d268 | 5714 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
7338102b | 5715 | struct ofputil_flow_mod fm; |
f25d0cf3 BP |
5716 | uint64_t ofpacts_stub[1024 / 8]; |
5717 | struct ofpbuf ofpacts; | |
90bf1e07 | 5718 | enum ofperr error; |
064af421 | 5719 | |
76589937 | 5720 | error = reject_slave_controller(ofconn); |
9deba63b | 5721 | if (error) { |
b0d38b2f | 5722 | return error; |
9deba63b | 5723 | } |
3052b0c5 | 5724 | |
f25d0cf3 | 5725 | ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub); |
7338102b | 5726 | error = ofputil_decode_flow_mod(&fm, oh, ofconn_get_protocol(ofconn), |
04f48a68 YHW |
5727 | ofproto_get_tun_tab(ofproto), |
5728 | &ofproto->vl_mff_map, &ofpacts, | |
7e9f8266 BP |
5729 | u16_to_ofp(ofproto->max_ports), |
5730 | ofproto->n_tables); | |
548de4dd | 5731 | if (!error) { |
0a0d9385 | 5732 | struct openflow_mod_requester req = { ofconn, oh }; |
7338102b | 5733 | error = handle_flow_mod__(ofproto, &fm, &req); |
49bdc010 | 5734 | } |
2e310801 | 5735 | |
f25d0cf3 | 5736 | ofpbuf_uninit(&ofpacts); |
f25d0cf3 | 5737 | return error; |
75a75043 BP |
5738 | } |
5739 | ||
90bf1e07 | 5740 | static enum ofperr |
7338102b | 5741 | handle_flow_mod__(struct ofproto *ofproto, const struct ofputil_flow_mod *fm, |
0a0d9385 | 5742 | const struct openflow_mod_requester *req) |
15aaf599 | 5743 | OVS_EXCLUDED(ofproto_mutex) |
75a75043 | 5744 | { |
7338102b | 5745 | struct ofproto_flow_mod ofm; |
35412852 | 5746 | enum ofperr error; |
75a75043 | 5747 | |
2c7ee524 | 5748 | error = ofproto_flow_mod_init(ofproto, &ofm, fm, NULL); |
5bacd5cd JR |
5749 | if (error) { |
5750 | return error; | |
5751 | } | |
7338102b | 5752 | |
15aaf599 | 5753 | ovs_mutex_lock(&ofproto_mutex); |
7338102b JR |
5754 | ofm.version = ofproto->tables_version + 1; |
5755 | error = ofproto_flow_mod_start(ofproto, &ofm); | |
1f42be1c | 5756 | if (!error) { |
39c94593 | 5757 | ofproto_bump_tables_version(ofproto); |
7338102b | 5758 | ofproto_flow_mod_finish(ofproto, &ofm, req); |
2c7ee524 | 5759 | ofmonitor_flush(ofproto->connmgr); |
3052b0c5 | 5760 | } |
15aaf599 | 5761 | ovs_mutex_unlock(&ofproto_mutex); |
35412852 | 5762 | |
35412852 | 5763 | return error; |
3052b0c5 BP |
5764 | } |
5765 | ||
90bf1e07 | 5766 | static enum ofperr |
d1e2cf21 | 5767 | handle_role_request(struct ofconn *ofconn, const struct ofp_header *oh) |
9deba63b | 5768 | { |
f4f1ea7e BP |
5769 | struct ofputil_role_request request; |
5770 | struct ofputil_role_request reply; | |
9deba63b | 5771 | struct ofpbuf *buf; |
6ea4776b JR |
5772 | enum ofperr error; |
5773 | ||
f4f1ea7e | 5774 | error = ofputil_decode_role_message(oh, &request); |
6ea4776b JR |
5775 | if (error) { |
5776 | return error; | |
5777 | } | |
9deba63b | 5778 | |
f4f1ea7e | 5779 | if (request.role != OFPCR12_ROLE_NOCHANGE) { |
d26eda9f BP |
5780 | if (request.role != OFPCR12_ROLE_EQUAL |
5781 | && request.have_generation_id | |
f4f1ea7e BP |
5782 | && !ofconn_set_master_election_id(ofconn, request.generation_id)) { |
5783 | return OFPERR_OFPRRFC_STALE; | |
6ea4776b | 5784 | } |
6ea4776b | 5785 | |
f4f1ea7e BP |
5786 | ofconn_set_role(ofconn, request.role); |
5787 | } | |
9deba63b | 5788 | |
f4f1ea7e | 5789 | reply.role = ofconn_get_role(ofconn); |
147cc9d3 BP |
5790 | reply.have_generation_id = ofconn_get_master_election_id( |
5791 | ofconn, &reply.generation_id); | |
f4f1ea7e | 5792 | buf = ofputil_encode_role_reply(oh, &reply); |
b0421aa2 | 5793 | ofconn_send_reply(ofconn, buf); |
9deba63b BP |
5794 | |
5795 | return 0; | |
5796 | } | |
5797 | ||
90bf1e07 | 5798 | static enum ofperr |
6c1491fb BP |
5799 | handle_nxt_flow_mod_table_id(struct ofconn *ofconn, |
5800 | const struct ofp_header *oh) | |
5801 | { | |
982697a4 | 5802 | const struct nx_flow_mod_table_id *msg = ofpmsg_body(oh); |
27527aa0 BP |
5803 | enum ofputil_protocol cur, next; |
5804 | ||
5805 | cur = ofconn_get_protocol(ofconn); | |
5806 | next = ofputil_protocol_set_tid(cur, msg->set != 0); | |
5807 | ofconn_set_protocol(ofconn, next); | |
6c1491fb | 5808 | |
6c1491fb BP |
5809 | return 0; |
5810 | } | |
5811 | ||
90bf1e07 | 5812 | static enum ofperr |
d1e2cf21 | 5813 | handle_nxt_set_flow_format(struct ofconn *ofconn, const struct ofp_header *oh) |
09246b99 | 5814 | { |
982697a4 | 5815 | const struct nx_set_flow_format *msg = ofpmsg_body(oh); |
27527aa0 BP |
5816 | enum ofputil_protocol cur, next; |
5817 | enum ofputil_protocol next_base; | |
09246b99 | 5818 | |
27527aa0 BP |
5819 | next_base = ofputil_nx_flow_format_to_protocol(ntohl(msg->format)); |
5820 | if (!next_base) { | |
90bf1e07 | 5821 | return OFPERR_OFPBRC_EPERM; |
09246b99 | 5822 | } |
7ee20df1 | 5823 | |
27527aa0 BP |
5824 | cur = ofconn_get_protocol(ofconn); |
5825 | next = ofputil_protocol_set_base(cur, next_base); | |
27527aa0 | 5826 | ofconn_set_protocol(ofconn, next); |
b20f4073 | 5827 | |
7ee20df1 | 5828 | return 0; |
09246b99 BP |
5829 | } |
5830 | ||
90bf1e07 | 5831 | static enum ofperr |
54834960 EJ |
5832 | handle_nxt_set_packet_in_format(struct ofconn *ofconn, |
5833 | const struct ofp_header *oh) | |
5834 | { | |
982697a4 | 5835 | const struct nx_set_packet_in_format *msg = ofpmsg_body(oh); |
54834960 EJ |
5836 | uint32_t format; |
5837 | ||
54834960 | 5838 | format = ntohl(msg->format); |
6409e008 | 5839 | if (!ofputil_packet_in_format_is_valid(format)) { |
90bf1e07 | 5840 | return OFPERR_OFPBRC_EPERM; |
54834960 EJ |
5841 | } |
5842 | ||
54834960 EJ |
5843 | ofconn_set_packet_in_format(ofconn, format); |
5844 | return 0; | |
5845 | } | |
5846 | ||
80d5aefd BP |
5847 | static enum ofperr |
5848 | handle_nxt_set_async_config(struct ofconn *ofconn, const struct ofp_header *oh) | |
5849 | { | |
5876b4db BP |
5850 | struct ofputil_async_cfg basis = ofconn_get_async_config(ofconn); |
5851 | struct ofputil_async_cfg ac; | |
d18cc1ee | 5852 | enum ofperr error; |
80d5aefd | 5853 | |
5876b4db | 5854 | error = ofputil_decode_set_async_config(oh, false, &basis, &ac); |
d18cc1ee AA |
5855 | if (error) { |
5856 | return error; | |
5857 | } | |
80d5aefd | 5858 | |
a930d4c5 | 5859 | ofconn_set_async_config(ofconn, &ac); |
4550b647 MM |
5860 | if (ofconn_get_type(ofconn) == OFCONN_SERVICE && |
5861 | !ofconn_get_miss_send_len(ofconn)) { | |
5862 | ofconn_set_miss_send_len(ofconn, OFP_DEFAULT_MISS_SEND_LEN); | |
5863 | } | |
80d5aefd BP |
5864 | |
5865 | return 0; | |
5866 | } | |
5867 | ||
c423b3b3 AC |
5868 | static enum ofperr |
5869 | handle_nxt_get_async_request(struct ofconn *ofconn, const struct ofp_header *oh) | |
5870 | { | |
a930d4c5 | 5871 | struct ofputil_async_cfg ac = ofconn_get_async_config(ofconn); |
af7bc161 | 5872 | ofconn_send_reply(ofconn, ofputil_encode_get_async_reply(oh, &ac)); |
c423b3b3 AC |
5873 | |
5874 | return 0; | |
5875 | } | |
5876 | ||
a7349929 BP |
5877 | static enum ofperr |
5878 | handle_nxt_set_controller_id(struct ofconn *ofconn, | |
5879 | const struct ofp_header *oh) | |
5880 | { | |
982697a4 | 5881 | const struct nx_controller_id *nci = ofpmsg_body(oh); |
a7349929 | 5882 | |
a7349929 BP |
5883 | if (!is_all_zeros(nci->zero, sizeof nci->zero)) { |
5884 | return OFPERR_NXBRC_MUST_BE_ZERO; | |
5885 | } | |
5886 | ||
5887 | ofconn_set_controller_id(ofconn, ntohs(nci->controller_id)); | |
5888 | return 0; | |
5889 | } | |
5890 | ||
90bf1e07 | 5891 | static enum ofperr |
d1e2cf21 | 5892 | handle_barrier_request(struct ofconn *ofconn, const struct ofp_header *oh) |
246e61ea | 5893 | { |
246e61ea JP |
5894 | struct ofpbuf *buf; |
5895 | ||
982697a4 BP |
5896 | buf = ofpraw_alloc_reply((oh->version == OFP10_VERSION |
5897 | ? OFPRAW_OFPT10_BARRIER_REPLY | |
5898 | : OFPRAW_OFPT11_BARRIER_REPLY), oh, 0); | |
b0421aa2 | 5899 | ofconn_send_reply(ofconn, buf); |
246e61ea JP |
5900 | return 0; |
5901 | } | |
5902 | ||
2b07c8b1 BP |
5903 | static void |
5904 | ofproto_compose_flow_refresh_update(const struct rule *rule, | |
5905 | enum nx_flow_monitor_flags flags, | |
140f36ba DDP |
5906 | struct ovs_list *msgs, |
5907 | const struct tun_table *tun_table) | |
15aaf599 | 5908 | OVS_REQUIRES(ofproto_mutex) |
2b07c8b1 | 5909 | { |
6f00e29b | 5910 | const struct rule_actions *actions; |
2b07c8b1 BP |
5911 | struct ofputil_flow_update fu; |
5912 | ||
2b07c8b1 BP |
5913 | fu.event = (flags & (NXFMF_INITIAL | NXFMF_ADD) |
5914 | ? NXFME_ADDED : NXFME_MODIFIED); | |
5915 | fu.reason = 0; | |
d10976b7 | 5916 | ovs_mutex_lock(&rule->mutex); |
2b07c8b1 BP |
5917 | fu.idle_timeout = rule->idle_timeout; |
5918 | fu.hard_timeout = rule->hard_timeout; | |
d10976b7 | 5919 | ovs_mutex_unlock(&rule->mutex); |
2b07c8b1 BP |
5920 | fu.table_id = rule->table_id; |
5921 | fu.cookie = rule->flow_cookie; | |
140f36ba | 5922 | minimatch_expand(&rule->cr.match, &fu.match); |
6b140a4e | 5923 | fu.priority = rule->cr.priority; |
6f00e29b | 5924 | |
b20f4073 | 5925 | actions = flags & NXFMF_ACTIONS ? rule_get_actions(rule) : NULL; |
6f00e29b BP |
5926 | fu.ofpacts = actions ? actions->ofpacts : NULL; |
5927 | fu.ofpacts_len = actions ? actions->ofpacts_len : 0; | |
2b07c8b1 | 5928 | |
417e7e66 | 5929 | if (ovs_list_is_empty(msgs)) { |
2b07c8b1 BP |
5930 | ofputil_start_flow_update(msgs); |
5931 | } | |
140f36ba | 5932 | ofputil_append_flow_update(&fu, msgs, tun_table); |
2b07c8b1 BP |
5933 | } |
5934 | ||
5935 | void | |
a8e547c1 | 5936 | ofmonitor_compose_refresh_updates(struct rule_collection *rules, |
ca6ba700 | 5937 | struct ovs_list *msgs) |
15aaf599 | 5938 | OVS_REQUIRES(ofproto_mutex) |
2b07c8b1 | 5939 | { |
fe59694b | 5940 | struct rule *rule; |
2b07c8b1 | 5941 | |
fe59694b | 5942 | RULE_COLLECTION_FOR_EACH (rule, rules) { |
2b07c8b1 BP |
5943 | enum nx_flow_monitor_flags flags = rule->monitor_flags; |
5944 | rule->monitor_flags = 0; | |
5945 | ||
140f36ba DDP |
5946 | ofproto_compose_flow_refresh_update(rule, flags, msgs, |
5947 | ofproto_get_tun_tab(rule->ofproto)); | |
2b07c8b1 BP |
5948 | } |
5949 | } | |
5950 | ||
5951 | static void | |
5952 | ofproto_collect_ofmonitor_refresh_rule(const struct ofmonitor *m, | |
5953 | struct rule *rule, uint64_t seqno, | |
a8e547c1 | 5954 | struct rule_collection *rules) |
15aaf599 | 5955 | OVS_REQUIRES(ofproto_mutex) |
2b07c8b1 BP |
5956 | { |
5957 | enum nx_flow_monitor_flags update; | |
5958 | ||
5e8b38b0 | 5959 | if (rule_is_hidden(rule)) { |
2b07c8b1 BP |
5960 | return; |
5961 | } | |
5962 | ||
b20f4073 | 5963 | if (!ofproto_rule_has_out_port(rule, m->out_port)) { |
2b07c8b1 BP |
5964 | return; |
5965 | } | |
5966 | ||
5967 | if (seqno) { | |
5968 | if (rule->add_seqno > seqno) { | |
5969 | update = NXFMF_ADD | NXFMF_MODIFY; | |
5970 | } else if (rule->modify_seqno > seqno) { | |
5971 | update = NXFMF_MODIFY; | |
5972 | } else { | |
5973 | return; | |
5974 | } | |
5975 | ||
5976 | if (!(m->flags & update)) { | |
5977 | return; | |
5978 | } | |
5979 | } else { | |
5980 | update = NXFMF_INITIAL; | |
5981 | } | |
5982 | ||
5983 | if (!rule->monitor_flags) { | |
a8e547c1 | 5984 | rule_collection_add(rules, rule); |
2b07c8b1 BP |
5985 | } |
5986 | rule->monitor_flags |= update | (m->flags & NXFMF_ACTIONS); | |
5987 | } | |
5988 | ||
5989 | static void | |
5990 | ofproto_collect_ofmonitor_refresh_rules(const struct ofmonitor *m, | |
5991 | uint64_t seqno, | |
a8e547c1 | 5992 | struct rule_collection *rules) |
15aaf599 | 5993 | OVS_REQUIRES(ofproto_mutex) |
2b07c8b1 BP |
5994 | { |
5995 | const struct ofproto *ofproto = ofconn_get_ofproto(m->ofconn); | |
2b07c8b1 | 5996 | const struct oftable *table; |
81a76618 | 5997 | struct cls_rule target; |
2b07c8b1 | 5998 | |
bd53aa17 | 5999 | cls_rule_init_from_minimatch(&target, &m->match, 0); |
2b07c8b1 | 6000 | FOR_EACH_MATCHING_TABLE (table, m->table_id, ofproto) { |
2b07c8b1 BP |
6001 | struct rule *rule; |
6002 | ||
44e0c35d | 6003 | CLS_FOR_EACH_TARGET (rule, cr, &table->cls, &target, OVS_VERSION_MAX) { |
2b07c8b1 BP |
6004 | ofproto_collect_ofmonitor_refresh_rule(m, rule, seqno, rules); |
6005 | } | |
6006 | } | |
48d28ac1 | 6007 | cls_rule_destroy(&target); |
2b07c8b1 BP |
6008 | } |
6009 | ||
6010 | static void | |
6011 | ofproto_collect_ofmonitor_initial_rules(struct ofmonitor *m, | |
a8e547c1 | 6012 | struct rule_collection *rules) |
15aaf599 | 6013 | OVS_REQUIRES(ofproto_mutex) |
2b07c8b1 BP |
6014 | { |
6015 | if (m->flags & NXFMF_INITIAL) { | |
6016 | ofproto_collect_ofmonitor_refresh_rules(m, 0, rules); | |
6017 | } | |
6018 | } | |
6019 | ||
6020 | void | |
6021 | ofmonitor_collect_resume_rules(struct ofmonitor *m, | |
a8e547c1 | 6022 | uint64_t seqno, struct rule_collection *rules) |
15aaf599 | 6023 | OVS_REQUIRES(ofproto_mutex) |
2b07c8b1 BP |
6024 | { |
6025 | ofproto_collect_ofmonitor_refresh_rules(m, seqno, rules); | |
6026 | } | |
6027 | ||
7b900689 SH |
6028 | static enum ofperr |
6029 | flow_monitor_delete(struct ofconn *ofconn, uint32_t id) | |
6030 | OVS_REQUIRES(ofproto_mutex) | |
6031 | { | |
6032 | struct ofmonitor *m; | |
6033 | enum ofperr error; | |
6034 | ||
6035 | m = ofmonitor_lookup(ofconn, id); | |
6036 | if (m) { | |
6037 | ofmonitor_destroy(m); | |
6038 | error = 0; | |
6039 | } else { | |
6040 | error = OFPERR_OFPMOFC_UNKNOWN_MONITOR; | |
6041 | } | |
6042 | ||
6043 | return error; | |
6044 | } | |
6045 | ||
2b07c8b1 | 6046 | static enum ofperr |
982697a4 | 6047 | handle_flow_monitor_request(struct ofconn *ofconn, const struct ofp_header *oh) |
15aaf599 | 6048 | OVS_EXCLUDED(ofproto_mutex) |
2b07c8b1 BP |
6049 | { |
6050 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
2b07c8b1 | 6051 | |
0a2869d5 BP |
6052 | struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length)); |
6053 | ||
6054 | struct ofmonitor **monitors = NULL; | |
6055 | size_t allocated_monitors = 0; | |
6056 | size_t n_monitors = 0; | |
6057 | ||
6058 | enum ofperr error; | |
15aaf599 BP |
6059 | |
6060 | ovs_mutex_lock(&ofproto_mutex); | |
2b07c8b1 BP |
6061 | for (;;) { |
6062 | struct ofputil_flow_monitor_request request; | |
6063 | struct ofmonitor *m; | |
6064 | int retval; | |
6065 | ||
6066 | retval = ofputil_decode_flow_monitor_request(&request, &b); | |
6067 | if (retval == EOF) { | |
6068 | break; | |
6069 | } else if (retval) { | |
6070 | error = retval; | |
6071 | goto error; | |
6072 | } | |
6073 | ||
6074 | if (request.table_id != 0xff | |
6075 | && request.table_id >= ofproto->n_tables) { | |
6076 | error = OFPERR_OFPBRC_BAD_TABLE_ID; | |
6077 | goto error; | |
6078 | } | |
6079 | ||
6080 | error = ofmonitor_create(&request, ofconn, &m); | |
6081 | if (error) { | |
6082 | goto error; | |
6083 | } | |
6084 | ||
6085 | if (n_monitors >= allocated_monitors) { | |
6086 | monitors = x2nrealloc(monitors, &allocated_monitors, | |
6087 | sizeof *monitors); | |
6088 | } | |
6089 | monitors[n_monitors++] = m; | |
6090 | } | |
6091 | ||
0a2869d5 | 6092 | struct rule_collection rules; |
a8e547c1 | 6093 | rule_collection_init(&rules); |
0a2869d5 | 6094 | for (size_t i = 0; i < n_monitors; i++) { |
2b07c8b1 BP |
6095 | ofproto_collect_ofmonitor_initial_rules(monitors[i], &rules); |
6096 | } | |
6097 | ||
0a2869d5 | 6098 | struct ovs_list replies; |
982697a4 | 6099 | ofpmp_init(&replies, oh); |
2b07c8b1 | 6100 | ofmonitor_compose_refresh_updates(&rules, &replies); |
15aaf599 BP |
6101 | ovs_mutex_unlock(&ofproto_mutex); |
6102 | ||
a8e547c1 BP |
6103 | rule_collection_destroy(&rules); |
6104 | ||
2b07c8b1 | 6105 | ofconn_send_replies(ofconn, &replies); |
2b07c8b1 BP |
6106 | free(monitors); |
6107 | ||
6108 | return 0; | |
6109 | ||
6110 | error: | |
0a2869d5 | 6111 | for (size_t i = 0; i < n_monitors; i++) { |
2b07c8b1 BP |
6112 | ofmonitor_destroy(monitors[i]); |
6113 | } | |
6114 | free(monitors); | |
4cd1bc9d BP |
6115 | ovs_mutex_unlock(&ofproto_mutex); |
6116 | ||
2b07c8b1 BP |
6117 | return error; |
6118 | } | |
6119 | ||
6120 | static enum ofperr | |
6121 | handle_flow_monitor_cancel(struct ofconn *ofconn, const struct ofp_header *oh) | |
15aaf599 | 6122 | OVS_EXCLUDED(ofproto_mutex) |
2b07c8b1 | 6123 | { |
15aaf599 | 6124 | enum ofperr error; |
2b07c8b1 BP |
6125 | uint32_t id; |
6126 | ||
6127 | id = ofputil_decode_flow_monitor_cancel(oh); | |
15aaf599 BP |
6128 | |
6129 | ovs_mutex_lock(&ofproto_mutex); | |
7b900689 | 6130 | error = flow_monitor_delete(ofconn, id); |
15aaf599 | 6131 | ovs_mutex_unlock(&ofproto_mutex); |
2b07c8b1 | 6132 | |
15aaf599 | 6133 | return error; |
2b07c8b1 BP |
6134 | } |
6135 | ||
9cae45dc JR |
6136 | /* Meters implementation. |
6137 | * | |
6138 | * Meter table entry, indexed by the OpenFlow meter_id. | |
9cae45dc JR |
6139 | * 'created' is used to compute the duration for meter stats. |
6140 | * 'list rules' is needed so that we can delete the dependent rules when the | |
6141 | * meter table entry is deleted. | |
6142 | * 'provider_meter_id' is for the provider's private use. | |
6143 | */ | |
6144 | struct meter { | |
6145 | long long int created; /* Time created. */ | |
ca6ba700 | 6146 | struct ovs_list rules; /* List of "struct rule_dpif"s. */ |
9cae45dc JR |
6147 | ofproto_meter_id provider_meter_id; |
6148 | uint16_t flags; /* Meter flags. */ | |
6149 | uint16_t n_bands; /* Number of meter bands. */ | |
6150 | struct ofputil_meter_band *bands; | |
6151 | }; | |
6152 | ||
6153 | /* | |
076caa2f JR |
6154 | * This is used in instruction validation at flow set-up time, to map |
6155 | * the OpenFlow meter ID to the corresponding datapath provider meter | |
6156 | * ID. If either does not exist, returns false. Otherwise updates | |
6157 | * the meter action and returns true. | |
9cae45dc | 6158 | */ |
076caa2f JR |
6159 | static bool |
6160 | ofproto_fix_meter_action(const struct ofproto *ofproto, | |
6161 | struct ofpact_meter *ma) | |
6162 | { | |
6163 | if (ma->meter_id && ma->meter_id <= ofproto->meter_features.max_meters) { | |
6164 | const struct meter *meter = ofproto->meters[ma->meter_id]; | |
6165 | ||
6166 | if (meter && meter->provider_meter_id.uint32 != UINT32_MAX) { | |
6167 | /* Update the action with the provider's meter ID, so that we | |
6168 | * do not need any synchronization between ofproto_dpif_xlate | |
6169 | * and ofproto for meter table access. */ | |
6170 | ma->provider_meter_id = meter->provider_meter_id.uint32; | |
6171 | return true; | |
9cae45dc JR |
6172 | } |
6173 | } | |
076caa2f | 6174 | return false; |
9cae45dc JR |
6175 | } |
6176 | ||
062fea06 BP |
6177 | /* Finds the meter invoked by 'rule''s actions and adds 'rule' to the meter's |
6178 | * list of rules. */ | |
6179 | static void | |
6180 | meter_insert_rule(struct rule *rule) | |
6181 | { | |
6182 | const struct rule_actions *a = rule_get_actions(rule); | |
6183 | uint32_t meter_id = ofpacts_get_meter(a->ofpacts, a->ofpacts_len); | |
6184 | struct meter *meter = rule->ofproto->meters[meter_id]; | |
6185 | ||
417e7e66 | 6186 | ovs_list_insert(&meter->rules, &rule->meter_list_node); |
062fea06 BP |
6187 | } |
6188 | ||
9cae45dc JR |
6189 | static void |
6190 | meter_update(struct meter *meter, const struct ofputil_meter_config *config) | |
6191 | { | |
6192 | free(meter->bands); | |
6193 | ||
6194 | meter->flags = config->flags; | |
6195 | meter->n_bands = config->n_bands; | |
6196 | meter->bands = xmemdup(config->bands, | |
6197 | config->n_bands * sizeof *meter->bands); | |
6198 | } | |
6199 | ||
6200 | static struct meter * | |
6201 | meter_create(const struct ofputil_meter_config *config, | |
6202 | ofproto_meter_id provider_meter_id) | |
6203 | { | |
6204 | struct meter *meter; | |
6205 | ||
6206 | meter = xzalloc(sizeof *meter); | |
6207 | meter->provider_meter_id = provider_meter_id; | |
6208 | meter->created = time_msec(); | |
417e7e66 | 6209 | ovs_list_init(&meter->rules); |
9cae45dc JR |
6210 | |
6211 | meter_update(meter, config); | |
6212 | ||
6213 | return meter; | |
6214 | } | |
6215 | ||
6b3f7f02 JR |
6216 | static void |
6217 | meter_delete(struct ofproto *ofproto, uint32_t first, uint32_t last) | |
15aaf599 | 6218 | OVS_REQUIRES(ofproto_mutex) |
6b3f7f02 | 6219 | { |
3059d7e5 | 6220 | for (uint32_t mid = first; mid <= last; ++mid) { |
6b3f7f02 JR |
6221 | struct meter *meter = ofproto->meters[mid]; |
6222 | if (meter) { | |
3059d7e5 JR |
6223 | /* First delete the rules that use this meter. */ |
6224 | if (!ovs_list_is_empty(&meter->rules)) { | |
6225 | struct rule_collection rules; | |
6226 | struct rule *rule; | |
6227 | ||
6228 | rule_collection_init(&rules); | |
6229 | ||
6230 | LIST_FOR_EACH (rule, meter_list_node, &meter->rules) { | |
6231 | rule_collection_add(&rules, rule); | |
6232 | } | |
6233 | delete_flows__(&rules, OFPRR_METER_DELETE, NULL); | |
6234 | } | |
6235 | ||
6b3f7f02 JR |
6236 | ofproto->meters[mid] = NULL; |
6237 | ofproto->ofproto_class->meter_del(ofproto, | |
6238 | meter->provider_meter_id); | |
6239 | free(meter->bands); | |
6240 | free(meter); | |
6241 | } | |
6242 | } | |
6243 | } | |
6244 | ||
9cae45dc JR |
6245 | static enum ofperr |
6246 | handle_add_meter(struct ofproto *ofproto, struct ofputil_meter_mod *mm) | |
6247 | { | |
6248 | ofproto_meter_id provider_meter_id = { UINT32_MAX }; | |
6249 | struct meter **meterp = &ofproto->meters[mm->meter.meter_id]; | |
6250 | enum ofperr error; | |
6251 | ||
6252 | if (*meterp) { | |
6253 | return OFPERR_OFPMMFC_METER_EXISTS; | |
6254 | } | |
6255 | ||
6256 | error = ofproto->ofproto_class->meter_set(ofproto, &provider_meter_id, | |
6257 | &mm->meter); | |
6258 | if (!error) { | |
6259 | ovs_assert(provider_meter_id.uint32 != UINT32_MAX); | |
6260 | *meterp = meter_create(&mm->meter, provider_meter_id); | |
6261 | } | |
f0f8c6c2 | 6262 | return error; |
9cae45dc JR |
6263 | } |
6264 | ||
6265 | static enum ofperr | |
6266 | handle_modify_meter(struct ofproto *ofproto, struct ofputil_meter_mod *mm) | |
6267 | { | |
6268 | struct meter *meter = ofproto->meters[mm->meter.meter_id]; | |
6269 | enum ofperr error; | |
e555eb7c | 6270 | uint32_t provider_meter_id; |
9cae45dc JR |
6271 | |
6272 | if (!meter) { | |
6273 | return OFPERR_OFPMMFC_UNKNOWN_METER; | |
6274 | } | |
6275 | ||
e555eb7c | 6276 | provider_meter_id = meter->provider_meter_id.uint32; |
9cae45dc JR |
6277 | error = ofproto->ofproto_class->meter_set(ofproto, |
6278 | &meter->provider_meter_id, | |
6279 | &mm->meter); | |
e555eb7c | 6280 | ovs_assert(meter->provider_meter_id.uint32 == provider_meter_id); |
9cae45dc JR |
6281 | if (!error) { |
6282 | meter_update(meter, &mm->meter); | |
6283 | } | |
6284 | return error; | |
6285 | } | |
6286 | ||
6287 | static enum ofperr | |
baae3d02 | 6288 | handle_delete_meter(struct ofconn *ofconn, struct ofputil_meter_mod *mm) |
15aaf599 | 6289 | OVS_EXCLUDED(ofproto_mutex) |
9cae45dc JR |
6290 | { |
6291 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
6292 | uint32_t meter_id = mm->meter.meter_id; | |
a8e547c1 | 6293 | enum ofperr error = 0; |
9cae45dc | 6294 | uint32_t first, last; |
9cae45dc JR |
6295 | |
6296 | if (meter_id == OFPM13_ALL) { | |
6297 | first = 1; | |
6298 | last = ofproto->meter_features.max_meters; | |
6299 | } else { | |
6300 | if (!meter_id || meter_id > ofproto->meter_features.max_meters) { | |
6301 | return 0; | |
6302 | } | |
6303 | first = last = meter_id; | |
6304 | } | |
6305 | ||
9cae45dc | 6306 | /* Delete the meters. */ |
3059d7e5 | 6307 | ovs_mutex_lock(&ofproto_mutex); |
6b3f7f02 | 6308 | meter_delete(ofproto, first, last); |
15aaf599 | 6309 | ovs_mutex_unlock(&ofproto_mutex); |
a8e547c1 BP |
6310 | |
6311 | return error; | |
9cae45dc JR |
6312 | } |
6313 | ||
6314 | static enum ofperr | |
6315 | handle_meter_mod(struct ofconn *ofconn, const struct ofp_header *oh) | |
6316 | { | |
6317 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
6318 | struct ofputil_meter_mod mm; | |
6319 | uint64_t bands_stub[256 / 8]; | |
6320 | struct ofpbuf bands; | |
6321 | uint32_t meter_id; | |
6322 | enum ofperr error; | |
6323 | ||
6324 | error = reject_slave_controller(ofconn); | |
6325 | if (error) { | |
6326 | return error; | |
6327 | } | |
6328 | ||
6329 | ofpbuf_use_stub(&bands, bands_stub, sizeof bands_stub); | |
6330 | ||
6331 | error = ofputil_decode_meter_mod(oh, &mm, &bands); | |
6332 | if (error) { | |
6333 | goto exit_free_bands; | |
6334 | } | |
6335 | ||
6336 | meter_id = mm.meter.meter_id; | |
6337 | ||
6338 | if (mm.command != OFPMC13_DELETE) { | |
6339 | /* Fails also when meters are not implemented by the provider. */ | |
b31e8700 | 6340 | if (meter_id == 0 || meter_id > OFPM13_MAX) { |
9cae45dc JR |
6341 | error = OFPERR_OFPMMFC_INVALID_METER; |
6342 | goto exit_free_bands; | |
b31e8700 JR |
6343 | } else if (meter_id > ofproto->meter_features.max_meters) { |
6344 | error = OFPERR_OFPMMFC_OUT_OF_METERS; | |
6345 | goto exit_free_bands; | |
9cae45dc JR |
6346 | } |
6347 | if (mm.meter.n_bands > ofproto->meter_features.max_bands) { | |
6348 | error = OFPERR_OFPMMFC_OUT_OF_BANDS; | |
6349 | goto exit_free_bands; | |
6350 | } | |
6351 | } | |
6352 | ||
6353 | switch (mm.command) { | |
6354 | case OFPMC13_ADD: | |
6355 | error = handle_add_meter(ofproto, &mm); | |
6356 | break; | |
6357 | ||
6358 | case OFPMC13_MODIFY: | |
6359 | error = handle_modify_meter(ofproto, &mm); | |
6360 | break; | |
6361 | ||
6362 | case OFPMC13_DELETE: | |
baae3d02 | 6363 | error = handle_delete_meter(ofconn, &mm); |
9cae45dc JR |
6364 | break; |
6365 | ||
6366 | default: | |
6367 | error = OFPERR_OFPMMFC_BAD_COMMAND; | |
6368 | break; | |
6369 | } | |
6370 | ||
3c35db62 NR |
6371 | if (!error) { |
6372 | struct ofputil_requestforward rf; | |
6373 | rf.xid = oh->xid; | |
6374 | rf.reason = OFPRFR_METER_MOD; | |
6375 | rf.meter_mod = &mm; | |
6376 | connmgr_send_requestforward(ofproto->connmgr, ofconn, &rf); | |
6377 | } | |
6378 | ||
9cae45dc JR |
6379 | exit_free_bands: |
6380 | ofpbuf_uninit(&bands); | |
6381 | return error; | |
6382 | } | |
6383 | ||
6384 | static enum ofperr | |
6385 | handle_meter_features_request(struct ofconn *ofconn, | |
6386 | const struct ofp_header *request) | |
6387 | { | |
6388 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
6389 | struct ofputil_meter_features features; | |
6390 | struct ofpbuf *b; | |
6391 | ||
6392 | if (ofproto->ofproto_class->meter_get_features) { | |
6393 | ofproto->ofproto_class->meter_get_features(ofproto, &features); | |
6394 | } else { | |
6395 | memset(&features, 0, sizeof features); | |
6396 | } | |
6397 | b = ofputil_encode_meter_features_reply(&features, request); | |
6398 | ||
6399 | ofconn_send_reply(ofconn, b); | |
6400 | return 0; | |
6401 | } | |
6402 | ||
6403 | static enum ofperr | |
6404 | handle_meter_request(struct ofconn *ofconn, const struct ofp_header *request, | |
6405 | enum ofptype type) | |
6406 | { | |
6407 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
ca6ba700 | 6408 | struct ovs_list replies; |
9cae45dc JR |
6409 | uint64_t bands_stub[256 / 8]; |
6410 | struct ofpbuf bands; | |
6411 | uint32_t meter_id, first, last; | |
6412 | ||
6413 | ofputil_decode_meter_request(request, &meter_id); | |
6414 | ||
6415 | if (meter_id == OFPM13_ALL) { | |
6416 | first = 1; | |
6417 | last = ofproto->meter_features.max_meters; | |
6418 | } else { | |
6419 | if (!meter_id || meter_id > ofproto->meter_features.max_meters || | |
6420 | !ofproto->meters[meter_id]) { | |
6421 | return OFPERR_OFPMMFC_UNKNOWN_METER; | |
6422 | } | |
6423 | first = last = meter_id; | |
6424 | } | |
6425 | ||
6426 | ofpbuf_use_stub(&bands, bands_stub, sizeof bands_stub); | |
6427 | ofpmp_init(&replies, request); | |
6428 | ||
6429 | for (meter_id = first; meter_id <= last; ++meter_id) { | |
6430 | struct meter *meter = ofproto->meters[meter_id]; | |
6431 | if (!meter) { | |
6432 | continue; /* Skip non-existing meters. */ | |
6433 | } | |
261bd854 | 6434 | if (type == OFPTYPE_METER_STATS_REQUEST) { |
9cae45dc JR |
6435 | struct ofputil_meter_stats stats; |
6436 | ||
6437 | stats.meter_id = meter_id; | |
6438 | ||
6439 | /* Provider sets the packet and byte counts, we do the rest. */ | |
417e7e66 | 6440 | stats.flow_count = ovs_list_size(&meter->rules); |
9cae45dc JR |
6441 | calc_duration(meter->created, time_msec(), |
6442 | &stats.duration_sec, &stats.duration_nsec); | |
6443 | stats.n_bands = meter->n_bands; | |
6444 | ofpbuf_clear(&bands); | |
6445 | stats.bands | |
6446 | = ofpbuf_put_uninit(&bands, | |
6447 | meter->n_bands * sizeof *stats.bands); | |
6448 | ||
6449 | if (!ofproto->ofproto_class->meter_get(ofproto, | |
6450 | meter->provider_meter_id, | |
5dddf960 | 6451 | &stats, meter->n_bands)) { |
9cae45dc JR |
6452 | ofputil_append_meter_stats(&replies, &stats); |
6453 | } | |
6454 | } else { /* type == OFPTYPE_METER_CONFIG_REQUEST */ | |
6455 | struct ofputil_meter_config config; | |
6456 | ||
6457 | config.meter_id = meter_id; | |
6458 | config.flags = meter->flags; | |
6459 | config.n_bands = meter->n_bands; | |
6460 | config.bands = meter->bands; | |
6461 | ofputil_append_meter_config(&replies, &config); | |
6462 | } | |
6463 | } | |
6464 | ||
6465 | ofconn_send_replies(ofconn, &replies); | |
6466 | ofpbuf_uninit(&bands); | |
6467 | return 0; | |
6468 | } | |
6469 | ||
db88b35c JR |
6470 | /* Returned group is RCU protected. */ |
6471 | static struct ofgroup * | |
5d08a275 JR |
6472 | ofproto_group_lookup__(const struct ofproto *ofproto, uint32_t group_id, |
6473 | ovs_version_t version) | |
db88b35c JR |
6474 | { |
6475 | struct ofgroup *group; | |
6476 | ||
6477 | CMAP_FOR_EACH_WITH_HASH (group, cmap_node, hash_int(group_id, 0), | |
6478 | &ofproto->groups) { | |
5d08a275 JR |
6479 | if (group->group_id == group_id |
6480 | && versions_visible_in_version(&group->versions, version)) { | |
db88b35c | 6481 | return group; |
7395c052 NZ |
6482 | } |
6483 | } | |
3fc3b679 | 6484 | |
db88b35c | 6485 | return NULL; |
7395c052 NZ |
6486 | } |
6487 | ||
3fc3b679 AZ |
6488 | /* If the group exists, this function increments the groups's reference count. |
6489 | * | |
6490 | * Make sure to call ofproto_group_unref() after no longer needing to maintain | |
6491 | * a reference to the group. */ | |
db88b35c | 6492 | struct ofgroup * |
76973237 | 6493 | ofproto_group_lookup(const struct ofproto *ofproto, uint32_t group_id, |
5d08a275 | 6494 | ovs_version_t version, bool take_ref) |
7395c052 | 6495 | { |
db88b35c | 6496 | struct ofgroup *group; |
7395c052 | 6497 | |
5d08a275 | 6498 | group = ofproto_group_lookup__(ofproto, group_id, version); |
76973237 | 6499 | if (group && take_ref) { |
db88b35c JR |
6500 | /* Not holding a lock, so it is possible that another thread releases |
6501 | * the last reference just before we manage to get one. */ | |
6502 | return ofproto_group_try_ref(group) ? group : NULL; | |
7395c052 | 6503 | } |
76973237 | 6504 | return group; |
7395c052 NZ |
6505 | } |
6506 | ||
cc099268 | 6507 | /* Caller should hold 'ofproto_mutex' if it is important that the |
db88b35c | 6508 | * group is not removed by someone else. */ |
7e9f8266 BP |
6509 | static bool |
6510 | ofproto_group_exists(const struct ofproto *ofproto, uint32_t group_id) | |
7e9f8266 | 6511 | { |
5d08a275 | 6512 | return ofproto_group_lookup__(ofproto, group_id, OVS_VERSION_MAX) != NULL; |
7e9f8266 BP |
6513 | } |
6514 | ||
cc099268 JR |
6515 | static void |
6516 | group_add_rule(struct ofgroup *group, struct rule *rule) | |
732feb93 | 6517 | { |
cc099268 JR |
6518 | rule_collection_add(&group->rules, rule); |
6519 | } | |
732feb93 | 6520 | |
cc099268 JR |
6521 | static void |
6522 | group_remove_rule(struct ofgroup *group, struct rule *rule) | |
6523 | { | |
6524 | rule_collection_remove(&group->rules, rule); | |
732feb93 SH |
6525 | } |
6526 | ||
7395c052 | 6527 | static void |
ca6ba700 | 6528 | append_group_stats(struct ofgroup *group, struct ovs_list *replies) |
cc099268 | 6529 | OVS_REQUIRES(ofproto_mutex) |
7395c052 NZ |
6530 | { |
6531 | struct ofputil_group_stats ogs; | |
eaf004bd | 6532 | const struct ofproto *ofproto = group->ofproto; |
7395c052 NZ |
6533 | long long int now = time_msec(); |
6534 | int error; | |
6535 | ||
646b2a9c SH |
6536 | ogs.bucket_stats = xmalloc(group->n_buckets * sizeof *ogs.bucket_stats); |
6537 | ||
732feb93 | 6538 | /* Provider sets the packet and byte counts, we do the rest. */ |
fe59694b | 6539 | ogs.ref_count = rule_collection_n(&group->rules); |
732feb93 SH |
6540 | ogs.n_buckets = group->n_buckets; |
6541 | ||
7395c052 NZ |
6542 | error = (ofproto->ofproto_class->group_get_stats |
6543 | ? ofproto->ofproto_class->group_get_stats(group, &ogs) | |
6544 | : EOPNOTSUPP); | |
6545 | if (error) { | |
7395c052 NZ |
6546 | ogs.packet_count = UINT64_MAX; |
6547 | ogs.byte_count = UINT64_MAX; | |
7395c052 NZ |
6548 | memset(ogs.bucket_stats, 0xff, |
6549 | ogs.n_buckets * sizeof *ogs.bucket_stats); | |
6550 | } | |
6551 | ||
6552 | ogs.group_id = group->group_id; | |
6553 | calc_duration(group->created, now, &ogs.duration_sec, &ogs.duration_nsec); | |
6554 | ||
6555 | ofputil_append_group_stats(replies, &ogs); | |
646b2a9c SH |
6556 | |
6557 | free(ogs.bucket_stats); | |
7395c052 NZ |
6558 | } |
6559 | ||
19187a71 BP |
6560 | static void |
6561 | handle_group_request(struct ofconn *ofconn, | |
6562 | const struct ofp_header *request, uint32_t group_id, | |
ca6ba700 | 6563 | void (*cb)(struct ofgroup *, struct ovs_list *replies)) |
cc099268 | 6564 | OVS_EXCLUDED(ofproto_mutex) |
7395c052 NZ |
6565 | { |
6566 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
7395c052 | 6567 | struct ofgroup *group; |
ca6ba700 | 6568 | struct ovs_list replies; |
7395c052 NZ |
6569 | |
6570 | ofpmp_init(&replies, request); | |
cc099268 JR |
6571 | /* Must exclude modifications to guarantee iterating groups. */ |
6572 | ovs_mutex_lock(&ofproto_mutex); | |
7395c052 | 6573 | if (group_id == OFPG_ALL) { |
db88b35c | 6574 | CMAP_FOR_EACH (group, cmap_node, &ofproto->groups) { |
19187a71 | 6575 | cb(group, &replies); |
7395c052 | 6576 | } |
7395c052 | 6577 | } else { |
5d08a275 | 6578 | group = ofproto_group_lookup__(ofproto, group_id, OVS_VERSION_MAX); |
db88b35c | 6579 | if (group) { |
19187a71 | 6580 | cb(group, &replies); |
7395c052 NZ |
6581 | } |
6582 | } | |
cc099268 | 6583 | ovs_mutex_unlock(&ofproto_mutex); |
7395c052 | 6584 | ofconn_send_replies(ofconn, &replies); |
7395c052 NZ |
6585 | } |
6586 | ||
6587 | static enum ofperr | |
19187a71 BP |
6588 | handle_group_stats_request(struct ofconn *ofconn, |
6589 | const struct ofp_header *request) | |
7395c052 | 6590 | { |
19187a71 BP |
6591 | uint32_t group_id; |
6592 | enum ofperr error; | |
7395c052 | 6593 | |
19187a71 BP |
6594 | error = ofputil_decode_group_stats_request(request, &group_id); |
6595 | if (error) { | |
6596 | return error; | |
7395c052 | 6597 | } |
7395c052 | 6598 | |
19187a71 BP |
6599 | handle_group_request(ofconn, request, group_id, append_group_stats); |
6600 | return 0; | |
6601 | } | |
6602 | ||
6603 | static void | |
ca6ba700 | 6604 | append_group_desc(struct ofgroup *group, struct ovs_list *replies) |
19187a71 BP |
6605 | { |
6606 | struct ofputil_group_desc gds; | |
7395c052 | 6607 | |
19187a71 BP |
6608 | gds.group_id = group->group_id; |
6609 | gds.type = group->type; | |
53eb84a5 SH |
6610 | gds.props = group->props; |
6611 | ||
19187a71 BP |
6612 | ofputil_append_group_desc_reply(&gds, &group->buckets, replies); |
6613 | } | |
6614 | ||
6615 | static enum ofperr | |
6616 | handle_group_desc_stats_request(struct ofconn *ofconn, | |
6617 | const struct ofp_header *request) | |
6618 | { | |
6619 | handle_group_request(ofconn, request, | |
6620 | ofputil_decode_group_desc_request(request), | |
6621 | append_group_desc); | |
7395c052 NZ |
6622 | return 0; |
6623 | } | |
6624 | ||
6625 | static enum ofperr | |
6626 | handle_group_features_stats_request(struct ofconn *ofconn, | |
6627 | const struct ofp_header *request) | |
6628 | { | |
6629 | struct ofproto *p = ofconn_get_ofproto(ofconn); | |
6630 | struct ofpbuf *msg; | |
6631 | ||
6632 | msg = ofputil_encode_group_features_reply(&p->ogf, request); | |
6633 | if (msg) { | |
6634 | ofconn_send_reply(ofconn, msg); | |
6635 | } | |
6636 | ||
6637 | return 0; | |
6638 | } | |
6639 | ||
56085be5 | 6640 | static void |
e016fb63 BP |
6641 | put_queue_get_config_reply(struct ofport *port, uint32_t queue, |
6642 | struct ovs_list *replies) | |
e8f9a7bb | 6643 | { |
e016fb63 | 6644 | struct ofputil_queue_config qc; |
e8f9a7bb | 6645 | |
e016fb63 BP |
6646 | /* None of the existing queues have compatible properties, so we hard-code |
6647 | * omitting min_rate and max_rate. */ | |
6648 | qc.port = port->ofp_port; | |
6649 | qc.queue = queue; | |
6650 | qc.min_rate = UINT16_MAX; | |
6651 | qc.max_rate = UINT16_MAX; | |
6652 | ofputil_append_queue_get_config_reply(&qc, replies); | |
6653 | } | |
e8f9a7bb | 6654 | |
e016fb63 BP |
6655 | static int |
6656 | handle_queue_get_config_request_for_port(struct ofport *port, uint32_t queue, | |
6657 | struct ovs_list *replies) | |
6658 | { | |
6659 | struct smap details = SMAP_INITIALIZER(&details); | |
6660 | if (queue != OFPQ_ALL) { | |
6661 | int error = netdev_get_queue(port->netdev, queue, &details); | |
6662 | switch (error) { | |
6663 | case 0: | |
6664 | put_queue_get_config_reply(port, queue, replies); | |
6665 | break; | |
6666 | case EOPNOTSUPP: | |
6667 | case EINVAL: | |
6668 | return OFPERR_OFPQOFC_BAD_QUEUE; | |
6669 | default: | |
6670 | return OFPERR_NXQOFC_QUEUE_ERROR; | |
6671 | } | |
6672 | } else { | |
6673 | struct netdev_queue_dump queue_dump; | |
6674 | uint32_t queue_id; | |
6675 | ||
6676 | NETDEV_QUEUE_FOR_EACH (&queue_id, &details, &queue_dump, | |
6677 | port->netdev) { | |
6678 | put_queue_get_config_reply(port, queue_id, replies); | |
6679 | } | |
6680 | } | |
6681 | smap_destroy(&details); | |
6682 | return 0; | |
56085be5 BP |
6683 | } |
6684 | ||
6685 | static enum ofperr | |
6686 | handle_queue_get_config_request(struct ofconn *ofconn, | |
6687 | const struct ofp_header *oh) | |
6688 | { | |
e016fb63 BP |
6689 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
6690 | struct ovs_list replies; | |
6691 | struct ofport *port; | |
6692 | ofp_port_t req_port; | |
6693 | uint32_t req_queue; | |
6694 | enum ofperr error; | |
6695 | ||
6696 | error = ofputil_decode_queue_get_config_request(oh, &req_port, &req_queue); | |
6697 | if (error) { | |
6698 | return error; | |
6699 | } | |
6700 | ||
6701 | ofputil_start_queue_get_config_reply(oh, &replies); | |
6702 | if (req_port == OFPP_ANY) { | |
6703 | error = OFPERR_OFPQOFC_BAD_QUEUE; | |
6704 | HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) { | |
6705 | if (!handle_queue_get_config_request_for_port(port, req_queue, | |
6706 | &replies)) { | |
6707 | error = 0; | |
6708 | } | |
6709 | } | |
6710 | } else { | |
6711 | port = ofproto_get_port(ofproto, req_port); | |
6712 | error = (port | |
6713 | ? handle_queue_get_config_request_for_port(port, req_queue, | |
6714 | &replies) | |
6715 | : OFPERR_OFPQOFC_BAD_PORT); | |
6716 | } | |
6717 | if (!error) { | |
6718 | ofconn_send_replies(ofconn, &replies); | |
6719 | } else { | |
6720 | ofpbuf_list_delete(&replies); | |
6721 | } | |
6722 | ||
6723 | return error; | |
e8f9a7bb VG |
6724 | } |
6725 | ||
809c7548 | 6726 | static enum ofperr |
63eded98 | 6727 | init_group(struct ofproto *ofproto, const struct ofputil_group_mod *gm, |
5d08a275 | 6728 | ovs_version_t version, struct ofgroup **ofgroup) |
809c7548 RW |
6729 | { |
6730 | enum ofperr error; | |
eaf004bd | 6731 | const long long int now = time_msec(); |
809c7548 RW |
6732 | |
6733 | if (gm->group_id > OFPG_MAX) { | |
6734 | return OFPERR_OFPGMFC_INVALID_GROUP; | |
6735 | } | |
6736 | if (gm->type > OFPGT11_FF) { | |
6737 | return OFPERR_OFPGMFC_BAD_TYPE; | |
6738 | } | |
6739 | ||
6740 | *ofgroup = ofproto->ofproto_class->group_alloc(); | |
6741 | if (!*ofgroup) { | |
6742 | VLOG_WARN_RL(&rl, "%s: failed to allocate group", ofproto->name); | |
6743 | return OFPERR_OFPGMFC_OUT_OF_GROUPS; | |
6744 | } | |
6745 | ||
5d08a275 | 6746 | *CONST_CAST(struct ofproto **, &(*ofgroup)->ofproto) = ofproto; |
eaf004bd AZ |
6747 | *CONST_CAST(uint32_t *, &((*ofgroup)->group_id)) = gm->group_id; |
6748 | *CONST_CAST(enum ofp11_group_type *, &(*ofgroup)->type) = gm->type; | |
6749 | *CONST_CAST(long long int *, &((*ofgroup)->created)) = now; | |
6750 | *CONST_CAST(long long int *, &((*ofgroup)->modified)) = now; | |
809c7548 | 6751 | ovs_refcount_init(&(*ofgroup)->ref_count); |
cc099268 | 6752 | (*ofgroup)->being_deleted = false; |
809c7548 | 6753 | |
db88b35c JR |
6754 | ovs_list_init(CONST_CAST(struct ovs_list *, &(*ofgroup)->buckets)); |
6755 | ofputil_bucket_clone_list(CONST_CAST(struct ovs_list *, | |
6756 | &(*ofgroup)->buckets), | |
6757 | &gm->buckets, NULL); | |
63eded98 | 6758 | |
eaf004bd | 6759 | *CONST_CAST(uint32_t *, &(*ofgroup)->n_buckets) = |
417e7e66 | 6760 | ovs_list_size(&(*ofgroup)->buckets); |
809c7548 | 6761 | |
e8dba719 JR |
6762 | ofputil_group_properties_copy(CONST_CAST(struct ofputil_group_props *, |
6763 | &(*ofgroup)->props), | |
6764 | &gm->props); | |
cc099268 | 6765 | rule_collection_init(&(*ofgroup)->rules); |
bc65c25a | 6766 | |
5d08a275 JR |
6767 | /* Make group visible from 'version'. */ |
6768 | (*ofgroup)->versions = VERSIONS_INITIALIZER(version, | |
6769 | OVS_VERSION_NOT_REMOVED); | |
6770 | ||
809c7548 RW |
6771 | /* Construct called BEFORE any locks are held. */ |
6772 | error = ofproto->ofproto_class->group_construct(*ofgroup); | |
6773 | if (error) { | |
e8dba719 JR |
6774 | ofputil_group_properties_destroy(CONST_CAST(struct ofputil_group_props *, |
6775 | &(*ofgroup)->props)); | |
db88b35c JR |
6776 | ofputil_bucket_list_destroy(CONST_CAST(struct ovs_list *, |
6777 | &(*ofgroup)->buckets)); | |
809c7548 RW |
6778 | ofproto->ofproto_class->group_dealloc(*ofgroup); |
6779 | } | |
6780 | return error; | |
6781 | } | |
6782 | ||
6c5b90ce BP |
6783 | /* Implements the OFPGC11_ADD operation specified by 'gm', adding a group to |
6784 | * 'ofproto''s group table. Returns 0 on success or an OpenFlow error code on | |
6785 | * failure. */ | |
7395c052 | 6786 | static enum ofperr |
2b0b0b80 | 6787 | add_group_start(struct ofproto *ofproto, struct ofproto_group_mod *ogm) |
cc099268 | 6788 | OVS_REQUIRES(ofproto_mutex) |
7395c052 | 6789 | { |
7395c052 NZ |
6790 | enum ofperr error; |
6791 | ||
2b0b0b80 JR |
6792 | if (ofproto_group_exists(ofproto, ogm->gm.group_id)) { |
6793 | return OFPERR_OFPGMFC_GROUP_EXISTS; | |
7395c052 NZ |
6794 | } |
6795 | ||
2b0b0b80 JR |
6796 | if (ofproto->n_groups[ogm->gm.type] |
6797 | >= ofproto->ogf.max_groups[ogm->gm.type]) { | |
6798 | return OFPERR_OFPGMFC_OUT_OF_GROUPS; | |
7395c052 NZ |
6799 | } |
6800 | ||
2b0b0b80 | 6801 | /* Allocate new group and initialize it. */ |
5d08a275 | 6802 | error = init_group(ofproto, &ogm->gm, ogm->version, &ogm->new_group); |
2b0b0b80 JR |
6803 | if (!error) { |
6804 | /* Insert new group. */ | |
6805 | cmap_insert(&ofproto->groups, &ogm->new_group->cmap_node, | |
6806 | hash_int(ogm->new_group->group_id, 0)); | |
6807 | ofproto->n_groups[ogm->new_group->type]++; | |
7395c052 | 6808 | } |
7395c052 NZ |
6809 | return error; |
6810 | } | |
6811 | ||
fce4730c SH |
6812 | /* Adds all of the buckets from 'ofgroup' to 'new_ofgroup'. The buckets |
6813 | * already in 'new_ofgroup' will be placed just after the (copy of the) bucket | |
6814 | * in 'ofgroup' with bucket ID 'command_bucket_id'. Special | |
6815 | * 'command_bucket_id' values OFPG15_BUCKET_FIRST and OFPG15_BUCKET_LAST are | |
6816 | * also honored. */ | |
6817 | static enum ofperr | |
6818 | copy_buckets_for_insert_bucket(const struct ofgroup *ofgroup, | |
6819 | struct ofgroup *new_ofgroup, | |
6820 | uint32_t command_bucket_id) | |
6821 | { | |
6822 | struct ofputil_bucket *last = NULL; | |
6823 | ||
6824 | if (command_bucket_id <= OFPG15_BUCKET_MAX) { | |
6825 | /* Check here to ensure that a bucket corresponding to | |
6826 | * command_bucket_id exists in the old bucket list. | |
6827 | * | |
6828 | * The subsequent search of below of new_ofgroup covers | |
6829 | * both buckets in the old bucket list and buckets added | |
6830 | * by the insert buckets group mod message this function processes. */ | |
6831 | if (!ofputil_bucket_find(&ofgroup->buckets, command_bucket_id)) { | |
6832 | return OFPERR_OFPGMFC_UNKNOWN_BUCKET; | |
6833 | } | |
6834 | ||
417e7e66 | 6835 | if (!ovs_list_is_empty(&new_ofgroup->buckets)) { |
fce4730c SH |
6836 | last = ofputil_bucket_list_back(&new_ofgroup->buckets); |
6837 | } | |
6838 | } | |
6839 | ||
db88b35c JR |
6840 | ofputil_bucket_clone_list(CONST_CAST(struct ovs_list *, |
6841 | &new_ofgroup->buckets), | |
6842 | &ofgroup->buckets, NULL); | |
fce4730c | 6843 | |
23a31238 | 6844 | if (ofputil_bucket_check_duplicate_id(&new_ofgroup->buckets)) { |
d2873d53 | 6845 | VLOG_INFO_RL(&rl, "Duplicate bucket id"); |
fce4730c SH |
6846 | return OFPERR_OFPGMFC_BUCKET_EXISTS; |
6847 | } | |
6848 | ||
6849 | /* Rearrange list according to command_bucket_id */ | |
6850 | if (command_bucket_id == OFPG15_BUCKET_LAST) { | |
417e7e66 | 6851 | if (!ovs_list_is_empty(&ofgroup->buckets)) { |
8e19e655 BP |
6852 | struct ofputil_bucket *new_first; |
6853 | const struct ofputil_bucket *first; | |
fce4730c | 6854 | |
8e19e655 BP |
6855 | first = ofputil_bucket_list_front(&ofgroup->buckets); |
6856 | new_first = ofputil_bucket_find(&new_ofgroup->buckets, | |
6857 | first->bucket_id); | |
fce4730c | 6858 | |
417e7e66 | 6859 | ovs_list_splice(new_ofgroup->buckets.next, &new_first->list_node, |
db88b35c JR |
6860 | CONST_CAST(struct ovs_list *, |
6861 | &new_ofgroup->buckets)); | |
8e19e655 | 6862 | } |
fce4730c SH |
6863 | } else if (command_bucket_id <= OFPG15_BUCKET_MAX && last) { |
6864 | struct ofputil_bucket *after; | |
6865 | ||
6866 | /* Presence of bucket is checked above so after should never be NULL */ | |
6867 | after = ofputil_bucket_find(&new_ofgroup->buckets, command_bucket_id); | |
6868 | ||
417e7e66 | 6869 | ovs_list_splice(after->list_node.next, new_ofgroup->buckets.next, |
fce4730c SH |
6870 | last->list_node.next); |
6871 | } | |
6872 | ||
6873 | return 0; | |
6874 | } | |
6875 | ||
6876 | /* Appends all of the a copy of all the buckets from 'ofgroup' to 'new_ofgroup' | |
6877 | * with the exception of the bucket whose bucket id is 'command_bucket_id'. | |
6878 | * Special 'command_bucket_id' values OFPG15_BUCKET_FIRST, OFPG15_BUCKET_LAST | |
6879 | * and OFPG15_BUCKET_ALL are also honored. */ | |
6880 | static enum ofperr | |
6881 | copy_buckets_for_remove_bucket(const struct ofgroup *ofgroup, | |
6882 | struct ofgroup *new_ofgroup, | |
6883 | uint32_t command_bucket_id) | |
6884 | { | |
6885 | const struct ofputil_bucket *skip = NULL; | |
6886 | ||
6887 | if (command_bucket_id == OFPG15_BUCKET_ALL) { | |
6888 | return 0; | |
6889 | } | |
6890 | ||
6891 | if (command_bucket_id == OFPG15_BUCKET_FIRST) { | |
417e7e66 | 6892 | if (!ovs_list_is_empty(&ofgroup->buckets)) { |
fce4730c SH |
6893 | skip = ofputil_bucket_list_front(&ofgroup->buckets); |
6894 | } | |
6895 | } else if (command_bucket_id == OFPG15_BUCKET_LAST) { | |
417e7e66 | 6896 | if (!ovs_list_is_empty(&ofgroup->buckets)) { |
fce4730c SH |
6897 | skip = ofputil_bucket_list_back(&ofgroup->buckets); |
6898 | } | |
6899 | } else { | |
6900 | skip = ofputil_bucket_find(&ofgroup->buckets, command_bucket_id); | |
6901 | if (!skip) { | |
6902 | return OFPERR_OFPGMFC_UNKNOWN_BUCKET; | |
6903 | } | |
6904 | } | |
6905 | ||
db88b35c JR |
6906 | ofputil_bucket_clone_list(CONST_CAST(struct ovs_list *, |
6907 | &new_ofgroup->buckets), | |
6908 | &ofgroup->buckets, skip); | |
fce4730c SH |
6909 | |
6910 | return 0; | |
6911 | } | |
6912 | ||
6913 | /* Implements OFPGC11_MODIFY, OFPGC15_INSERT_BUCKET and | |
6914 | * OFPGC15_REMOVE_BUCKET. Returns 0 on success or an OpenFlow error code | |
6c5b90ce | 6915 | * on failure. |
7395c052 | 6916 | * |
809c7548 RW |
6917 | * Note that the group is re-created and then replaces the old group in |
6918 | * ofproto's ofgroup hash map. Thus, the group is never altered while users of | |
6c5b90ce | 6919 | * the xlate module hold a pointer to the group. */ |
7395c052 | 6920 | static enum ofperr |
2b0b0b80 | 6921 | modify_group_start(struct ofproto *ofproto, struct ofproto_group_mod *ogm) |
cc099268 | 6922 | OVS_REQUIRES(ofproto_mutex) |
7395c052 | 6923 | { |
2b0b0b80 JR |
6924 | struct ofgroup *old_group; /* Modified group. */ |
6925 | struct ofgroup *new_group; | |
7395c052 NZ |
6926 | enum ofperr error; |
6927 | ||
5d08a275 JR |
6928 | old_group = ofproto_group_lookup__(ofproto, ogm->gm.group_id, |
6929 | OVS_VERSION_MAX); | |
2b0b0b80 JR |
6930 | if (!old_group) { |
6931 | return OFPERR_OFPGMFC_UNKNOWN_GROUP; | |
7395c052 NZ |
6932 | } |
6933 | ||
2b0b0b80 JR |
6934 | if (old_group->type != ogm->gm.type |
6935 | && (ofproto->n_groups[ogm->gm.type] | |
6936 | >= ofproto->ogf.max_groups[ogm->gm.type])) { | |
6937 | return OFPERR_OFPGMFC_OUT_OF_GROUPS; | |
7395c052 | 6938 | } |
809c7548 | 6939 | |
5d08a275 | 6940 | error = init_group(ofproto, &ogm->gm, ogm->version, &ogm->new_group); |
2b0b0b80 JR |
6941 | if (error) { |
6942 | return error; | |
7395c052 | 6943 | } |
2b0b0b80 | 6944 | new_group = ogm->new_group; |
7395c052 | 6945 | |
fce4730c | 6946 | /* Manipulate bucket list for bucket commands */ |
2b0b0b80 JR |
6947 | if (ogm->gm.command == OFPGC15_INSERT_BUCKET) { |
6948 | error = copy_buckets_for_insert_bucket(old_group, new_group, | |
6949 | ogm->gm.command_bucket_id); | |
6950 | } else if (ogm->gm.command == OFPGC15_REMOVE_BUCKET) { | |
6951 | error = copy_buckets_for_remove_bucket(old_group, new_group, | |
6952 | ogm->gm.command_bucket_id); | |
fce4730c SH |
6953 | } |
6954 | if (error) { | |
6955 | goto out; | |
6956 | } | |
6957 | ||
809c7548 | 6958 | /* The group creation time does not change during modification. */ |
2b0b0b80 JR |
6959 | *CONST_CAST(long long int *, &(new_group->created)) = old_group->created; |
6960 | *CONST_CAST(long long int *, &(new_group->modified)) = time_msec(); | |
7395c052 | 6961 | |
2b0b0b80 | 6962 | group_collection_add(&ogm->old_groups, old_group); |
db88b35c | 6963 | |
5d08a275 JR |
6964 | /* Mark the old group for deletion. */ |
6965 | versions_set_remove_version(&old_group->versions, ogm->version); | |
6966 | /* Insert replacement group. */ | |
6967 | cmap_insert(&ofproto->groups, &new_group->cmap_node, | |
6968 | hash_int(new_group->group_id, 0)); | |
cc099268 | 6969 | /* Transfer rules. */ |
2b0b0b80 | 6970 | rule_collection_move(&new_group->rules, &old_group->rules); |
db88b35c | 6971 | |
2b0b0b80 JR |
6972 | if (old_group->type != new_group->type) { |
6973 | ofproto->n_groups[old_group->type]--; | |
6974 | ofproto->n_groups[new_group->type]++; | |
7395c052 | 6975 | } |
2b0b0b80 | 6976 | return 0; |
7395c052 | 6977 | |
809c7548 | 6978 | out: |
2b0b0b80 | 6979 | ofproto_group_unref(new_group); |
7395c052 NZ |
6980 | return error; |
6981 | } | |
6982 | ||
88b87a36 JS |
6983 | /* Implements the OFPGC11_ADD_OR_MOD command which creates the group when it does not |
6984 | * exist yet and modifies it otherwise */ | |
6985 | static enum ofperr | |
2b0b0b80 JR |
6986 | add_or_modify_group_start(struct ofproto *ofproto, |
6987 | struct ofproto_group_mod *ogm) | |
cc099268 | 6988 | OVS_REQUIRES(ofproto_mutex) |
88b87a36 | 6989 | { |
88b87a36 | 6990 | enum ofperr error; |
88b87a36 | 6991 | |
2b0b0b80 JR |
6992 | if (!ofproto_group_exists(ofproto, ogm->gm.group_id)) { |
6993 | error = add_group_start(ofproto, ogm); | |
88b87a36 | 6994 | } else { |
2b0b0b80 | 6995 | error = modify_group_start(ofproto, ogm); |
88b87a36 | 6996 | } |
cc099268 | 6997 | |
88b87a36 JS |
6998 | return error; |
6999 | } | |
7000 | ||
7395c052 | 7001 | static void |
5d08a275 JR |
7002 | delete_group_start(struct ofproto *ofproto, ovs_version_t version, |
7003 | struct group_collection *groups, struct ofgroup *group) | |
cc099268 | 7004 | OVS_REQUIRES(ofproto_mutex) |
7395c052 | 7005 | { |
2b0b0b80 | 7006 | /* Makes flow deletion code leave the rule pointers in 'group->rules' |
cc099268 JR |
7007 | * intact, so that we can later refer to the rules deleted due to the group |
7008 | * deletion. Rule pointers will be removed from all other groups, if any, | |
7009 | * so we will never try to delete the same rule twice. */ | |
2b0b0b80 JR |
7010 | group->being_deleted = true; |
7011 | ||
7012 | /* Mark all the referring groups for deletion. */ | |
5d08a275 | 7013 | delete_flows_start__(ofproto, version, &group->rules); |
2b0b0b80 | 7014 | group_collection_add(groups, group); |
5d08a275 | 7015 | versions_set_remove_version(&group->versions, version); |
2b0b0b80 JR |
7016 | ofproto->n_groups[group->type]--; |
7017 | } | |
276f6518 | 7018 | |
2b0b0b80 JR |
7019 | static void |
7020 | delete_group_finish(struct ofproto *ofproto, struct ofgroup *group) | |
7021 | OVS_REQUIRES(ofproto_mutex) | |
7022 | { | |
7023 | /* Finish deletion of all flow entries containing this group in a group | |
7024 | * action. */ | |
7025 | delete_flows_finish__(ofproto, &group->rules, OFPRR_GROUP_DELETE, NULL); | |
276f6518 | 7026 | |
5d08a275 | 7027 | /* Group removal is postponed by the caller. */ |
7395c052 NZ |
7028 | } |
7029 | ||
6c5b90ce | 7030 | /* Implements OFPGC11_DELETE. */ |
7395c052 | 7031 | static void |
2b0b0b80 | 7032 | delete_groups_start(struct ofproto *ofproto, struct ofproto_group_mod *ogm) |
cc099268 | 7033 | OVS_REQUIRES(ofproto_mutex) |
7395c052 | 7034 | { |
2b0b0b80 | 7035 | struct ofgroup *group; |
7395c052 | 7036 | |
2b0b0b80 JR |
7037 | if (ogm->gm.group_id == OFPG_ALL) { |
7038 | CMAP_FOR_EACH (group, cmap_node, &ofproto->groups) { | |
5d08a275 JR |
7039 | if (versions_visible_in_version(&group->versions, ogm->version)) { |
7040 | delete_group_start(ofproto, ogm->version, &ogm->old_groups, | |
7041 | group); | |
7042 | } | |
7395c052 NZ |
7043 | } |
7044 | } else { | |
5d08a275 JR |
7045 | group = ofproto_group_lookup__(ofproto, ogm->gm.group_id, ogm->version); |
7046 | if (group) { | |
7047 | delete_group_start(ofproto, ogm->version, &ogm->old_groups, group); | |
7395c052 NZ |
7048 | } |
7049 | } | |
7395c052 NZ |
7050 | } |
7051 | ||
7052 | static enum ofperr | |
2b0b0b80 JR |
7053 | ofproto_group_mod_start(struct ofproto *ofproto, struct ofproto_group_mod *ogm) |
7054 | OVS_REQUIRES(ofproto_mutex) | |
7395c052 | 7055 | { |
7395c052 NZ |
7056 | enum ofperr error; |
7057 | ||
2b0b0b80 JR |
7058 | ogm->new_group = NULL; |
7059 | group_collection_init(&ogm->old_groups); | |
7395c052 | 7060 | |
2b0b0b80 | 7061 | switch (ogm->gm.command) { |
7395c052 | 7062 | case OFPGC11_ADD: |
2b0b0b80 | 7063 | error = add_group_start(ofproto, ogm); |
3c35db62 | 7064 | break; |
7395c052 NZ |
7065 | |
7066 | case OFPGC11_MODIFY: | |
2b0b0b80 | 7067 | error = modify_group_start(ofproto, ogm); |
3c35db62 | 7068 | break; |
7395c052 | 7069 | |
88b87a36 | 7070 | case OFPGC11_ADD_OR_MOD: |
2b0b0b80 | 7071 | error = add_or_modify_group_start(ofproto, ogm); |
88b87a36 JS |
7072 | break; |
7073 | ||
7395c052 | 7074 | case OFPGC11_DELETE: |
2b0b0b80 | 7075 | delete_groups_start(ofproto, ogm); |
3c35db62 NR |
7076 | error = 0; |
7077 | break; | |
7395c052 | 7078 | |
fce4730c | 7079 | case OFPGC15_INSERT_BUCKET: |
2b0b0b80 | 7080 | error = modify_group_start(ofproto, ogm); |
3c35db62 | 7081 | break; |
fce4730c SH |
7082 | |
7083 | case OFPGC15_REMOVE_BUCKET: | |
2b0b0b80 | 7084 | error = modify_group_start(ofproto, ogm); |
3c35db62 | 7085 | break; |
fce4730c | 7086 | |
7395c052 | 7087 | default: |
2b0b0b80 | 7088 | if (ogm->gm.command > OFPGC11_DELETE) { |
d2873d53 | 7089 | VLOG_INFO_RL(&rl, "%s: Invalid group_mod command type %d", |
2b0b0b80 | 7090 | ofproto->name, ogm->gm.command); |
7395c052 | 7091 | } |
63eded98 | 7092 | error = OFPERR_OFPGMFC_BAD_COMMAND; |
2b0b0b80 | 7093 | break; |
7395c052 | 7094 | } |
2b0b0b80 JR |
7095 | return error; |
7096 | } | |
3c35db62 | 7097 | |
25070e04 JR |
7098 | static void |
7099 | ofproto_group_mod_revert(struct ofproto *ofproto, | |
7100 | struct ofproto_group_mod *ogm) | |
7101 | OVS_REQUIRES(ofproto_mutex) | |
7102 | { | |
7103 | struct ofgroup *new_group = ogm->new_group; | |
7104 | struct ofgroup *old_group; | |
7105 | ||
7106 | /* Restore replaced or deleted groups. */ | |
7107 | GROUP_COLLECTION_FOR_EACH (old_group, &ogm->old_groups) { | |
7108 | ofproto->n_groups[old_group->type]++; | |
7109 | if (new_group) { | |
7110 | ovs_assert(group_collection_n(&ogm->old_groups) == 1); | |
7111 | /* Transfer rules back. */ | |
7112 | rule_collection_move(&old_group->rules, &new_group->rules); | |
7113 | } else { | |
7114 | old_group->being_deleted = false; | |
7115 | /* Revert rule deletion. */ | |
7116 | delete_flows_revert__(ofproto, &old_group->rules); | |
7117 | } | |
7118 | /* Restore visibility. */ | |
7119 | versions_set_remove_version(&old_group->versions, | |
7120 | OVS_VERSION_NOT_REMOVED); | |
7121 | } | |
7122 | if (new_group) { | |
7123 | /* Remove the new group immediately. It was never visible to | |
7124 | * lookups. */ | |
7125 | cmap_remove(&ofproto->groups, &new_group->cmap_node, | |
7126 | hash_int(new_group->group_id, 0)); | |
7127 | ofproto->n_groups[new_group->type]--; | |
7128 | ofproto_group_unref(new_group); | |
7129 | } | |
7130 | } | |
7131 | ||
2b0b0b80 JR |
7132 | static void |
7133 | ofproto_group_mod_finish(struct ofproto *ofproto, | |
7134 | struct ofproto_group_mod *ogm, | |
7135 | const struct openflow_mod_requester *req) | |
7136 | OVS_REQUIRES(ofproto_mutex) | |
7137 | { | |
7138 | struct ofgroup *new_group = ogm->new_group; | |
7139 | struct ofgroup *old_group; | |
7140 | ||
ccb3bc08 JR |
7141 | if (new_group && group_collection_n(&ogm->old_groups) && |
7142 | ofproto->ofproto_class->group_modify) { | |
2b0b0b80 JR |
7143 | /* Modify a group. */ |
7144 | ovs_assert(group_collection_n(&ogm->old_groups) == 1); | |
2b0b0b80 JR |
7145 | |
7146 | /* XXX: OK to lose old group's stats? */ | |
7147 | ofproto->ofproto_class->group_modify(new_group); | |
5d08a275 | 7148 | } |
2b0b0b80 | 7149 | |
5d08a275 JR |
7150 | /* Delete old groups. */ |
7151 | GROUP_COLLECTION_FOR_EACH(old_group, &ogm->old_groups) { | |
7152 | delete_group_finish(ofproto, old_group); | |
2b0b0b80 | 7153 | } |
5d08a275 | 7154 | remove_groups_postponed(&ogm->old_groups); |
2b0b0b80 JR |
7155 | |
7156 | if (req) { | |
3c35db62 | 7157 | struct ofputil_requestforward rf; |
2b0b0b80 | 7158 | rf.xid = req->request->xid; |
3c35db62 | 7159 | rf.reason = OFPRFR_GROUP_MOD; |
2b0b0b80 JR |
7160 | rf.group_mod = &ogm->gm; |
7161 | connmgr_send_requestforward(ofproto->connmgr, req->ofconn, &rf); | |
7162 | } | |
7163 | } | |
7164 | ||
7165 | /* Delete all groups from 'ofproto'. | |
7166 | * | |
7167 | * This is intended for use within an ofproto provider's 'destruct' | |
7168 | * function. */ | |
7169 | void | |
7170 | ofproto_group_delete_all(struct ofproto *ofproto) | |
7171 | OVS_EXCLUDED(ofproto_mutex) | |
7172 | { | |
7173 | struct ofproto_group_mod ogm; | |
7174 | ||
7175 | ogm.gm.command = OFPGC11_DELETE; | |
7176 | ogm.gm.group_id = OFPG_ALL; | |
7177 | ||
7178 | ovs_mutex_lock(&ofproto_mutex); | |
5d08a275 | 7179 | ogm.version = ofproto->tables_version + 1; |
2b0b0b80 | 7180 | ofproto_group_mod_start(ofproto, &ogm); |
5d08a275 | 7181 | ofproto_bump_tables_version(ofproto); |
2b0b0b80 JR |
7182 | ofproto_group_mod_finish(ofproto, &ogm, NULL); |
7183 | ovs_mutex_unlock(&ofproto_mutex); | |
7184 | } | |
7185 | ||
7186 | static enum ofperr | |
7187 | handle_group_mod(struct ofconn *ofconn, const struct ofp_header *oh) | |
7188 | OVS_EXCLUDED(ofproto_mutex) | |
7189 | { | |
7190 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
7191 | struct ofproto_group_mod ogm; | |
7192 | enum ofperr error; | |
7193 | ||
7194 | error = reject_slave_controller(ofconn); | |
7195 | if (error) { | |
7196 | return error; | |
7197 | } | |
7198 | ||
7199 | error = ofputil_decode_group_mod(oh, &ogm.gm); | |
7200 | if (error) { | |
7201 | return error; | |
3c35db62 | 7202 | } |
2b0b0b80 JR |
7203 | |
7204 | ovs_mutex_lock(&ofproto_mutex); | |
5d08a275 | 7205 | ogm.version = ofproto->tables_version + 1; |
2b0b0b80 JR |
7206 | error = ofproto_group_mod_start(ofproto, &ogm); |
7207 | if (!error) { | |
7208 | struct openflow_mod_requester req = { ofconn, oh }; | |
5d08a275 JR |
7209 | |
7210 | ofproto_bump_tables_version(ofproto); | |
2b0b0b80 | 7211 | ofproto_group_mod_finish(ofproto, &ogm, &req); |
2c7ee524 | 7212 | ofmonitor_flush(ofproto->connmgr); |
2b0b0b80 | 7213 | } |
2b0b0b80 JR |
7214 | ovs_mutex_unlock(&ofproto_mutex); |
7215 | ||
75868d0e | 7216 | ofputil_uninit_group_mod(&ogm.gm); |
63eded98 | 7217 | |
3c35db62 | 7218 | return error; |
7395c052 NZ |
7219 | } |
7220 | ||
3c1bb396 BP |
7221 | enum ofputil_table_miss |
7222 | ofproto_table_get_miss_config(const struct ofproto *ofproto, uint8_t table_id) | |
6d328fa2 | 7223 | { |
82c22d34 BP |
7224 | enum ofputil_table_miss miss; |
7225 | ||
7226 | atomic_read_relaxed(&ofproto->tables[table_id].miss_config, &miss); | |
7227 | return miss; | |
7228 | } | |
7229 | ||
7230 | static void | |
7231 | table_mod__(struct oftable *oftable, | |
de7d3c07 | 7232 | const struct ofputil_table_mod *tm) |
82c22d34 | 7233 | { |
de7d3c07 | 7234 | if (tm->miss == OFPUTIL_TABLE_MISS_DEFAULT) { |
82c22d34 BP |
7235 | /* This is how an OFPT_TABLE_MOD decodes if it doesn't specify any |
7236 | * table-miss configuration (because the protocol used doesn't have | |
7237 | * such a concept), so there's nothing to do. */ | |
7238 | } else { | |
de7d3c07 | 7239 | atomic_store_relaxed(&oftable->miss_config, tm->miss); |
82c22d34 BP |
7240 | } |
7241 | ||
7242 | unsigned int new_eviction = oftable->eviction; | |
de7d3c07 | 7243 | if (tm->eviction == OFPUTIL_TABLE_EVICTION_ON) { |
82c22d34 | 7244 | new_eviction |= EVICTION_OPENFLOW; |
de7d3c07 | 7245 | } else if (tm->eviction == OFPUTIL_TABLE_EVICTION_OFF) { |
82c22d34 BP |
7246 | new_eviction &= ~EVICTION_OPENFLOW; |
7247 | } | |
afcea9ea | 7248 | |
82c22d34 BP |
7249 | if (new_eviction != oftable->eviction) { |
7250 | ovs_mutex_lock(&ofproto_mutex); | |
7251 | oftable_configure_eviction(oftable, new_eviction, | |
7252 | oftable->eviction_fields, | |
7253 | oftable->n_eviction_fields); | |
7254 | ovs_mutex_unlock(&ofproto_mutex); | |
7255 | } | |
de7d3c07 SJ |
7256 | |
7257 | if (tm->vacancy != OFPUTIL_TABLE_VACANCY_DEFAULT) { | |
7258 | ovs_mutex_lock(&ofproto_mutex); | |
de7d3c07 SJ |
7259 | oftable->vacancy_down = tm->table_vacancy.vacancy_down; |
7260 | oftable->vacancy_up = tm->table_vacancy.vacancy_up; | |
6c6eedc5 SJ |
7261 | if (tm->vacancy == OFPUTIL_TABLE_VACANCY_OFF) { |
7262 | oftable->vacancy_event = 0; | |
7263 | } else if (!oftable->vacancy_event) { | |
7264 | uint8_t vacancy = oftable_vacancy(oftable); | |
7265 | oftable->vacancy_event = (vacancy < oftable->vacancy_up | |
7266 | ? OFPTR_VACANCY_UP | |
7267 | : OFPTR_VACANCY_DOWN); | |
7268 | } | |
de7d3c07 SJ |
7269 | ovs_mutex_unlock(&ofproto_mutex); |
7270 | } | |
6d328fa2 SH |
7271 | } |
7272 | ||
67761761 SH |
7273 | static enum ofperr |
7274 | table_mod(struct ofproto *ofproto, const struct ofputil_table_mod *tm) | |
7275 | { | |
3c1bb396 | 7276 | if (!check_table_id(ofproto, tm->table_id)) { |
67761761 | 7277 | return OFPERR_OFPTMFC_BAD_TABLE; |
82c22d34 BP |
7278 | } |
7279 | ||
7280 | /* Don't allow the eviction flags to be changed (except to the only fixed | |
7281 | * value that OVS supports). OF1.4 says this is normal: "The | |
7282 | * OFPTMPT_EVICTION property usually cannot be modified using a | |
7283 | * OFP_TABLE_MOD request, because the eviction mechanism is switch | |
7284 | * defined". */ | |
7285 | if (tm->eviction_flags != UINT32_MAX | |
7286 | && tm->eviction_flags != OFPROTO_EVICTION_FLAGS) { | |
7287 | return OFPERR_OFPTMFC_BAD_CONFIG; | |
7288 | } | |
7289 | ||
7290 | if (tm->table_id == OFPTT_ALL) { | |
7291 | struct oftable *oftable; | |
7292 | OFPROTO_FOR_EACH_TABLE (oftable, ofproto) { | |
7293 | if (!(oftable->flags & (OFTABLE_HIDDEN | OFTABLE_READONLY))) { | |
de7d3c07 | 7294 | table_mod__(oftable, tm); |
3c1bb396 | 7295 | } |
3c1bb396 | 7296 | } |
82c22d34 BP |
7297 | } else { |
7298 | struct oftable *oftable = &ofproto->tables[tm->table_id]; | |
7299 | if (oftable->flags & OFTABLE_READONLY) { | |
7300 | return OFPERR_OFPTMFC_EPERM; | |
7301 | } | |
de7d3c07 | 7302 | table_mod__(oftable, tm); |
67761761 | 7303 | } |
82c22d34 | 7304 | |
67761761 SH |
7305 | return 0; |
7306 | } | |
7307 | ||
918f2b82 AZ |
7308 | static enum ofperr |
7309 | handle_table_mod(struct ofconn *ofconn, const struct ofp_header *oh) | |
7310 | { | |
67761761 | 7311 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
918f2b82 AZ |
7312 | struct ofputil_table_mod tm; |
7313 | enum ofperr error; | |
7314 | ||
7315 | error = reject_slave_controller(ofconn); | |
7316 | if (error) { | |
7317 | return error; | |
7318 | } | |
7319 | ||
7320 | error = ofputil_decode_table_mod(oh, &tm); | |
7321 | if (error) { | |
7322 | return error; | |
7323 | } | |
7324 | ||
67761761 | 7325 | return table_mod(ofproto, &tm); |
918f2b82 AZ |
7326 | } |
7327 | ||
5bacd5cd JR |
7328 | /* Free resources that may be allocated by ofproto_flow_mod_init(). */ |
7329 | void | |
7330 | ofproto_flow_mod_uninit(struct ofproto_flow_mod *ofm) | |
7331 | { | |
7332 | if (ofm->temp_rule) { | |
7333 | ofproto_rule_unref(ofm->temp_rule); | |
7334 | ofm->temp_rule = NULL; | |
7335 | } | |
7336 | if (ofm->criteria.version != OVS_VERSION_NOT_REMOVED) { | |
7337 | rule_criteria_destroy(&ofm->criteria); | |
7338 | } | |
7339 | if (ofm->conjs) { | |
7340 | free(ofm->conjs); | |
7341 | ofm->conjs = NULL; | |
7342 | ofm->n_conjs = 0; | |
7343 | } | |
7344 | } | |
7345 | ||
777af88d | 7346 | static enum ofperr |
5bacd5cd | 7347 | ofproto_flow_mod_init(struct ofproto *ofproto, struct ofproto_flow_mod *ofm, |
2c7ee524 | 7348 | const struct ofputil_flow_mod *fm, struct rule *rule) |
5bacd5cd | 7349 | OVS_EXCLUDED(ofproto_mutex) |
777af88d | 7350 | { |
5bacd5cd JR |
7351 | enum ofperr error; |
7352 | ||
7353 | /* Forward flow mod fields we need later. */ | |
7354 | ofm->command = fm->command; | |
5bacd5cd JR |
7355 | ofm->modify_cookie = fm->modify_cookie; |
7356 | ||
7357 | ofm->modify_may_add_flow = (fm->new_cookie != OVS_BE64_MAX | |
7358 | && fm->cookie_mask == htonll(0)); | |
8b6987d7 JR |
7359 | /* Old flags must be kept when modifying a flow, but we still must |
7360 | * honor the reset counts flag if present in the flow mod. */ | |
7361 | ofm->modify_keep_counts = !(fm->flags & OFPUTIL_FF_RESET_COUNTS); | |
5bacd5cd JR |
7362 | |
7363 | /* Initialize state needed by ofproto_flow_mod_uninit(). */ | |
2c7ee524 | 7364 | ofm->temp_rule = rule; |
5bacd5cd JR |
7365 | ofm->criteria.version = OVS_VERSION_NOT_REMOVED; |
7366 | ofm->conjs = NULL; | |
7367 | ofm->n_conjs = 0; | |
7368 | ||
c184807c JR |
7369 | bool check_buffer_id = false; |
7370 | ||
5bacd5cd JR |
7371 | switch (ofm->command) { |
7372 | case OFPFC_ADD: | |
c184807c | 7373 | check_buffer_id = true; |
5bacd5cd JR |
7374 | error = add_flow_init(ofproto, ofm, fm); |
7375 | break; | |
7376 | case OFPFC_MODIFY: | |
c184807c | 7377 | check_buffer_id = true; |
5bacd5cd JR |
7378 | error = modify_flows_init_loose(ofproto, ofm, fm); |
7379 | break; | |
7380 | case OFPFC_MODIFY_STRICT: | |
c184807c | 7381 | check_buffer_id = true; |
5bacd5cd JR |
7382 | error = modify_flow_init_strict(ofproto, ofm, fm); |
7383 | break; | |
7384 | case OFPFC_DELETE: | |
7385 | error = delete_flows_init_loose(ofproto, ofm, fm); | |
7386 | break; | |
7387 | case OFPFC_DELETE_STRICT: | |
7388 | error = delete_flows_init_strict(ofproto, ofm, fm); | |
7389 | break; | |
7390 | default: | |
7391 | error = OFPERR_OFPFMFC_BAD_COMMAND; | |
7392 | break; | |
7393 | } | |
c184807c JR |
7394 | if (!error && check_buffer_id && fm->buffer_id != UINT32_MAX) { |
7395 | error = OFPERR_OFPBRC_BUFFER_UNKNOWN; | |
7396 | } | |
7397 | ||
cc099268 | 7398 | if (error) { |
5bacd5cd | 7399 | ofproto_flow_mod_uninit(ofm); |
cc099268 | 7400 | } |
5bacd5cd JR |
7401 | return error; |
7402 | } | |
cc099268 | 7403 | |
5bacd5cd JR |
7404 | static enum ofperr |
7405 | ofproto_flow_mod_start(struct ofproto *ofproto, struct ofproto_flow_mod *ofm) | |
7406 | OVS_REQUIRES(ofproto_mutex) | |
7407 | { | |
7408 | enum ofperr error; | |
81dee635 | 7409 | |
5bacd5cd JR |
7410 | rule_collection_init(&ofm->old_rules); |
7411 | rule_collection_init(&ofm->new_rules); | |
81dee635 | 7412 | |
5a27991d | 7413 | switch (ofm->command) { |
1f42be1c | 7414 | case OFPFC_ADD: |
5bacd5cd JR |
7415 | error = add_flow_start(ofproto, ofm); |
7416 | break; | |
1f42be1c | 7417 | case OFPFC_MODIFY: |
5bacd5cd JR |
7418 | error = modify_flows_start_loose(ofproto, ofm); |
7419 | break; | |
1f42be1c | 7420 | case OFPFC_MODIFY_STRICT: |
5bacd5cd JR |
7421 | error = modify_flow_start_strict(ofproto, ofm); |
7422 | break; | |
1f42be1c | 7423 | case OFPFC_DELETE: |
5bacd5cd JR |
7424 | error = delete_flows_start_loose(ofproto, ofm); |
7425 | break; | |
1f42be1c | 7426 | case OFPFC_DELETE_STRICT: |
5bacd5cd JR |
7427 | error = delete_flow_start_strict(ofproto, ofm); |
7428 | break; | |
7429 | default: | |
7430 | OVS_NOT_REACHED(); | |
1f42be1c | 7431 | } |
5bacd5cd JR |
7432 | /* Release resources not needed after start. */ |
7433 | ofproto_flow_mod_uninit(ofm); | |
1f42be1c | 7434 | |
5bacd5cd JR |
7435 | if (error) { |
7436 | rule_collection_destroy(&ofm->old_rules); | |
7437 | rule_collection_destroy(&ofm->new_rules); | |
7438 | } | |
7439 | return error; | |
1f42be1c JR |
7440 | } |
7441 | ||
7442 | static void | |
8be00367 | 7443 | ofproto_flow_mod_revert(struct ofproto *ofproto, struct ofproto_flow_mod *ofm) |
1f42be1c JR |
7444 | OVS_REQUIRES(ofproto_mutex) |
7445 | { | |
5a27991d | 7446 | switch (ofm->command) { |
1f42be1c | 7447 | case OFPFC_ADD: |
8be00367 | 7448 | add_flow_revert(ofproto, ofm); |
1f42be1c JR |
7449 | break; |
7450 | ||
7451 | case OFPFC_MODIFY: | |
7452 | case OFPFC_MODIFY_STRICT: | |
8be00367 | 7453 | modify_flows_revert(ofproto, ofm); |
1f42be1c JR |
7454 | break; |
7455 | ||
7456 | case OFPFC_DELETE: | |
7457 | case OFPFC_DELETE_STRICT: | |
8be00367 | 7458 | delete_flows_revert(ofproto, ofm); |
1f42be1c JR |
7459 | break; |
7460 | ||
7461 | default: | |
7462 | break; | |
7463 | } | |
2c7ee524 | 7464 | |
5bacd5cd JR |
7465 | rule_collection_destroy(&ofm->old_rules); |
7466 | rule_collection_destroy(&ofm->new_rules); | |
1f42be1c JR |
7467 | } |
7468 | ||
7469 | static void | |
2c7ee524 | 7470 | ofproto_flow_mod_finish(struct ofproto *ofproto, struct ofproto_flow_mod *ofm, |
0a0d9385 | 7471 | const struct openflow_mod_requester *req) |
1f42be1c JR |
7472 | OVS_REQUIRES(ofproto_mutex) |
7473 | { | |
5a27991d | 7474 | switch (ofm->command) { |
1f42be1c | 7475 | case OFPFC_ADD: |
8be00367 | 7476 | add_flow_finish(ofproto, ofm, req); |
1f42be1c JR |
7477 | break; |
7478 | ||
7479 | case OFPFC_MODIFY: | |
7480 | case OFPFC_MODIFY_STRICT: | |
8be00367 | 7481 | modify_flows_finish(ofproto, ofm, req); |
1f42be1c JR |
7482 | break; |
7483 | ||
7484 | case OFPFC_DELETE: | |
7485 | case OFPFC_DELETE_STRICT: | |
8be00367 | 7486 | delete_flows_finish(ofproto, ofm, req); |
1f42be1c JR |
7487 | break; |
7488 | ||
7489 | default: | |
7490 | break; | |
7491 | } | |
b0d38b2f | 7492 | |
5bacd5cd JR |
7493 | rule_collection_destroy(&ofm->old_rules); |
7494 | rule_collection_destroy(&ofm->new_rules); | |
7495 | ||
b0d38b2f | 7496 | if (req) { |
5a27991d | 7497 | ofconn_report_flow_mod(req->ofconn, ofm->command); |
b0d38b2f | 7498 | } |
1f42be1c JR |
7499 | } |
7500 | ||
39c94593 JR |
7501 | /* Commit phases (all while locking ofproto_mutex): |
7502 | * | |
7503 | * 1. Begin: Gather resources and make changes visible in the next version. | |
7504 | * - Mark affected rules for removal in the next version. | |
7505 | * - Create new replacement rules, make visible in the next | |
7506 | * version. | |
7507 | * - Do not send any events or notifications. | |
7508 | * | |
7509 | * 2. Revert: Fail if any errors are found. After this point no errors are | |
7510 | * possible. No visible changes were made, so rollback is minimal (remove | |
7511 | * added invisible rules, restore visibility of rules marked for removal). | |
7512 | * | |
1c38055d JR |
7513 | * 3. Finish: Make the changes visible for lookups. Insert replacement rules to |
7514 | * the ofproto provider. Remove replaced and deleted rules from ofproto data | |
7515 | * structures, and Schedule postponed removal of deleted rules from the | |
7516 | * classifier. Send notifications, buffered packets, etc. | |
39c94593 | 7517 | */ |
1f42be1c JR |
7518 | static enum ofperr |
7519 | do_bundle_commit(struct ofconn *ofconn, uint32_t id, uint16_t flags) | |
7520 | { | |
7521 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
44e0c35d | 7522 | ovs_version_t version = ofproto->tables_version + 1; |
1f42be1c JR |
7523 | struct ofp_bundle *bundle; |
7524 | struct ofp_bundle_entry *be; | |
777af88d | 7525 | enum ofperr error; |
1f42be1c JR |
7526 | |
7527 | bundle = ofconn_get_bundle(ofconn, id); | |
7528 | ||
7529 | if (!bundle) { | |
7530 | return OFPERR_OFPBFC_BAD_ID; | |
7531 | } | |
7532 | if (bundle->flags != flags) { | |
7533 | error = OFPERR_OFPBFC_BAD_FLAGS; | |
7534 | } else { | |
1c38055d JR |
7535 | bool prev_is_port_mod = false; |
7536 | ||
1f42be1c JR |
7537 | error = 0; |
7538 | ovs_mutex_lock(&ofproto_mutex); | |
39c94593 JR |
7539 | |
7540 | /* 1. Begin. */ | |
1f42be1c JR |
7541 | LIST_FOR_EACH (be, node, &bundle->msg_list) { |
7542 | if (be->type == OFPTYPE_PORT_MOD) { | |
1c38055d JR |
7543 | /* Our port mods are not atomic. */ |
7544 | if (flags & OFPBF_ATOMIC) { | |
7545 | error = OFPERR_OFPBFC_MSG_FAILED; | |
7546 | } else { | |
7547 | prev_is_port_mod = true; | |
8be00367 | 7548 | error = port_mod_start(ofconn, &be->opm.pm, &be->opm.port); |
1c38055d | 7549 | } |
25070e04 JR |
7550 | } else { |
7551 | /* Flow & group mods between port mods are applied as a single | |
7552 | * version, but the versions are published only after we know | |
7553 | * the commit is successful. */ | |
1c38055d | 7554 | if (prev_is_port_mod) { |
25070e04 | 7555 | prev_is_port_mod = false; |
8be00367 | 7556 | ++version; |
1c38055d | 7557 | } |
25070e04 JR |
7558 | if (be->type == OFPTYPE_FLOW_MOD) { |
7559 | /* Store the version in which the changes should take | |
7560 | * effect. */ | |
7561 | be->ofm.version = version; | |
7562 | error = ofproto_flow_mod_start(ofproto, &be->ofm); | |
7563 | } else if (be->type == OFPTYPE_GROUP_MOD) { | |
7564 | /* Store the version in which the changes should take | |
7565 | * effect. */ | |
7566 | be->ogm.version = version; | |
7567 | error = ofproto_group_mod_start(ofproto, &be->ogm); | |
6dd3c787 JR |
7568 | } else if (be->type == OFPTYPE_PACKET_OUT) { |
7569 | be->opo.version = version; | |
7570 | error = ofproto_packet_out_start(ofproto, &be->opo); | |
25070e04 JR |
7571 | } else { |
7572 | OVS_NOT_REACHED(); | |
7573 | } | |
1f42be1c JR |
7574 | } |
7575 | if (error) { | |
7576 | break; | |
7577 | } | |
7578 | } | |
1c38055d | 7579 | |
1f42be1c JR |
7580 | if (error) { |
7581 | /* Send error referring to the original message. */ | |
7582 | if (error) { | |
51bb26fa | 7583 | ofconn_send_error(ofconn, &be->ofp_msg, error); |
1f42be1c JR |
7584 | error = OFPERR_OFPBFC_MSG_FAILED; |
7585 | } | |
7586 | ||
39c94593 | 7587 | /* 2. Revert. Undo all the changes made above. */ |
1f42be1c JR |
7588 | LIST_FOR_EACH_REVERSE_CONTINUE(be, node, &bundle->msg_list) { |
7589 | if (be->type == OFPTYPE_FLOW_MOD) { | |
8be00367 | 7590 | ofproto_flow_mod_revert(ofproto, &be->ofm); |
25070e04 JR |
7591 | } else if (be->type == OFPTYPE_GROUP_MOD) { |
7592 | ofproto_group_mod_revert(ofproto, &be->ogm); | |
6dd3c787 JR |
7593 | } else if (be->type == OFPTYPE_PACKET_OUT) { |
7594 | ofproto_packet_out_revert(ofproto, &be->opo); | |
1f42be1c | 7595 | } |
1c38055d | 7596 | /* Nothing needs to be reverted for a port mod. */ |
1f42be1c JR |
7597 | } |
7598 | } else { | |
39c94593 | 7599 | /* 4. Finish. */ |
1f42be1c | 7600 | LIST_FOR_EACH (be, node, &bundle->msg_list) { |
25070e04 | 7601 | if (be->type == OFPTYPE_PORT_MOD) { |
1c38055d JR |
7602 | /* Perform the actual port mod. This is not atomic, i.e., |
7603 | * the effects will be immediately seen by upcall | |
7604 | * processing regardless of the lookup version. It should | |
7605 | * be noted that port configuration changes can originate | |
7606 | * also from OVSDB changes asynchronously to all upcall | |
7607 | * processing. */ | |
8be00367 | 7608 | port_mod_finish(ofconn, &be->opm.pm, be->opm.port); |
25070e04 | 7609 | } else { |
6dd3c787 JR |
7610 | version = |
7611 | (be->type == OFPTYPE_FLOW_MOD) ? be->ofm.version : | |
7612 | (be->type == OFPTYPE_GROUP_MOD) ? be->ogm.version : | |
7613 | (be->type == OFPTYPE_PACKET_OUT) ? be->opo.version : | |
7614 | version; | |
7615 | ||
7616 | /* Bump the lookup version to the one of the current | |
7617 | * message. This makes all the changes in the bundle at | |
7618 | * this version visible to lookups at once. */ | |
7619 | if (ofproto->tables_version < version) { | |
7620 | ofproto->tables_version = version; | |
7621 | ofproto->ofproto_class->set_tables_version( | |
7622 | ofproto, ofproto->tables_version); | |
7623 | } | |
7624 | ||
25070e04 | 7625 | struct openflow_mod_requester req = { ofconn, |
51bb26fa | 7626 | &be->ofp_msg }; |
25070e04 | 7627 | |
6dd3c787 | 7628 | if (be->type == OFPTYPE_FLOW_MOD) { |
25070e04 JR |
7629 | ofproto_flow_mod_finish(ofproto, &be->ofm, &req); |
7630 | } else if (be->type == OFPTYPE_GROUP_MOD) { | |
25070e04 | 7631 | ofproto_group_mod_finish(ofproto, &be->ogm, &req); |
6dd3c787 JR |
7632 | } else if (be->type == OFPTYPE_PACKET_OUT) { |
7633 | ofproto_packet_out_finish(ofproto, &be->opo); | |
25070e04 | 7634 | } |
1f42be1c JR |
7635 | } |
7636 | } | |
7637 | } | |
1c38055d | 7638 | |
1f42be1c JR |
7639 | ofmonitor_flush(ofproto->connmgr); |
7640 | ovs_mutex_unlock(&ofproto_mutex); | |
1f42be1c JR |
7641 | } |
7642 | ||
7643 | /* The bundle is discarded regardless the outcome. */ | |
0c78eebe | 7644 | ofp_bundle_remove__(ofconn, bundle); |
1f42be1c JR |
7645 | return error; |
7646 | } | |
7647 | ||
7648 | static enum ofperr | |
7649 | handle_bundle_control(struct ofconn *ofconn, const struct ofp_header *oh) | |
7650 | { | |
777af88d | 7651 | struct ofputil_bundle_ctrl_msg bctrl; |
777af88d | 7652 | struct ofputil_bundle_ctrl_msg reply; |
1f42be1c JR |
7653 | struct ofpbuf *buf; |
7654 | enum ofperr error; | |
777af88d | 7655 | |
ba9d374a JR |
7656 | error = reject_slave_controller(ofconn); |
7657 | if (error) { | |
7658 | return error; | |
7659 | } | |
7660 | ||
777af88d AC |
7661 | error = ofputil_decode_bundle_ctrl(oh, &bctrl); |
7662 | if (error) { | |
7663 | return error; | |
7664 | } | |
7665 | reply.flags = 0; | |
7666 | reply.bundle_id = bctrl.bundle_id; | |
7667 | ||
7668 | switch (bctrl.type) { | |
51bb26fa JR |
7669 | case OFPBCT_OPEN_REQUEST: |
7670 | error = ofp_bundle_open(ofconn, bctrl.bundle_id, bctrl.flags, oh); | |
777af88d AC |
7671 | reply.type = OFPBCT_OPEN_REPLY; |
7672 | break; | |
7673 | case OFPBCT_CLOSE_REQUEST: | |
7674 | error = ofp_bundle_close(ofconn, bctrl.bundle_id, bctrl.flags); | |
ea53e3a8 | 7675 | reply.type = OFPBCT_CLOSE_REPLY; |
777af88d AC |
7676 | break; |
7677 | case OFPBCT_COMMIT_REQUEST: | |
1f42be1c | 7678 | error = do_bundle_commit(ofconn, bctrl.bundle_id, bctrl.flags); |
777af88d AC |
7679 | reply.type = OFPBCT_COMMIT_REPLY; |
7680 | break; | |
7681 | case OFPBCT_DISCARD_REQUEST: | |
7682 | error = ofp_bundle_discard(ofconn, bctrl.bundle_id); | |
7683 | reply.type = OFPBCT_DISCARD_REPLY; | |
7684 | break; | |
7685 | ||
7686 | case OFPBCT_OPEN_REPLY: | |
7687 | case OFPBCT_CLOSE_REPLY: | |
7688 | case OFPBCT_COMMIT_REPLY: | |
7689 | case OFPBCT_DISCARD_REPLY: | |
7690 | return OFPERR_OFPBFC_BAD_TYPE; | |
7691 | break; | |
7692 | } | |
7693 | ||
7694 | if (!error) { | |
7695 | buf = ofputil_encode_bundle_ctrl_reply(oh, &reply); | |
7696 | ofconn_send_reply(ofconn, buf); | |
7697 | } | |
7698 | return error; | |
7699 | } | |
7700 | ||
777af88d AC |
7701 | static enum ofperr |
7702 | handle_bundle_add(struct ofconn *ofconn, const struct ofp_header *oh) | |
5bacd5cd | 7703 | OVS_EXCLUDED(ofproto_mutex) |
777af88d | 7704 | { |
7ac27a04 | 7705 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
777af88d AC |
7706 | enum ofperr error; |
7707 | struct ofputil_bundle_add_msg badd; | |
7ac27a04 JR |
7708 | struct ofp_bundle_entry *bmsg; |
7709 | enum ofptype type; | |
777af88d | 7710 | |
ba9d374a JR |
7711 | error = reject_slave_controller(ofconn); |
7712 | if (error) { | |
7713 | return error; | |
7714 | } | |
7715 | ||
7ac27a04 | 7716 | error = ofputil_decode_bundle_add(oh, &badd, &type); |
777af88d AC |
7717 | if (error) { |
7718 | return error; | |
7719 | } | |
7720 | ||
1f42be1c | 7721 | bmsg = ofp_bundle_entry_alloc(type, badd.msg); |
7ac27a04 | 7722 | |
6dd3c787 JR |
7723 | struct ofpbuf ofpacts; |
7724 | uint64_t ofpacts_stub[1024 / 8]; | |
7725 | ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub); | |
7726 | ||
7ac27a04 | 7727 | if (type == OFPTYPE_PORT_MOD) { |
8be00367 | 7728 | error = ofputil_decode_port_mod(badd.msg, &bmsg->opm.pm, false); |
7ac27a04 | 7729 | } else if (type == OFPTYPE_FLOW_MOD) { |
5bacd5cd | 7730 | struct ofputil_flow_mod fm; |
7ac27a04 | 7731 | |
5bacd5cd | 7732 | error = ofputil_decode_flow_mod(&fm, badd.msg, |
7ac27a04 | 7733 | ofconn_get_protocol(ofconn), |
8d8ab6c2 | 7734 | ofproto_get_tun_tab(ofproto), |
04f48a68 | 7735 | &ofproto->vl_mff_map, &ofpacts, |
7ac27a04 JR |
7736 | u16_to_ofp(ofproto->max_ports), |
7737 | ofproto->n_tables); | |
5bacd5cd | 7738 | if (!error) { |
2c7ee524 | 7739 | error = ofproto_flow_mod_init(ofproto, &bmsg->ofm, &fm, NULL); |
5bacd5cd | 7740 | } |
25070e04 JR |
7741 | } else if (type == OFPTYPE_GROUP_MOD) { |
7742 | error = ofputil_decode_group_mod(badd.msg, &bmsg->ogm.gm); | |
6dd3c787 JR |
7743 | } else if (type == OFPTYPE_PACKET_OUT) { |
7744 | struct ofputil_packet_out po; | |
7745 | ||
7746 | COVERAGE_INC(ofproto_packet_out); | |
7747 | ||
7748 | /* Decode message. */ | |
7749 | error = ofputil_decode_packet_out(&po, badd.msg, &ofpacts); | |
7750 | if (!error) { | |
7751 | po.ofpacts = ofpbuf_steal_data(&ofpacts); /* Move to heap. */ | |
7752 | error = ofproto_packet_out_init(ofproto, ofconn, &bmsg->opo, &po); | |
7753 | } | |
7ac27a04 JR |
7754 | } else { |
7755 | OVS_NOT_REACHED(); | |
7756 | } | |
7757 | ||
6dd3c787 JR |
7758 | ofpbuf_uninit(&ofpacts); |
7759 | ||
7ac27a04 JR |
7760 | if (!error) { |
7761 | error = ofp_bundle_add_message(ofconn, badd.bundle_id, badd.flags, | |
51bb26fa | 7762 | bmsg, oh); |
7ac27a04 JR |
7763 | } |
7764 | ||
7765 | if (error) { | |
7766 | ofp_bundle_entry_free(bmsg); | |
7767 | } | |
7768 | ||
7769 | return error; | |
777af88d AC |
7770 | } |
7771 | ||
6159c531 | 7772 | static enum ofperr |
4e548ad9 | 7773 | handle_tlv_table_mod(struct ofconn *ofconn, const struct ofp_header *oh) |
6159c531 | 7774 | { |
8d8ab6c2 JG |
7775 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
7776 | struct tun_table *old_tab, *new_tab; | |
4e548ad9 | 7777 | struct ofputil_tlv_table_mod ttm; |
6159c531 JG |
7778 | enum ofperr error; |
7779 | ||
7780 | error = reject_slave_controller(ofconn); | |
7781 | if (error) { | |
7782 | return error; | |
7783 | } | |
7784 | ||
4e548ad9 | 7785 | error = ofputil_decode_tlv_table_mod(oh, &ttm); |
6159c531 JG |
7786 | if (error) { |
7787 | return error; | |
7788 | } | |
7789 | ||
8d8ab6c2 JG |
7790 | old_tab = ovsrcu_get_protected(struct tun_table *, &ofproto->metadata_tab); |
7791 | error = tun_metadata_table_mod(&ttm, old_tab, &new_tab); | |
7792 | if (!error) { | |
7793 | ovsrcu_set(&ofproto->metadata_tab, new_tab); | |
7794 | tun_metadata_postpone_free(old_tab); | |
04f48a68 YHW |
7795 | |
7796 | ovs_mutex_lock(&ofproto->vl_mff_map.mutex); | |
7797 | error = mf_vl_mff_map_mod_from_tun_metadata(&ofproto->vl_mff_map, | |
7798 | &ttm); | |
7799 | ovs_mutex_unlock(&ofproto->vl_mff_map.mutex); | |
8d8ab6c2 | 7800 | } |
9558d2a5 | 7801 | |
4e548ad9 | 7802 | ofputil_uninit_tlv_table(&ttm.mappings); |
9558d2a5 | 7803 | return error; |
6159c531 JG |
7804 | } |
7805 | ||
7806 | static enum ofperr | |
4e548ad9 | 7807 | handle_tlv_table_request(struct ofconn *ofconn, const struct ofp_header *oh) |
6159c531 | 7808 | { |
8d8ab6c2 | 7809 | const struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
4e548ad9 | 7810 | struct ofputil_tlv_table_reply ttr; |
9558d2a5 JG |
7811 | struct ofpbuf *b; |
7812 | ||
8d8ab6c2 JG |
7813 | tun_metadata_table_request(ofproto_get_tun_tab(ofproto), &ttr); |
7814 | ||
4e548ad9 ML |
7815 | b = ofputil_encode_tlv_table_reply(oh, &ttr); |
7816 | ofputil_uninit_tlv_table(&ttr.mappings); | |
9558d2a5 JG |
7817 | |
7818 | ofconn_send_reply(ofconn, b); | |
7819 | return 0; | |
6159c531 JG |
7820 | } |
7821 | ||
90bf1e07 | 7822 | static enum ofperr |
d1e2cf21 | 7823 | handle_openflow__(struct ofconn *ofconn, const struct ofpbuf *msg) |
15aaf599 | 7824 | OVS_EXCLUDED(ofproto_mutex) |
064af421 | 7825 | { |
6fd6ed71 | 7826 | const struct ofp_header *oh = msg->data; |
982697a4 | 7827 | enum ofptype type; |
90bf1e07 | 7828 | enum ofperr error; |
064af421 | 7829 | |
982697a4 | 7830 | error = ofptype_decode(&type, oh); |
d1e2cf21 BP |
7831 | if (error) { |
7832 | return error; | |
7833 | } | |
ff3c2c63 YT |
7834 | if (oh->version >= OFP13_VERSION && ofpmsg_is_stat_request(oh) |
7835 | && ofpmp_more(oh)) { | |
7836 | /* We have no buffer implementation for multipart requests. | |
7837 | * Report overflow for requests which consists of multiple | |
7838 | * messages. */ | |
7839 | return OFPERR_OFPBRC_MULTIPART_BUFFER_OVERFLOW; | |
7840 | } | |
064af421 | 7841 | |
982697a4 | 7842 | switch (type) { |
d1e2cf21 | 7843 | /* OpenFlow requests. */ |
982697a4 | 7844 | case OFPTYPE_ECHO_REQUEST: |
d1e2cf21 | 7845 | return handle_echo_request(ofconn, oh); |
064af421 | 7846 | |
982697a4 | 7847 | case OFPTYPE_FEATURES_REQUEST: |
d1e2cf21 | 7848 | return handle_features_request(ofconn, oh); |
064af421 | 7849 | |
982697a4 | 7850 | case OFPTYPE_GET_CONFIG_REQUEST: |
d1e2cf21 | 7851 | return handle_get_config_request(ofconn, oh); |
064af421 | 7852 | |
982697a4 BP |
7853 | case OFPTYPE_SET_CONFIG: |
7854 | return handle_set_config(ofconn, oh); | |
064af421 | 7855 | |
982697a4 BP |
7856 | case OFPTYPE_PACKET_OUT: |
7857 | return handle_packet_out(ofconn, oh); | |
064af421 | 7858 | |
982697a4 | 7859 | case OFPTYPE_PORT_MOD: |
d1e2cf21 | 7860 | return handle_port_mod(ofconn, oh); |
064af421 | 7861 | |
982697a4 | 7862 | case OFPTYPE_FLOW_MOD: |
2e4f5fcf | 7863 | return handle_flow_mod(ofconn, oh); |
064af421 | 7864 | |
7395c052 NZ |
7865 | case OFPTYPE_GROUP_MOD: |
7866 | return handle_group_mod(ofconn, oh); | |
7867 | ||
918f2b82 AZ |
7868 | case OFPTYPE_TABLE_MOD: |
7869 | return handle_table_mod(ofconn, oh); | |
7870 | ||
9cae45dc JR |
7871 | case OFPTYPE_METER_MOD: |
7872 | return handle_meter_mod(ofconn, oh); | |
7873 | ||
982697a4 | 7874 | case OFPTYPE_BARRIER_REQUEST: |
d1e2cf21 | 7875 | return handle_barrier_request(ofconn, oh); |
064af421 | 7876 | |
6ea4776b JR |
7877 | case OFPTYPE_ROLE_REQUEST: |
7878 | return handle_role_request(ofconn, oh); | |
7879 | ||
d1e2cf21 | 7880 | /* OpenFlow replies. */ |
982697a4 | 7881 | case OFPTYPE_ECHO_REPLY: |
d1e2cf21 | 7882 | return 0; |
246e61ea | 7883 | |
d1e2cf21 | 7884 | /* Nicira extension requests. */ |
982697a4 | 7885 | case OFPTYPE_FLOW_MOD_TABLE_ID: |
6c1491fb | 7886 | return handle_nxt_flow_mod_table_id(ofconn, oh); |
d1e2cf21 | 7887 | |
982697a4 | 7888 | case OFPTYPE_SET_FLOW_FORMAT: |
d1e2cf21 BP |
7889 | return handle_nxt_set_flow_format(ofconn, oh); |
7890 | ||
982697a4 | 7891 | case OFPTYPE_SET_PACKET_IN_FORMAT: |
54834960 EJ |
7892 | return handle_nxt_set_packet_in_format(ofconn, oh); |
7893 | ||
982697a4 | 7894 | case OFPTYPE_SET_CONTROLLER_ID: |
a7349929 BP |
7895 | return handle_nxt_set_controller_id(ofconn, oh); |
7896 | ||
982697a4 | 7897 | case OFPTYPE_FLOW_AGE: |
f27f2134 BP |
7898 | /* Nothing to do. */ |
7899 | return 0; | |
7900 | ||
982697a4 | 7901 | case OFPTYPE_FLOW_MONITOR_CANCEL: |
2b07c8b1 BP |
7902 | return handle_flow_monitor_cancel(ofconn, oh); |
7903 | ||
982697a4 | 7904 | case OFPTYPE_SET_ASYNC_CONFIG: |
80d5aefd BP |
7905 | return handle_nxt_set_async_config(ofconn, oh); |
7906 | ||
c423b3b3 AC |
7907 | case OFPTYPE_GET_ASYNC_REQUEST: |
7908 | return handle_nxt_get_async_request(ofconn, oh); | |
7909 | ||
77ab5fd2 BP |
7910 | case OFPTYPE_NXT_RESUME: |
7911 | return handle_nxt_resume(ofconn, oh); | |
7912 | ||
349adfb2 | 7913 | /* Statistics requests. */ |
982697a4 BP |
7914 | case OFPTYPE_DESC_STATS_REQUEST: |
7915 | return handle_desc_stats_request(ofconn, oh); | |
7916 | ||
7917 | case OFPTYPE_FLOW_STATS_REQUEST: | |
7918 | return handle_flow_stats_request(ofconn, oh); | |
7919 | ||
7920 | case OFPTYPE_AGGREGATE_STATS_REQUEST: | |
7921 | return handle_aggregate_stats_request(ofconn, oh); | |
7922 | ||
7923 | case OFPTYPE_TABLE_STATS_REQUEST: | |
7924 | return handle_table_stats_request(ofconn, oh); | |
7925 | ||
3c4e10fb BP |
7926 | case OFPTYPE_TABLE_FEATURES_STATS_REQUEST: |
7927 | return handle_table_features_request(ofconn, oh); | |
7928 | ||
03c72922 BP |
7929 | case OFPTYPE_TABLE_DESC_REQUEST: |
7930 | return handle_table_desc_request(ofconn, oh); | |
7931 | ||
982697a4 BP |
7932 | case OFPTYPE_PORT_STATS_REQUEST: |
7933 | return handle_port_stats_request(ofconn, oh); | |
7934 | ||
7935 | case OFPTYPE_QUEUE_STATS_REQUEST: | |
7936 | return handle_queue_stats_request(ofconn, oh); | |
7937 | ||
7938 | case OFPTYPE_PORT_DESC_STATS_REQUEST: | |
7939 | return handle_port_desc_stats_request(ofconn, oh); | |
7940 | ||
7941 | case OFPTYPE_FLOW_MONITOR_STATS_REQUEST: | |
7942 | return handle_flow_monitor_request(ofconn, oh); | |
7943 | ||
261bd854 BP |
7944 | case OFPTYPE_METER_STATS_REQUEST: |
7945 | case OFPTYPE_METER_CONFIG_STATS_REQUEST: | |
9cae45dc JR |
7946 | return handle_meter_request(ofconn, oh, type); |
7947 | ||
261bd854 | 7948 | case OFPTYPE_METER_FEATURES_STATS_REQUEST: |
9cae45dc JR |
7949 | return handle_meter_features_request(ofconn, oh); |
7950 | ||
261bd854 | 7951 | case OFPTYPE_GROUP_STATS_REQUEST: |
7395c052 NZ |
7952 | return handle_group_stats_request(ofconn, oh); |
7953 | ||
261bd854 | 7954 | case OFPTYPE_GROUP_DESC_STATS_REQUEST: |
7395c052 NZ |
7955 | return handle_group_desc_stats_request(ofconn, oh); |
7956 | ||
261bd854 | 7957 | case OFPTYPE_GROUP_FEATURES_STATS_REQUEST: |
7395c052 NZ |
7958 | return handle_group_features_stats_request(ofconn, oh); |
7959 | ||
7395c052 | 7960 | case OFPTYPE_QUEUE_GET_CONFIG_REQUEST: |
e8f9a7bb | 7961 | return handle_queue_get_config_request(ofconn, oh); |
2e1ae200 | 7962 | |
777af88d AC |
7963 | case OFPTYPE_BUNDLE_CONTROL: |
7964 | return handle_bundle_control(ofconn, oh); | |
7965 | ||
7966 | case OFPTYPE_BUNDLE_ADD_MESSAGE: | |
7967 | return handle_bundle_add(ofconn, oh); | |
7968 | ||
4e548ad9 ML |
7969 | case OFPTYPE_NXT_TLV_TABLE_MOD: |
7970 | return handle_tlv_table_mod(ofconn, oh); | |
6159c531 | 7971 | |
4e548ad9 ML |
7972 | case OFPTYPE_NXT_TLV_TABLE_REQUEST: |
7973 | return handle_tlv_table_request(ofconn, oh); | |
6159c531 | 7974 | |
fb8f22c1 BY |
7975 | case OFPTYPE_IPFIX_BRIDGE_STATS_REQUEST: |
7976 | return handle_ipfix_bridge_stats_request(ofconn, oh); | |
7977 | ||
7978 | case OFPTYPE_IPFIX_FLOW_STATS_REQUEST: | |
7979 | return handle_ipfix_flow_stats_request(ofconn, oh); | |
7980 | ||
2a7c4805 JP |
7981 | case OFPTYPE_CT_FLUSH_ZONE: |
7982 | return handle_nxt_ct_flush_zone(ofconn, oh); | |
7983 | ||
982697a4 BP |
7984 | case OFPTYPE_HELLO: |
7985 | case OFPTYPE_ERROR: | |
7986 | case OFPTYPE_FEATURES_REPLY: | |
7987 | case OFPTYPE_GET_CONFIG_REPLY: | |
7988 | case OFPTYPE_PACKET_IN: | |
7989 | case OFPTYPE_FLOW_REMOVED: | |
7990 | case OFPTYPE_PORT_STATUS: | |
7991 | case OFPTYPE_BARRIER_REPLY: | |
c545d38d | 7992 | case OFPTYPE_QUEUE_GET_CONFIG_REPLY: |
982697a4 BP |
7993 | case OFPTYPE_DESC_STATS_REPLY: |
7994 | case OFPTYPE_FLOW_STATS_REPLY: | |
7995 | case OFPTYPE_QUEUE_STATS_REPLY: | |
7996 | case OFPTYPE_PORT_STATS_REPLY: | |
7997 | case OFPTYPE_TABLE_STATS_REPLY: | |
7998 | case OFPTYPE_AGGREGATE_STATS_REPLY: | |
7999 | case OFPTYPE_PORT_DESC_STATS_REPLY: | |
8000 | case OFPTYPE_ROLE_REPLY: | |
8001 | case OFPTYPE_FLOW_MONITOR_PAUSED: | |
8002 | case OFPTYPE_FLOW_MONITOR_RESUMED: | |
8003 | case OFPTYPE_FLOW_MONITOR_STATS_REPLY: | |
2e1ae200 | 8004 | case OFPTYPE_GET_ASYNC_REPLY: |
261bd854 BP |
8005 | case OFPTYPE_GROUP_STATS_REPLY: |
8006 | case OFPTYPE_GROUP_DESC_STATS_REPLY: | |
8007 | case OFPTYPE_GROUP_FEATURES_STATS_REPLY: | |
8008 | case OFPTYPE_METER_STATS_REPLY: | |
8009 | case OFPTYPE_METER_CONFIG_STATS_REPLY: | |
8010 | case OFPTYPE_METER_FEATURES_STATS_REPLY: | |
8011 | case OFPTYPE_TABLE_FEATURES_STATS_REPLY: | |
03c72922 | 8012 | case OFPTYPE_TABLE_DESC_REPLY: |
252f3411 | 8013 | case OFPTYPE_ROLE_STATUS: |
3c35db62 | 8014 | case OFPTYPE_REQUESTFORWARD: |
6c6eedc5 | 8015 | case OFPTYPE_TABLE_STATUS: |
4e548ad9 | 8016 | case OFPTYPE_NXT_TLV_TABLE_REPLY: |
fb8f22c1 BY |
8017 | case OFPTYPE_IPFIX_BRIDGE_STATS_REPLY: |
8018 | case OFPTYPE_IPFIX_FLOW_STATS_REPLY: | |
064af421 | 8019 | default: |
76ec08e0 YT |
8020 | if (ofpmsg_is_stat_request(oh)) { |
8021 | return OFPERR_OFPBRC_BAD_STAT; | |
8022 | } else { | |
8023 | return OFPERR_OFPBRC_BAD_TYPE; | |
8024 | } | |
064af421 | 8025 | } |
d1e2cf21 | 8026 | } |
064af421 | 8027 | |
b20f4073 | 8028 | static void |
e03248b7 | 8029 | handle_openflow(struct ofconn *ofconn, const struct ofpbuf *ofp_msg) |
15aaf599 | 8030 | OVS_EXCLUDED(ofproto_mutex) |
d1e2cf21 | 8031 | { |
d51c8b71 JR |
8032 | enum ofperr error = handle_openflow__(ofconn, ofp_msg); |
8033 | ||
b20f4073 | 8034 | if (error) { |
6fd6ed71 | 8035 | ofconn_send_error(ofconn, ofp_msg->data, error); |
064af421 | 8036 | } |
d1e2cf21 | 8037 | COVERAGE_INC(ofproto_recv_openflow); |
7ee20df1 BP |
8038 | } |
8039 | \f | |
064af421 | 8040 | static uint64_t |
fa60c019 | 8041 | pick_datapath_id(const struct ofproto *ofproto) |
064af421 | 8042 | { |
fa60c019 | 8043 | const struct ofport *port; |
064af421 | 8044 | |
abe529af | 8045 | port = ofproto_get_port(ofproto, OFPP_LOCAL); |
fa60c019 | 8046 | if (port) { |
74ff3298 | 8047 | struct eth_addr ea; |
fa60c019 BP |
8048 | int error; |
8049 | ||
74ff3298 | 8050 | error = netdev_get_etheraddr(port->netdev, &ea); |
064af421 BP |
8051 | if (!error) { |
8052 | return eth_addr_to_uint64(ea); | |
8053 | } | |
fbfa2911 BP |
8054 | VLOG_WARN("%s: could not get MAC address for %s (%s)", |
8055 | ofproto->name, netdev_get_name(port->netdev), | |
10a89ef0 | 8056 | ovs_strerror(error)); |
064af421 | 8057 | } |
fa60c019 | 8058 | return ofproto->fallback_dpid; |
064af421 BP |
8059 | } |
8060 | ||
8061 | static uint64_t | |
8062 | pick_fallback_dpid(void) | |
8063 | { | |
74ff3298 JR |
8064 | struct eth_addr ea; |
8065 | eth_addr_nicira_random(&ea); | |
064af421 BP |
8066 | return eth_addr_to_uint64(ea); |
8067 | } | |
8068 | \f | |
254750ce BP |
8069 | /* Table overflow policy. */ |
8070 | ||
ad3efdcb EJ |
8071 | /* Chooses and updates 'rulep' with a rule to evict from 'table'. Sets 'rulep' |
8072 | * to NULL if the table is not configured to evict rules or if the table | |
8073 | * contains no evictable rules. (Rules with a readlock on their evict rwlock, | |
8074 | * or with no timeouts are not evictable.) */ | |
8075 | static bool | |
8076 | choose_rule_to_evict(struct oftable *table, struct rule **rulep) | |
15aaf599 | 8077 | OVS_REQUIRES(ofproto_mutex) |
254750ce BP |
8078 | { |
8079 | struct eviction_group *evg; | |
8080 | ||
ad3efdcb | 8081 | *rulep = NULL; |
82c22d34 | 8082 | if (!table->eviction) { |
ad3efdcb | 8083 | return false; |
254750ce BP |
8084 | } |
8085 | ||
8086 | /* In the common case, the outer and inner loops here will each be entered | |
8087 | * exactly once: | |
8088 | * | |
8089 | * - The inner loop normally "return"s in its first iteration. If the | |
8090 | * eviction group has any evictable rules, then it always returns in | |
8091 | * some iteration. | |
8092 | * | |
8093 | * - The outer loop only iterates more than once if the largest eviction | |
8094 | * group has no evictable rules. | |
8095 | * | |
8096 | * - The outer loop can exit only if table's 'max_flows' is all filled up | |
afe2143d | 8097 | * by unevictable rules. */ |
254750ce BP |
8098 | HEAP_FOR_EACH (evg, size_node, &table->eviction_groups_by_size) { |
8099 | struct rule *rule; | |
8100 | ||
8101 | HEAP_FOR_EACH (rule, evg_node, &evg->rules) { | |
15aaf599 BP |
8102 | *rulep = rule; |
8103 | return true; | |
254750ce BP |
8104 | } |
8105 | } | |
8106 | ||
ad3efdcb | 8107 | return false; |
254750ce | 8108 | } |
254750ce BP |
8109 | \f |
8110 | /* Eviction groups. */ | |
8111 | ||
8112 | /* Returns the priority to use for an eviction_group that contains 'n_rules' | |
8113 | * rules. The priority contains low-order random bits to ensure that eviction | |
8114 | * groups with the same number of rules are prioritized randomly. */ | |
8115 | static uint32_t | |
8116 | eviction_group_priority(size_t n_rules) | |
8117 | { | |
8118 | uint16_t size = MIN(UINT16_MAX, n_rules); | |
8119 | return (size << 16) | random_uint16(); | |
8120 | } | |
8121 | ||
8122 | /* Updates 'evg', an eviction_group within 'table', following a change that | |
8123 | * adds or removes rules in 'evg'. */ | |
8124 | static void | |
8125 | eviction_group_resized(struct oftable *table, struct eviction_group *evg) | |
15aaf599 | 8126 | OVS_REQUIRES(ofproto_mutex) |
254750ce BP |
8127 | { |
8128 | heap_change(&table->eviction_groups_by_size, &evg->size_node, | |
8129 | eviction_group_priority(heap_count(&evg->rules))); | |
8130 | } | |
8131 | ||
8132 | /* Destroys 'evg', an eviction_group within 'table': | |
8133 | * | |
8134 | * - Removes all the rules, if any, from 'evg'. (It doesn't destroy the | |
8135 | * rules themselves, just removes them from the eviction group.) | |
8136 | * | |
8137 | * - Removes 'evg' from 'table'. | |
8138 | * | |
8139 | * - Frees 'evg'. */ | |
8140 | static void | |
8141 | eviction_group_destroy(struct oftable *table, struct eviction_group *evg) | |
15aaf599 | 8142 | OVS_REQUIRES(ofproto_mutex) |
254750ce BP |
8143 | { |
8144 | while (!heap_is_empty(&evg->rules)) { | |
8145 | struct rule *rule; | |
8146 | ||
8147 | rule = CONTAINER_OF(heap_pop(&evg->rules), struct rule, evg_node); | |
8148 | rule->eviction_group = NULL; | |
8149 | } | |
8150 | hmap_remove(&table->eviction_groups_by_id, &evg->id_node); | |
8151 | heap_remove(&table->eviction_groups_by_size, &evg->size_node); | |
8152 | heap_destroy(&evg->rules); | |
8153 | free(evg); | |
8154 | } | |
8155 | ||
8156 | /* Removes 'rule' from its eviction group, if any. */ | |
8157 | static void | |
8158 | eviction_group_remove_rule(struct rule *rule) | |
15aaf599 | 8159 | OVS_REQUIRES(ofproto_mutex) |
254750ce BP |
8160 | { |
8161 | if (rule->eviction_group) { | |
8162 | struct oftable *table = &rule->ofproto->tables[rule->table_id]; | |
8163 | struct eviction_group *evg = rule->eviction_group; | |
8164 | ||
8165 | rule->eviction_group = NULL; | |
8166 | heap_remove(&evg->rules, &rule->evg_node); | |
8167 | if (heap_is_empty(&evg->rules)) { | |
8168 | eviction_group_destroy(table, evg); | |
8169 | } else { | |
8170 | eviction_group_resized(table, evg); | |
8171 | } | |
8172 | } | |
8173 | } | |
8174 | ||
8175 | /* Hashes the 'rule''s values for the eviction_fields of 'rule''s table, and | |
8176 | * returns the hash value. */ | |
8177 | static uint32_t | |
8178 | eviction_group_hash_rule(struct rule *rule) | |
15aaf599 | 8179 | OVS_REQUIRES(ofproto_mutex) |
254750ce BP |
8180 | { |
8181 | struct oftable *table = &rule->ofproto->tables[rule->table_id]; | |
8182 | const struct mf_subfield *sf; | |
5cb7a798 | 8183 | struct flow flow; |
254750ce BP |
8184 | uint32_t hash; |
8185 | ||
8186 | hash = table->eviction_group_id_basis; | |
8fd47924 | 8187 | miniflow_expand(rule->cr.match.flow, &flow); |
254750ce BP |
8188 | for (sf = table->eviction_fields; |
8189 | sf < &table->eviction_fields[table->n_eviction_fields]; | |
8190 | sf++) | |
8191 | { | |
aff49b8c | 8192 | if (mf_are_prereqs_ok(sf->field, &flow, NULL)) { |
254750ce BP |
8193 | union mf_value value; |
8194 | ||
5cb7a798 | 8195 | mf_get_value(sf->field, &flow, &value); |
254750ce BP |
8196 | if (sf->ofs) { |
8197 | bitwise_zero(&value, sf->field->n_bytes, 0, sf->ofs); | |
8198 | } | |
8199 | if (sf->ofs + sf->n_bits < sf->field->n_bytes * 8) { | |
8200 | unsigned int start = sf->ofs + sf->n_bits; | |
8201 | bitwise_zero(&value, sf->field->n_bytes, start, | |
8202 | sf->field->n_bytes * 8 - start); | |
8203 | } | |
8204 | hash = hash_bytes(&value, sf->field->n_bytes, hash); | |
8205 | } else { | |
8206 | hash = hash_int(hash, 0); | |
8207 | } | |
8208 | } | |
8209 | ||
8210 | return hash; | |
8211 | } | |
8212 | ||
8213 | /* Returns an eviction group within 'table' with the given 'id', creating one | |
8214 | * if necessary. */ | |
8215 | static struct eviction_group * | |
8216 | eviction_group_find(struct oftable *table, uint32_t id) | |
15aaf599 | 8217 | OVS_REQUIRES(ofproto_mutex) |
254750ce BP |
8218 | { |
8219 | struct eviction_group *evg; | |
8220 | ||
8221 | HMAP_FOR_EACH_WITH_HASH (evg, id_node, id, &table->eviction_groups_by_id) { | |
8222 | return evg; | |
8223 | } | |
8224 | ||
8225 | evg = xmalloc(sizeof *evg); | |
8226 | hmap_insert(&table->eviction_groups_by_id, &evg->id_node, id); | |
8227 | heap_insert(&table->eviction_groups_by_size, &evg->size_node, | |
8228 | eviction_group_priority(0)); | |
8229 | heap_init(&evg->rules); | |
8230 | ||
8231 | return evg; | |
8232 | } | |
8233 | ||
8234 | /* Returns an eviction priority for 'rule'. The return value should be | |
f70b94de BP |
8235 | * interpreted so that higher priorities make a rule a more attractive |
8236 | * candidate for eviction. */ | |
8237 | static uint64_t | |
dc437090 | 8238 | rule_eviction_priority(struct ofproto *ofproto, struct rule *rule) |
15aaf599 | 8239 | OVS_REQUIRES(ofproto_mutex) |
254750ce | 8240 | { |
f70b94de BP |
8241 | /* Calculate absolute time when this flow will expire. If it will never |
8242 | * expire, then return 0 to make it unevictable. */ | |
dc437090 | 8243 | long long int expiration = LLONG_MAX; |
dc437090 | 8244 | if (rule->hard_timeout) { |
f70b94de BP |
8245 | /* 'modified' needs protection even when we hold 'ofproto_mutex'. */ |
8246 | ovs_mutex_lock(&rule->mutex); | |
8247 | long long int modified = rule->modified; | |
8248 | ovs_mutex_unlock(&rule->mutex); | |
8249 | ||
dc437090 JR |
8250 | expiration = modified + rule->hard_timeout * 1000; |
8251 | } | |
8252 | if (rule->idle_timeout) { | |
8253 | uint64_t packets, bytes; | |
8254 | long long int used; | |
8255 | long long int idle_expiration; | |
8256 | ||
8257 | ofproto->ofproto_class->rule_get_stats(rule, &packets, &bytes, &used); | |
8258 | idle_expiration = used + rule->idle_timeout * 1000; | |
8259 | expiration = MIN(expiration, idle_expiration); | |
8260 | } | |
254750ce BP |
8261 | if (expiration == LLONG_MAX) { |
8262 | return 0; | |
8263 | } | |
8264 | ||
8265 | /* Calculate the time of expiration as a number of (approximate) seconds | |
8266 | * after program startup. | |
8267 | * | |
8268 | * This should work OK for program runs that last UINT32_MAX seconds or | |
8269 | * less. Therefore, please restart OVS at least once every 136 years. */ | |
f70b94de | 8270 | uint32_t expiration_ofs = (expiration >> 10) - (time_boot_msec() >> 10); |
254750ce | 8271 | |
f70b94de BP |
8272 | /* Combine expiration time with OpenFlow "importance" to form a single |
8273 | * priority value. We want flows with relatively low "importance" to be | |
8274 | * evicted before even considering expiration time, so put "importance" in | |
8275 | * the most significant bits and expiration time in the least significant | |
8276 | * bits. | |
8277 | * | |
8278 | * Small 'priority' should be evicted before those with large 'priority'. | |
8279 | * The caller expects the opposite convention (a large return value being | |
8280 | * more attractive for eviction) so we invert it before returning. */ | |
8281 | uint64_t priority = ((uint64_t) rule->importance << 32) + expiration_ofs; | |
8282 | return UINT64_MAX - priority; | |
254750ce BP |
8283 | } |
8284 | ||
8285 | /* Adds 'rule' to an appropriate eviction group for its oftable's | |
8286 | * configuration. Does nothing if 'rule''s oftable doesn't have eviction | |
8287 | * enabled, or if 'rule' is a permanent rule (one that will never expire on its | |
8288 | * own). | |
8289 | * | |
8290 | * The caller must ensure that 'rule' is not already in an eviction group. */ | |
8291 | static void | |
8292 | eviction_group_add_rule(struct rule *rule) | |
15aaf599 | 8293 | OVS_REQUIRES(ofproto_mutex) |
254750ce BP |
8294 | { |
8295 | struct ofproto *ofproto = rule->ofproto; | |
8296 | struct oftable *table = &ofproto->tables[rule->table_id]; | |
a3779dbc | 8297 | bool has_timeout; |
254750ce | 8298 | |
dc437090 JR |
8299 | /* Timeouts may be modified only when holding 'ofproto_mutex'. We have it |
8300 | * so no additional protection is needed. */ | |
a3779dbc | 8301 | has_timeout = rule->hard_timeout || rule->idle_timeout; |
a3779dbc | 8302 | |
82c22d34 | 8303 | if (table->eviction && has_timeout) { |
254750ce BP |
8304 | struct eviction_group *evg; |
8305 | ||
8306 | evg = eviction_group_find(table, eviction_group_hash_rule(rule)); | |
8307 | ||
8308 | rule->eviction_group = evg; | |
8309 | heap_insert(&evg->rules, &rule->evg_node, | |
dc437090 | 8310 | rule_eviction_priority(ofproto, rule)); |
254750ce BP |
8311 | eviction_group_resized(table, evg); |
8312 | } | |
8313 | } | |
8314 | \f | |
d0918789 BP |
8315 | /* oftables. */ |
8316 | ||
8317 | /* Initializes 'table'. */ | |
8318 | static void | |
8319 | oftable_init(struct oftable *table) | |
8320 | { | |
5c67e4af | 8321 | memset(table, 0, sizeof *table); |
d70e8c28 | 8322 | classifier_init(&table->cls, flow_segment_u64s); |
ec1c5c7e | 8323 | table->max_flows = UINT_MAX; |
d79e3d70 | 8324 | table->n_flows = 0; |
82c22d34 BP |
8325 | hmap_init(&table->eviction_groups_by_id); |
8326 | heap_init(&table->eviction_groups_by_size); | |
3c1bb396 | 8327 | atomic_init(&table->miss_config, OFPUTIL_TABLE_MISS_DEFAULT); |
f017d986 | 8328 | |
f017d986 JR |
8329 | classifier_set_prefix_fields(&table->cls, default_prefix_fields, |
8330 | ARRAY_SIZE(default_prefix_fields)); | |
d611866c SH |
8331 | |
8332 | atomic_init(&table->n_matched, 0); | |
8333 | atomic_init(&table->n_missed, 0); | |
d0918789 BP |
8334 | } |
8335 | ||
254750ce | 8336 | /* Destroys 'table', including its classifier and eviction groups. |
d0918789 BP |
8337 | * |
8338 | * The caller is responsible for freeing 'table' itself. */ | |
8339 | static void | |
8340 | oftable_destroy(struct oftable *table) | |
8341 | { | |
cb22974d | 8342 | ovs_assert(classifier_is_empty(&table->cls)); |
82c22d34 | 8343 | |
7394b8fb | 8344 | ovs_mutex_lock(&ofproto_mutex); |
82c22d34 | 8345 | oftable_configure_eviction(table, 0, NULL, 0); |
7394b8fb | 8346 | ovs_mutex_unlock(&ofproto_mutex); |
82c22d34 BP |
8347 | |
8348 | hmap_destroy(&table->eviction_groups_by_id); | |
8349 | heap_destroy(&table->eviction_groups_by_size); | |
d0918789 | 8350 | classifier_destroy(&table->cls); |
254750ce BP |
8351 | free(table->name); |
8352 | } | |
8353 | ||
8354 | /* Changes the name of 'table' to 'name'. If 'name' is NULL or the empty | |
8355 | * string, then 'table' will use its default name. | |
8356 | * | |
8357 | * This only affects the name exposed for a table exposed through the OpenFlow | |
8358 | * OFPST_TABLE (as printed by "ovs-ofctl dump-tables"). */ | |
8359 | static void | |
8360 | oftable_set_name(struct oftable *table, const char *name) | |
8361 | { | |
8362 | if (name && name[0]) { | |
8363 | int len = strnlen(name, OFP_MAX_TABLE_NAME_LEN); | |
8364 | if (!table->name || strncmp(name, table->name, len)) { | |
8365 | free(table->name); | |
8366 | table->name = xmemdup0(name, len); | |
8367 | } | |
8368 | } else { | |
8369 | free(table->name); | |
8370 | table->name = NULL; | |
8371 | } | |
8372 | } | |
8373 | ||
254750ce BP |
8374 | /* oftables support a choice of two policies when adding a rule would cause the |
8375 | * number of flows in the table to exceed the configured maximum number: either | |
8376 | * they can refuse to add the new flow or they can evict some existing flow. | |
8377 | * This function configures the latter policy on 'table', with fairness based | |
8378 | * on the values of the 'n_fields' fields specified in 'fields'. (Specifying | |
8379 | * 'n_fields' as 0 disables fairness.) */ | |
8380 | static void | |
82c22d34 BP |
8381 | oftable_configure_eviction(struct oftable *table, unsigned int eviction, |
8382 | const struct mf_subfield *fields, size_t n_fields) | |
15aaf599 | 8383 | OVS_REQUIRES(ofproto_mutex) |
254750ce | 8384 | { |
254750ce BP |
8385 | struct rule *rule; |
8386 | ||
82c22d34 | 8387 | if ((table->eviction != 0) == (eviction != 0) |
254750ce BP |
8388 | && n_fields == table->n_eviction_fields |
8389 | && (!n_fields | |
8390 | || !memcmp(fields, table->eviction_fields, | |
8391 | n_fields * sizeof *fields))) { | |
82c22d34 BP |
8392 | /* The set of eviction fields did not change. If 'eviction' changed, |
8393 | * it remains nonzero, so that we can just update table->eviction | |
8394 | * without fussing with the eviction groups. */ | |
8395 | table->eviction = eviction; | |
254750ce BP |
8396 | return; |
8397 | } | |
8398 | ||
82c22d34 BP |
8399 | /* Destroy existing eviction groups, then destroy and recreate data |
8400 | * structures to recover memory. */ | |
8401 | struct eviction_group *evg, *next; | |
8402 | HMAP_FOR_EACH_SAFE (evg, next, id_node, &table->eviction_groups_by_id) { | |
8403 | eviction_group_destroy(table, evg); | |
8404 | } | |
8405 | hmap_destroy(&table->eviction_groups_by_id); | |
254750ce | 8406 | hmap_init(&table->eviction_groups_by_id); |
82c22d34 | 8407 | heap_destroy(&table->eviction_groups_by_size); |
254750ce BP |
8408 | heap_init(&table->eviction_groups_by_size); |
8409 | ||
82c22d34 BP |
8410 | /* Replace eviction groups by the new ones, if there is a change. Free the |
8411 | * old fields only after allocating the new ones, because 'fields == | |
8412 | * table->eviction_fields' is possible. */ | |
8413 | struct mf_subfield *old_fields = table->eviction_fields; | |
8414 | table->n_eviction_fields = n_fields; | |
8415 | table->eviction_fields = (fields | |
8416 | ? xmemdup(fields, n_fields * sizeof *fields) | |
8417 | : NULL); | |
8418 | free(old_fields); | |
8419 | ||
8420 | /* Add the new eviction groups, if enabled. */ | |
8421 | table->eviction = eviction; | |
8422 | if (table->eviction) { | |
8423 | table->eviction_group_id_basis = random_uint32(); | |
8424 | CLS_FOR_EACH (rule, cr, &table->cls) { | |
8425 | eviction_group_add_rule(rule); | |
8426 | } | |
254750ce | 8427 | } |
d0918789 BP |
8428 | } |
8429 | ||
bc5e6a91 JR |
8430 | /* Inserts 'rule' from the ofproto data structures BEFORE caller has inserted |
8431 | * it to the classifier. */ | |
8432 | static void | |
8433 | ofproto_rule_insert__(struct ofproto *ofproto, struct rule *rule) | |
8434 | OVS_REQUIRES(ofproto_mutex) | |
8435 | { | |
8436 | const struct rule_actions *actions = rule_get_actions(rule); | |
8437 | ||
30d0452c JR |
8438 | /* A rule may not be reinserted. */ |
8439 | ovs_assert(rule->state == RULE_INITIALIZED); | |
39c94593 | 8440 | |
bc5e6a91 | 8441 | if (rule->hard_timeout || rule->idle_timeout) { |
417e7e66 | 8442 | ovs_list_insert(&ofproto->expirable, &rule->expirable); |
bc5e6a91 JR |
8443 | } |
8444 | cookies_insert(ofproto, rule); | |
8445 | eviction_group_add_rule(rule); | |
8446 | if (actions->has_meter) { | |
8447 | meter_insert_rule(rule); | |
8448 | } | |
cc099268 JR |
8449 | if (actions->has_groups) { |
8450 | const struct ofpact_group *a; | |
8451 | OFPACT_FOR_EACH_TYPE_FLATTENED (a, GROUP, actions->ofpacts, | |
8452 | actions->ofpacts_len) { | |
8453 | struct ofgroup *group; | |
8454 | ||
5d08a275 JR |
8455 | group = ofproto_group_lookup(ofproto, a->group_id, OVS_VERSION_MAX, |
8456 | false); | |
cc099268 JR |
8457 | ovs_assert(group != NULL); |
8458 | group_add_rule(group, rule); | |
8459 | } | |
8460 | } | |
8461 | ||
30d0452c | 8462 | rule->state = RULE_INSERTED; |
bc5e6a91 JR |
8463 | } |
8464 | ||
6787a49f JR |
8465 | /* Removes 'rule' from the ofproto data structures. Caller may have deferred |
8466 | * the removal from the classifier. */ | |
d0918789 | 8467 | static void |
802f84ff | 8468 | ofproto_rule_remove__(struct ofproto *ofproto, struct rule *rule) |
15aaf599 | 8469 | OVS_REQUIRES(ofproto_mutex) |
d0918789 | 8470 | { |
30d0452c | 8471 | ovs_assert(rule->state == RULE_INSERTED); |
39c94593 | 8472 | |
98eaac36 | 8473 | cookies_remove(ofproto, rule); |
2c916028 | 8474 | |
254750ce | 8475 | eviction_group_remove_rule(rule); |
417e7e66 BW |
8476 | if (!ovs_list_is_empty(&rule->expirable)) { |
8477 | ovs_list_remove(&rule->expirable); | |
e503cc19 | 8478 | } |
417e7e66 BW |
8479 | if (!ovs_list_is_empty(&rule->meter_list_node)) { |
8480 | ovs_list_remove(&rule->meter_list_node); | |
8481 | ovs_list_init(&rule->meter_list_node); | |
9cae45dc | 8482 | } |
802f84ff | 8483 | |
cc099268 JR |
8484 | /* Remove the rule from any groups, except from the group that is being |
8485 | * deleted, if any. */ | |
8486 | const struct rule_actions *actions = rule_get_actions(rule); | |
8487 | ||
8488 | if (actions->has_groups) { | |
8489 | const struct ofpact_group *a; | |
8490 | ||
8483173b | 8491 | OFPACT_FOR_EACH_TYPE_FLATTENED(a, GROUP, actions->ofpacts, |
cc099268 JR |
8492 | actions->ofpacts_len) { |
8493 | struct ofgroup *group; | |
8494 | ||
5d08a275 JR |
8495 | group = ofproto_group_lookup(ofproto, a->group_id, OVS_VERSION_MAX, |
8496 | false); | |
cc099268 JR |
8497 | ovs_assert(group); |
8498 | ||
8499 | /* Leave the rule for the group that is being deleted, if any, | |
8500 | * as we still need the list of rules for clean-up. */ | |
8501 | if (!group->being_deleted) { | |
8502 | group_remove_rule(group, rule); | |
8503 | } | |
8504 | } | |
8505 | } | |
8506 | ||
30d0452c | 8507 | rule->state = RULE_REMOVED; |
0b4f2078 | 8508 | } |
d0918789 | 8509 | \f |
abe529af | 8510 | /* unixctl commands. */ |
7aa697dd | 8511 | |
abe529af | 8512 | struct ofproto * |
2ac6bedd | 8513 | ofproto_lookup(const char *name) |
7aa697dd | 8514 | { |
2ac6bedd | 8515 | struct ofproto *ofproto; |
7aa697dd | 8516 | |
2ac6bedd BP |
8517 | HMAP_FOR_EACH_WITH_HASH (ofproto, hmap_node, hash_string(name, 0), |
8518 | &all_ofprotos) { | |
8519 | if (!strcmp(ofproto->name, name)) { | |
8520 | return ofproto; | |
8521 | } | |
7aa697dd | 8522 | } |
2ac6bedd | 8523 | return NULL; |
7aa697dd BP |
8524 | } |
8525 | ||
8526 | static void | |
0e15264f BP |
8527 | ofproto_unixctl_list(struct unixctl_conn *conn, int argc OVS_UNUSED, |
8528 | const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED) | |
7aa697dd | 8529 | { |
7aa697dd | 8530 | struct ofproto *ofproto; |
7aa697dd | 8531 | struct ds results; |
7aa697dd | 8532 | |
7aa697dd | 8533 | ds_init(&results); |
2ac6bedd BP |
8534 | HMAP_FOR_EACH (ofproto, hmap_node, &all_ofprotos) { |
8535 | ds_put_format(&results, "%s\n", ofproto->name); | |
7aa697dd | 8536 | } |
bde9f75d | 8537 | unixctl_command_reply(conn, ds_cstr(&results)); |
7aa697dd | 8538 | ds_destroy(&results); |
7aa697dd BP |
8539 | } |
8540 | ||
8541 | static void | |
8542 | ofproto_unixctl_init(void) | |
8543 | { | |
8544 | static bool registered; | |
8545 | if (registered) { | |
8546 | return; | |
8547 | } | |
8548 | registered = true; | |
8549 | ||
0e15264f BP |
8550 | unixctl_command_register("ofproto/list", "", 0, 0, |
8551 | ofproto_unixctl_list, NULL); | |
064af421 | 8552 | } |