]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
cb22974d | 2 | * Copyright (c) 2009, 2010, 2011, 2012, 2013 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> | |
19 | #include "ofproto.h" | |
20 | #include <errno.h> | |
21 | #include <inttypes.h> | |
064af421 BP |
22 | #include <stdbool.h> |
23 | #include <stdlib.h> | |
448a4b2f | 24 | #include <unistd.h> |
52a90c29 | 25 | #include "bitmap.h" |
10a24935 | 26 | #include "byte-order.h" |
064af421 | 27 | #include "classifier.h" |
19a87e36 | 28 | #include "connmgr.h" |
064af421 | 29 | #include "coverage.h" |
4f2cad2c | 30 | #include "dynamic-string.h" |
ca0f572c BP |
31 | #include "hash.h" |
32 | #include "hmap.h" | |
254750ce | 33 | #include "meta-flow.h" |
064af421 | 34 | #include "netdev.h" |
09246b99 | 35 | #include "nx-match.h" |
f25d0cf3 | 36 | #include "ofp-actions.h" |
90bf1e07 | 37 | #include "ofp-errors.h" |
982697a4 | 38 | #include "ofp-msgs.h" |
064af421 | 39 | #include "ofp-print.h" |
fa37b408 | 40 | #include "ofp-util.h" |
064af421 | 41 | #include "ofpbuf.h" |
5bee6e26 | 42 | #include "ofproto-provider.h" |
064af421 BP |
43 | #include "openflow/nicira-ext.h" |
44 | #include "openflow/openflow.h" | |
064af421 BP |
45 | #include "packets.h" |
46 | #include "pinsched.h" | |
47 | #include "pktbuf.h" | |
48 | #include "poll-loop.h" | |
254750ce | 49 | #include "random.h" |
064af421 | 50 | #include "shash.h" |
0d085684 | 51 | #include "simap.h" |
b3c01ed3 | 52 | #include "sset.h" |
064af421 | 53 | #include "timeval.h" |
c4617b3c | 54 | #include "unaligned.h" |
4f2cad2c | 55 | #include "unixctl.h" |
5136ce49 | 56 | #include "vlog.h" |
064af421 | 57 | |
d98e6007 | 58 | VLOG_DEFINE_THIS_MODULE(ofproto); |
064af421 | 59 | |
d76f09ea | 60 | COVERAGE_DEFINE(ofproto_flush); |
d76f09ea BP |
61 | COVERAGE_DEFINE(ofproto_packet_out); |
62 | COVERAGE_DEFINE(ofproto_queue_req); | |
63 | COVERAGE_DEFINE(ofproto_recv_openflow); | |
64 | COVERAGE_DEFINE(ofproto_reinit_ports); | |
d76f09ea BP |
65 | COVERAGE_DEFINE(ofproto_update_port); |
66 | ||
7ee20df1 BP |
67 | enum ofproto_state { |
68 | S_OPENFLOW, /* Processing OpenFlow commands. */ | |
254750ce | 69 | S_EVICT, /* Evicting flows from over-limit tables. */ |
7ee20df1 BP |
70 | S_FLUSH, /* Deleting all flow table rules. */ |
71 | }; | |
72 | ||
73 | enum ofoperation_type { | |
74 | OFOPERATION_ADD, | |
75 | OFOPERATION_DELETE, | |
b277c7cc BP |
76 | OFOPERATION_MODIFY, |
77 | OFOPERATION_REPLACE | |
7ee20df1 BP |
78 | }; |
79 | ||
80 | /* A single OpenFlow request can execute any number of operations. The | |
81 | * ofopgroup maintain OpenFlow state common to all of the operations, e.g. the | |
82 | * ofconn to which an error reply should be sent if necessary. | |
83 | * | |
84 | * ofproto initiates some operations internally. These operations are still | |
85 | * assigned to groups but will not have an associated ofconn. */ | |
86 | struct ofopgroup { | |
87 | struct ofproto *ofproto; /* Owning ofproto. */ | |
88 | struct list ofproto_node; /* In ofproto's "pending" list. */ | |
89 | struct list ops; /* List of "struct ofoperation"s. */ | |
e615b0a3 | 90 | int n_running; /* Number of ops still pending. */ |
7ee20df1 BP |
91 | |
92 | /* Data needed to send OpenFlow reply on failure or to send a buffered | |
93 | * packet on success. | |
94 | * | |
95 | * If list_is_empty(ofconn_node) then this ofopgroup never had an | |
96 | * associated ofconn or its ofconn's connection dropped after it initiated | |
97 | * the operation. In the latter case 'ofconn' is a wild pointer that | |
98 | * refers to freed memory, so the 'ofconn' member must be used only if | |
99 | * !list_is_empty(ofconn_node). | |
100 | */ | |
101 | struct list ofconn_node; /* In ofconn's list of pending opgroups. */ | |
102 | struct ofconn *ofconn; /* ofconn for reply (but see note above). */ | |
103 | struct ofp_header *request; /* Original request (truncated at 64 bytes). */ | |
104 | uint32_t buffer_id; /* Buffer id from original request. */ | |
7ee20df1 BP |
105 | }; |
106 | ||
7024dffe BP |
107 | static struct ofopgroup *ofopgroup_create_unattached(struct ofproto *); |
108 | static struct ofopgroup *ofopgroup_create(struct ofproto *, struct ofconn *, | |
109 | const struct ofp_header *, | |
110 | uint32_t buffer_id); | |
7ee20df1 | 111 | static void ofopgroup_submit(struct ofopgroup *); |
e615b0a3 | 112 | static void ofopgroup_complete(struct ofopgroup *); |
7ee20df1 BP |
113 | |
114 | /* A single flow table operation. */ | |
115 | struct ofoperation { | |
116 | struct ofopgroup *group; /* Owning group. */ | |
117 | struct list group_node; /* In ofopgroup's "ops" list. */ | |
118 | struct hmap_node hmap_node; /* In ofproto's "deletions" hmap. */ | |
119 | struct rule *rule; /* Rule being operated upon. */ | |
120 | enum ofoperation_type type; /* Type of operation. */ | |
08043761 | 121 | |
b277c7cc BP |
122 | /* OFOPERATION_MODIFY, OFOPERATION_REPLACE: The old actions, if the actions |
123 | * are changing. */ | |
6f00e29b | 124 | struct rule_actions *actions; |
08043761 | 125 | |
2b07c8b1 BP |
126 | /* OFOPERATION_DELETE. */ |
127 | enum ofp_flow_removed_reason reason; /* Reason flow was removed. */ | |
128 | ||
3f517bcd DB |
129 | ovs_be64 flow_cookie; /* Rule's old flow cookie. */ |
130 | uint16_t idle_timeout; /* Rule's old idle timeout. */ | |
131 | uint16_t hard_timeout; /* Rule's old hard timeout. */ | |
132 | enum ofputil_flow_mod_flags flags; /* Rule's old flags. */ | |
133 | enum ofperr error; /* 0 if no error. */ | |
7ee20df1 BP |
134 | }; |
135 | ||
a7d94793 BP |
136 | static struct ofoperation *ofoperation_create(struct ofopgroup *, |
137 | struct rule *, | |
2b07c8b1 BP |
138 | enum ofoperation_type, |
139 | enum ofp_flow_removed_reason); | |
7ee20df1 BP |
140 | static void ofoperation_destroy(struct ofoperation *); |
141 | ||
d0918789 BP |
142 | /* oftable. */ |
143 | static void oftable_init(struct oftable *); | |
144 | static void oftable_destroy(struct oftable *); | |
878ae780 | 145 | |
254750ce BP |
146 | static void oftable_set_name(struct oftable *, const char *name); |
147 | ||
148 | static void oftable_disable_eviction(struct oftable *); | |
149 | static void oftable_enable_eviction(struct oftable *, | |
150 | const struct mf_subfield *fields, | |
151 | size_t n_fields); | |
152 | ||
15aaf599 | 153 | static void oftable_remove_rule(struct rule *rule) OVS_REQUIRES(ofproto_mutex); |
8b81d1ef | 154 | static void oftable_remove_rule__(struct ofproto *, struct rule *) |
15aaf599 | 155 | OVS_REQUIRES(ofproto_mutex); |
b277c7cc | 156 | static void oftable_insert_rule(struct rule *); |
064af421 | 157 | |
254750ce BP |
158 | /* A set of rules within a single OpenFlow table (oftable) that have the same |
159 | * values for the oftable's eviction_fields. A rule to be evicted, when one is | |
160 | * needed, is taken from the eviction group that contains the greatest number | |
161 | * of rules. | |
162 | * | |
163 | * An oftable owns any number of eviction groups, each of which contains any | |
164 | * number of rules. | |
165 | * | |
166 | * Membership in an eviction group is imprecise, based on the hash of the | |
167 | * oftable's eviction_fields (in the eviction_group's id_node.hash member). | |
168 | * That is, if two rules have different eviction_fields, but those | |
169 | * eviction_fields hash to the same value, then they will belong to the same | |
170 | * eviction_group anyway. | |
171 | * | |
172 | * (When eviction is not enabled on an oftable, we don't track any eviction | |
173 | * groups, to save time and space.) */ | |
174 | struct eviction_group { | |
175 | struct hmap_node id_node; /* In oftable's "eviction_groups_by_id". */ | |
176 | struct heap_node size_node; /* In oftable's "eviction_groups_by_size". */ | |
177 | struct heap rules; /* Contains "struct rule"s. */ | |
178 | }; | |
179 | ||
15aaf599 BP |
180 | static bool choose_rule_to_evict(struct oftable *table, struct rule **rulep); |
181 | static void ofproto_evict(struct ofproto *) OVS_EXCLUDED(ofproto_mutex); | |
254750ce | 182 | static uint32_t rule_eviction_priority(struct rule *); |
b277c7cc BP |
183 | static void eviction_group_add_rule(struct rule *); |
184 | static void eviction_group_remove_rule(struct rule *); | |
f29152ca | 185 | |
a9b22b7f BP |
186 | /* Criteria that flow_mod and other operations use for selecting rules on |
187 | * which to operate. */ | |
188 | struct rule_criteria { | |
189 | /* An OpenFlow table or 255 for all tables. */ | |
190 | uint8_t table_id; | |
191 | ||
192 | /* OpenFlow matching criteria. Interpreted different in "loose" way by | |
193 | * collect_rules_loose() and "strict" way by collect_rules_strict(), as | |
194 | * defined in the OpenFlow spec. */ | |
195 | struct cls_rule cr; | |
196 | ||
197 | /* Matching criteria for the OpenFlow cookie. Consider a bit B in a rule's | |
198 | * cookie and the corresponding bits C in 'cookie' and M in 'cookie_mask'. | |
199 | * The rule will not be selected if M is 1 and B != C. */ | |
200 | ovs_be64 cookie; | |
201 | ovs_be64 cookie_mask; | |
202 | ||
203 | /* Selection based on actions within a rule: | |
204 | * | |
205 | * If out_port != OFPP_ANY, selects only rules that output to out_port. | |
206 | * If out_group != OFPG_ALL, select only rules that output to out_group. */ | |
207 | ofp_port_t out_port; | |
208 | uint32_t out_group; | |
209 | }; | |
210 | ||
211 | static void rule_criteria_init(struct rule_criteria *, uint8_t table_id, | |
212 | const struct match *match, | |
213 | unsigned int priority, | |
214 | ovs_be64 cookie, ovs_be64 cookie_mask, | |
215 | ofp_port_t out_port, uint32_t out_group); | |
216 | static void rule_criteria_destroy(struct rule_criteria *); | |
217 | ||
15aaf599 BP |
218 | /* A packet that needs to be passed to rule_execute(). |
219 | * | |
220 | * (We can't do this immediately from ofopgroup_complete() because that holds | |
221 | * ofproto_mutex, which rule_execute() needs released.) */ | |
35412852 BP |
222 | struct rule_execute { |
223 | struct list list_node; /* In struct ofproto's "rule_executes" list. */ | |
224 | struct rule *rule; /* Owns a reference to the rule. */ | |
225 | ofp_port_t in_port; | |
226 | struct ofpbuf *packet; /* Owns the packet. */ | |
227 | }; | |
228 | ||
15aaf599 | 229 | static void run_rule_executes(struct ofproto *) OVS_EXCLUDED(ofproto_mutex); |
35412852 BP |
230 | static void destroy_rule_executes(struct ofproto *); |
231 | ||
d0918789 | 232 | /* ofport. */ |
15aaf599 | 233 | static void ofport_destroy__(struct ofport *) OVS_EXCLUDED(ofproto_mutex); |
d0918789 BP |
234 | static void ofport_destroy(struct ofport *); |
235 | ||
236 | static void update_port(struct ofproto *, const char *devname); | |
237 | static int init_ports(struct ofproto *); | |
238 | static void reinit_ports(struct ofproto *); | |
7ee20df1 | 239 | |
fdcea803 GS |
240 | static long long int ofport_get_usage(const struct ofproto *, |
241 | ofp_port_t ofp_port); | |
242 | static void ofport_set_usage(struct ofproto *, ofp_port_t ofp_port, | |
243 | long long int last_used); | |
865b26a4 | 244 | static void ofport_remove_usage(struct ofproto *, ofp_port_t ofp_port); |
fdcea803 GS |
245 | |
246 | /* Ofport usage. | |
247 | * | |
248 | * Keeps track of the currently used and recently used ofport values and is | |
249 | * used to prevent immediate recycling of ofport values. */ | |
250 | struct ofport_usage { | |
251 | struct hmap_node hmap_node; /* In struct ofproto's "ofport_usage" hmap. */ | |
252 | ofp_port_t ofp_port; /* OpenFlow port number. */ | |
253 | long long int last_used; /* Last time the 'ofp_port' was used. LLONG_MAX | |
254 | represents in-use ofports. */ | |
255 | }; | |
256 | ||
254750ce BP |
257 | /* rule. */ |
258 | static void ofproto_rule_destroy__(struct rule *); | |
259 | static void ofproto_rule_send_removed(struct rule *, uint8_t reason); | |
260 | static bool rule_is_modifiable(const struct rule *); | |
254750ce | 261 | |
d0918789 | 262 | /* OpenFlow. */ |
90bf1e07 | 263 | static enum ofperr add_flow(struct ofproto *, struct ofconn *, |
e3b56933 | 264 | struct ofputil_flow_mod *, |
90bf1e07 | 265 | const struct ofp_header *); |
b277c7cc BP |
266 | static enum ofperr modify_flows__(struct ofproto *, struct ofconn *, |
267 | struct ofputil_flow_mod *, | |
a8e547c1 BP |
268 | const struct ofp_header *, |
269 | const struct rule_collection *); | |
ad3efdcb EJ |
270 | static void delete_flow__(struct rule *rule, struct ofopgroup *, |
271 | enum ofp_flow_removed_reason) | |
15aaf599 | 272 | OVS_REQUIRES(ofproto_mutex); |
7395c052 | 273 | static enum ofperr add_group(struct ofproto *, struct ofputil_group_mod *); |
e03248b7 | 274 | static bool handle_openflow(struct ofconn *, const struct ofpbuf *); |
90bf1e07 | 275 | static enum ofperr handle_flow_mod__(struct ofproto *, struct ofconn *, |
e3b56933 | 276 | struct ofputil_flow_mod *, |
15aaf599 BP |
277 | const struct ofp_header *) |
278 | OVS_EXCLUDED(ofproto_mutex); | |
65e0be10 BP |
279 | static void calc_duration(long long int start, long long int now, |
280 | uint32_t *sec, uint32_t *nsec); | |
f29152ca | 281 | |
d0918789 BP |
282 | /* ofproto. */ |
283 | static uint64_t pick_datapath_id(const struct ofproto *); | |
284 | static uint64_t pick_fallback_dpid(void); | |
285 | static void ofproto_destroy__(struct ofproto *); | |
ada3428f | 286 | static void update_mtu(struct ofproto *, struct ofport *); |
6b3f7f02 | 287 | static void meter_delete(struct ofproto *, uint32_t first, uint32_t last); |
f29152ca | 288 | |
d0918789 | 289 | /* unixctl. */ |
abe529af | 290 | static void ofproto_unixctl_init(void); |
f29152ca | 291 | |
abe529af BP |
292 | /* All registered ofproto classes, in probe order. */ |
293 | static const struct ofproto_class **ofproto_classes; | |
294 | static size_t n_ofproto_classes; | |
295 | static size_t allocated_ofproto_classes; | |
7aa697dd | 296 | |
15aaf599 BP |
297 | /* Global lock that protects all flow table operations. */ |
298 | struct ovs_mutex ofproto_mutex = OVS_MUTEX_INITIALIZER; | |
abe7b10f | 299 | |
380f49c4 | 300 | unsigned flow_eviction_threshold = OFPROTO_FLOW_EVICTION_THRESHOLD_DEFAULT; |
448a4b2f | 301 | unsigned n_handler_threads; |
7155fa52 | 302 | enum ofproto_flow_miss_model flow_miss_model = OFPROTO_HANDLE_MISS_AUTO; |
380f49c4 | 303 | |
f797957a | 304 | /* Map from datapath name to struct ofproto, for use by unixctl commands. */ |
abe529af | 305 | static struct hmap all_ofprotos = HMAP_INITIALIZER(&all_ofprotos); |
ebe482fd | 306 | |
e1b1d06a JP |
307 | /* Initial mappings of port to OpenFlow number mappings. */ |
308 | static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports); | |
309 | ||
abe529af | 310 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); |
f29152ca | 311 | |
40358701 GS |
312 | /* The default value of true waits for flow restore. */ |
313 | static bool flow_restore_wait = true; | |
314 | ||
b0408fca JP |
315 | /* Must be called to initialize the ofproto library. |
316 | * | |
317 | * The caller may pass in 'iface_hints', which contains an shash of | |
318 | * "iface_hint" elements indexed by the interface's name. The provider | |
319 | * may use these hints to describe the startup configuration in order to | |
320 | * reinitialize its state. The caller owns the provided data, so a | |
321 | * provider will make copies of anything required. An ofproto provider | |
322 | * will remove any existing state that is not described by the hint, and | |
323 | * may choose to remove it all. */ | |
324 | void | |
325 | ofproto_init(const struct shash *iface_hints) | |
abe529af | 326 | { |
e1b1d06a | 327 | struct shash_node *node; |
b0408fca | 328 | size_t i; |
f29152ca | 329 | |
b0408fca JP |
330 | ofproto_class_register(&ofproto_dpif_class); |
331 | ||
e1b1d06a JP |
332 | /* Make a local copy, since we don't own 'iface_hints' elements. */ |
333 | SHASH_FOR_EACH(node, iface_hints) { | |
334 | const struct iface_hint *orig_hint = node->data; | |
335 | struct iface_hint *new_hint = xmalloc(sizeof *new_hint); | |
336 | const char *br_type = ofproto_normalize_type(orig_hint->br_type); | |
337 | ||
338 | new_hint->br_name = xstrdup(orig_hint->br_name); | |
339 | new_hint->br_type = xstrdup(br_type); | |
340 | new_hint->ofp_port = orig_hint->ofp_port; | |
341 | ||
342 | shash_add(&init_ofp_ports, node->name, new_hint); | |
343 | } | |
344 | ||
b0408fca | 345 | for (i = 0; i < n_ofproto_classes; i++) { |
e1b1d06a | 346 | ofproto_classes[i]->init(&init_ofp_ports); |
abe529af BP |
347 | } |
348 | } | |
f29152ca | 349 | |
abe529af BP |
350 | /* 'type' should be a normalized datapath type, as returned by |
351 | * ofproto_normalize_type(). Returns the corresponding ofproto_class | |
352 | * structure, or a null pointer if there is none registered for 'type'. */ | |
353 | static const struct ofproto_class * | |
354 | ofproto_class_find__(const char *type) | |
355 | { | |
356 | size_t i; | |
f29152ca | 357 | |
abe529af BP |
358 | for (i = 0; i < n_ofproto_classes; i++) { |
359 | const struct ofproto_class *class = ofproto_classes[i]; | |
360 | struct sset types; | |
361 | bool found; | |
064af421 | 362 | |
abe529af BP |
363 | sset_init(&types); |
364 | class->enumerate_types(&types); | |
365 | found = sset_contains(&types, type); | |
366 | sset_destroy(&types); | |
bcf84111 | 367 | |
abe529af BP |
368 | if (found) { |
369 | return class; | |
370 | } | |
371 | } | |
372 | VLOG_WARN("unknown datapath type %s", type); | |
373 | return NULL; | |
374 | } | |
064af421 | 375 | |
abe529af BP |
376 | /* Registers a new ofproto class. After successful registration, new ofprotos |
377 | * of that type can be created using ofproto_create(). */ | |
378 | int | |
379 | ofproto_class_register(const struct ofproto_class *new_class) | |
380 | { | |
381 | size_t i; | |
7aa697dd | 382 | |
abe529af BP |
383 | for (i = 0; i < n_ofproto_classes; i++) { |
384 | if (ofproto_classes[i] == new_class) { | |
385 | return EEXIST; | |
386 | } | |
387 | } | |
064af421 | 388 | |
abe529af BP |
389 | if (n_ofproto_classes >= allocated_ofproto_classes) { |
390 | ofproto_classes = x2nrealloc(ofproto_classes, | |
391 | &allocated_ofproto_classes, | |
392 | sizeof *ofproto_classes); | |
393 | } | |
394 | ofproto_classes[n_ofproto_classes++] = new_class; | |
395 | return 0; | |
396 | } | |
064af421 | 397 | |
abe529af BP |
398 | /* Unregisters a datapath provider. 'type' must have been previously |
399 | * registered and not currently be in use by any ofprotos. After | |
400 | * unregistration new datapaths of that type cannot be opened using | |
401 | * ofproto_create(). */ | |
402 | int | |
403 | ofproto_class_unregister(const struct ofproto_class *class) | |
404 | { | |
405 | size_t i; | |
76ce9432 | 406 | |
abe529af BP |
407 | for (i = 0; i < n_ofproto_classes; i++) { |
408 | if (ofproto_classes[i] == class) { | |
409 | for (i++; i < n_ofproto_classes; i++) { | |
410 | ofproto_classes[i - 1] = ofproto_classes[i]; | |
411 | } | |
412 | n_ofproto_classes--; | |
413 | return 0; | |
414 | } | |
415 | } | |
416 | VLOG_WARN("attempted to unregister an ofproto class that is not " | |
417 | "registered"); | |
418 | return EAFNOSUPPORT; | |
419 | } | |
4a4cdb3b | 420 | |
f79e673f BP |
421 | /* Clears 'types' and enumerates all registered ofproto types into it. The |
422 | * caller must first initialize the sset. */ | |
423 | void | |
424 | ofproto_enumerate_types(struct sset *types) | |
425 | { | |
abe529af | 426 | size_t i; |
064af421 | 427 | |
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; | |
454 | } | |
7aa697dd | 455 | |
064af421 | 456 | int |
abe529af | 457 | ofproto_create(const char *datapath_name, const char *datapath_type, |
064af421 BP |
458 | struct ofproto **ofprotop) |
459 | { | |
abe529af BP |
460 | const struct ofproto_class *class; |
461 | struct ofproto *ofproto; | |
064af421 | 462 | int error; |
c2f0373a | 463 | int i; |
064af421 BP |
464 | |
465 | *ofprotop = NULL; | |
466 | ||
7aa697dd BP |
467 | ofproto_unixctl_init(); |
468 | ||
abe529af BP |
469 | datapath_type = ofproto_normalize_type(datapath_type); |
470 | class = ofproto_class_find__(datapath_type); | |
471 | if (!class) { | |
472 | VLOG_WARN("could not create datapath %s of unknown type %s", | |
473 | datapath_name, datapath_type); | |
474 | return EAFNOSUPPORT; | |
064af421 BP |
475 | } |
476 | ||
abe529af BP |
477 | ofproto = class->alloc(); |
478 | if (!ofproto) { | |
479 | VLOG_ERR("failed to allocate datapath %s of type %s", | |
480 | datapath_name, datapath_type); | |
481 | return ENOMEM; | |
482 | } | |
483 | ||
484 | /* Initialize. */ | |
abe7b10f | 485 | ovs_mutex_lock(&ofproto_mutex); |
abe529af BP |
486 | memset(ofproto, 0, sizeof *ofproto); |
487 | ofproto->ofproto_class = class; | |
488 | ofproto->name = xstrdup(datapath_name); | |
489 | ofproto->type = xstrdup(datapath_type); | |
490 | hmap_insert(&all_ofprotos, &ofproto->hmap_node, | |
491 | hash_string(ofproto->name, 0)); | |
492 | ofproto->datapath_id = 0; | |
8402c74b | 493 | ofproto->forward_bpdu = false; |
abe529af | 494 | ofproto->fallback_dpid = pick_fallback_dpid(); |
061bfea4 BP |
495 | ofproto->mfr_desc = NULL; |
496 | ofproto->hw_desc = NULL; | |
497 | ofproto->sw_desc = NULL; | |
498 | ofproto->serial_desc = NULL; | |
499 | ofproto->dp_desc = NULL; | |
7257b535 | 500 | ofproto->frag_handling = OFPC_FRAG_NORMAL; |
abe529af | 501 | hmap_init(&ofproto->ports); |
fdcea803 | 502 | hmap_init(&ofproto->ofport_usage); |
abe529af | 503 | shash_init(&ofproto->port_by_name); |
e1b1d06a | 504 | simap_init(&ofproto->ofp_requests); |
430dbb14 | 505 | ofproto->max_ports = ofp_to_u16(OFPP_MAX); |
448c2fa8 | 506 | ofproto->eviction_group_timer = LLONG_MIN; |
6c1491fb BP |
507 | ofproto->tables = NULL; |
508 | ofproto->n_tables = 0; | |
98eaac36 | 509 | hindex_init(&ofproto->cookies); |
e503cc19 | 510 | list_init(&ofproto->expirable); |
abe529af | 511 | ofproto->connmgr = connmgr_create(ofproto, datapath_name, datapath_name); |
7ee20df1 BP |
512 | ofproto->state = S_OPENFLOW; |
513 | list_init(&ofproto->pending); | |
dd5616b3 | 514 | ofproto->n_pending = 0; |
7ee20df1 | 515 | hmap_init(&ofproto->deletions); |
35412852 | 516 | guarded_list_init(&ofproto->rule_executes); |
a5b8d268 BP |
517 | ofproto->n_add = ofproto->n_delete = ofproto->n_modify = 0; |
518 | ofproto->first_op = ofproto->last_op = LLONG_MIN; | |
519 | ofproto->next_op_report = LLONG_MAX; | |
520 | ofproto->op_backoff = LLONG_MIN; | |
52a90c29 BP |
521 | ofproto->vlan_bitmap = NULL; |
522 | ofproto->vlans_changed = false; | |
ada3428f | 523 | ofproto->min_mtu = INT_MAX; |
7395c052 NZ |
524 | ovs_rwlock_init(&ofproto->groups_rwlock); |
525 | hmap_init(&ofproto->groups); | |
abe7b10f | 526 | ovs_mutex_unlock(&ofproto_mutex); |
abe529af | 527 | |
0f5f95a9 | 528 | error = ofproto->ofproto_class->construct(ofproto); |
19a87e36 | 529 | if (error) { |
abe529af | 530 | VLOG_ERR("failed to open datapath %s: %s", |
10a89ef0 | 531 | datapath_name, ovs_strerror(error)); |
abe529af | 532 | ofproto_destroy__(ofproto); |
19a87e36 BP |
533 | return error; |
534 | } | |
073e2a6f | 535 | |
c2f0373a | 536 | /* Check that hidden tables, if any, are at the end. */ |
cb22974d | 537 | ovs_assert(ofproto->n_tables); |
c2f0373a BP |
538 | for (i = 0; i + 1 < ofproto->n_tables; i++) { |
539 | enum oftable_flags flags = ofproto->tables[i].flags; | |
540 | enum oftable_flags next_flags = ofproto->tables[i + 1].flags; | |
541 | ||
cb22974d | 542 | ovs_assert(!(flags & OFTABLE_HIDDEN) || next_flags & OFTABLE_HIDDEN); |
c2f0373a | 543 | } |
19a87e36 | 544 | |
abe529af | 545 | ofproto->datapath_id = pick_datapath_id(ofproto); |
abe529af | 546 | init_ports(ofproto); |
7aa697dd | 547 | |
9cae45dc JR |
548 | /* Initialize meters table. */ |
549 | if (ofproto->ofproto_class->meter_get_features) { | |
550 | ofproto->ofproto_class->meter_get_features(ofproto, | |
551 | &ofproto->meter_features); | |
552 | } else { | |
553 | memset(&ofproto->meter_features, 0, sizeof ofproto->meter_features); | |
554 | } | |
555 | ofproto->meters = xzalloc((ofproto->meter_features.max_meters + 1) | |
556 | * sizeof(struct meter *)); | |
557 | ||
abe529af | 558 | *ofprotop = ofproto; |
064af421 BP |
559 | return 0; |
560 | } | |
561 | ||
91858960 BP |
562 | /* Must be called (only) by an ofproto implementation in its constructor |
563 | * function. See the large comment on 'construct' in struct ofproto_class for | |
564 | * details. */ | |
0f5f95a9 BP |
565 | void |
566 | ofproto_init_tables(struct ofproto *ofproto, int n_tables) | |
567 | { | |
568 | struct oftable *table; | |
569 | ||
cb22974d BP |
570 | ovs_assert(!ofproto->n_tables); |
571 | ovs_assert(n_tables >= 1 && n_tables <= 255); | |
0f5f95a9 BP |
572 | |
573 | ofproto->n_tables = n_tables; | |
574 | ofproto->tables = xmalloc(n_tables * sizeof *ofproto->tables); | |
575 | OFPROTO_FOR_EACH_TABLE (table, ofproto) { | |
576 | oftable_init(table); | |
577 | } | |
578 | } | |
579 | ||
91858960 BP |
580 | /* To be optionally called (only) by an ofproto implementation in its |
581 | * constructor function. See the large comment on 'construct' in struct | |
582 | * ofproto_class for details. | |
583 | * | |
584 | * Sets the maximum number of ports to 'max_ports'. The ofproto generic layer | |
585 | * will then ensure that actions passed into the ofproto implementation will | |
586 | * not refer to OpenFlow ports numbered 'max_ports' or higher. If this | |
587 | * function is not called, there will be no such restriction. | |
588 | * | |
589 | * Reserved ports numbered OFPP_MAX and higher are special and not subject to | |
590 | * the 'max_ports' restriction. */ | |
591 | void | |
430dbb14 | 592 | ofproto_init_max_ports(struct ofproto *ofproto, uint16_t max_ports) |
91858960 | 593 | { |
430dbb14 | 594 | ovs_assert(max_ports <= ofp_to_u16(OFPP_MAX)); |
91858960 BP |
595 | ofproto->max_ports = max_ports; |
596 | } | |
597 | ||
e825ace2 BP |
598 | uint64_t |
599 | ofproto_get_datapath_id(const struct ofproto *ofproto) | |
600 | { | |
601 | return ofproto->datapath_id; | |
602 | } | |
603 | ||
064af421 BP |
604 | void |
605 | ofproto_set_datapath_id(struct ofproto *p, uint64_t datapath_id) | |
606 | { | |
607 | uint64_t old_dpid = p->datapath_id; | |
fa60c019 | 608 | p->datapath_id = datapath_id ? datapath_id : pick_datapath_id(p); |
064af421 | 609 | if (p->datapath_id != old_dpid) { |
76ce9432 BP |
610 | /* Force all active connections to reconnect, since there is no way to |
611 | * notify a controller that the datapath ID has changed. */ | |
fa05809b | 612 | ofproto_reconnect_controllers(p); |
064af421 BP |
613 | } |
614 | } | |
615 | ||
76ce9432 BP |
616 | void |
617 | ofproto_set_controllers(struct ofproto *p, | |
618 | const struct ofproto_controller *controllers, | |
1d9ffc17 | 619 | size_t n_controllers, uint32_t allowed_versions) |
76ce9432 | 620 | { |
1d9ffc17 SH |
621 | connmgr_set_controllers(p->connmgr, controllers, n_controllers, |
622 | allowed_versions); | |
064af421 BP |
623 | } |
624 | ||
31681a5d JP |
625 | void |
626 | ofproto_set_fail_mode(struct ofproto *p, enum ofproto_fail_mode fail_mode) | |
627 | { | |
19a87e36 | 628 | connmgr_set_fail_mode(p->connmgr, fail_mode); |
31681a5d JP |
629 | } |
630 | ||
fa05809b BP |
631 | /* Drops the connections between 'ofproto' and all of its controllers, forcing |
632 | * them to reconnect. */ | |
633 | void | |
634 | ofproto_reconnect_controllers(struct ofproto *ofproto) | |
635 | { | |
19a87e36 | 636 | connmgr_reconnect(ofproto->connmgr); |
917e50e1 BP |
637 | } |
638 | ||
639 | /* Sets the 'n' TCP port addresses in 'extras' as ones to which 'ofproto''s | |
640 | * in-band control should guarantee access, in the same way that in-band | |
641 | * control guarantees access to OpenFlow controllers. */ | |
642 | void | |
643 | ofproto_set_extra_in_band_remotes(struct ofproto *ofproto, | |
644 | const struct sockaddr_in *extras, size_t n) | |
645 | { | |
19a87e36 | 646 | connmgr_set_extra_in_band_remotes(ofproto->connmgr, extras, n); |
917e50e1 BP |
647 | } |
648 | ||
b1da6250 BP |
649 | /* Sets the OpenFlow queue used by flows set up by in-band control on |
650 | * 'ofproto' to 'queue_id'. If 'queue_id' is negative, then in-band control | |
651 | * flows will use the default queue. */ | |
652 | void | |
653 | ofproto_set_in_band_queue(struct ofproto *ofproto, int queue_id) | |
654 | { | |
19a87e36 | 655 | connmgr_set_in_band_queue(ofproto->connmgr, queue_id); |
b1da6250 BP |
656 | } |
657 | ||
084f5290 SH |
658 | /* Sets the number of flows at which eviction from the kernel flow table |
659 | * will occur. */ | |
660 | void | |
380f49c4 | 661 | ofproto_set_flow_eviction_threshold(unsigned threshold) |
084f5290 | 662 | { |
380f49c4 EJ |
663 | flow_eviction_threshold = MAX(OFPROTO_FLOW_EVICTION_THRESHOLD_MIN, |
664 | threshold); | |
084f5290 SH |
665 | } |
666 | ||
7155fa52 JS |
667 | /* Sets the path for handling flow misses. */ |
668 | void | |
669 | ofproto_set_flow_miss_model(unsigned model) | |
670 | { | |
671 | flow_miss_model = model; | |
672 | } | |
673 | ||
b53055f4 | 674 | /* If forward_bpdu is true, the NORMAL action will forward frames with |
8402c74b SS |
675 | * reserved (e.g. STP) destination Ethernet addresses. if forward_bpdu is false, |
676 | * the NORMAL action will drop these frames. */ | |
677 | void | |
678 | ofproto_set_forward_bpdu(struct ofproto *ofproto, bool forward_bpdu) | |
679 | { | |
680 | bool old_val = ofproto->forward_bpdu; | |
681 | ofproto->forward_bpdu = forward_bpdu; | |
682 | if (old_val != ofproto->forward_bpdu) { | |
683 | if (ofproto->ofproto_class->forward_bpdu_changed) { | |
684 | ofproto->ofproto_class->forward_bpdu_changed(ofproto); | |
685 | } | |
b53055f4 | 686 | } |
8402c74b SS |
687 | } |
688 | ||
e764773c | 689 | /* Sets the MAC aging timeout for the OFPP_NORMAL action on 'ofproto' to |
c4069512 BP |
690 | * 'idle_time', in seconds, and the maximum number of MAC table entries to |
691 | * 'max_entries'. */ | |
e764773c | 692 | void |
c4069512 BP |
693 | ofproto_set_mac_table_config(struct ofproto *ofproto, unsigned idle_time, |
694 | size_t max_entries) | |
e764773c | 695 | { |
c4069512 BP |
696 | if (ofproto->ofproto_class->set_mac_table_config) { |
697 | ofproto->ofproto_class->set_mac_table_config(ofproto, idle_time, | |
698 | max_entries); | |
e764773c BP |
699 | } |
700 | } | |
701 | ||
448a4b2f | 702 | /* Sets number of upcall handler threads. The default is |
cc86b1f8 | 703 | * (number of online cores - 2). */ |
448a4b2f AW |
704 | void |
705 | ofproto_set_n_handler_threads(unsigned limit) | |
706 | { | |
707 | if (limit) { | |
708 | n_handler_threads = limit; | |
709 | } else { | |
cc86b1f8 EJ |
710 | int n_proc = sysconf(_SC_NPROCESSORS_ONLN); |
711 | n_handler_threads = n_proc > 2 ? n_proc - 2 : 1; | |
448a4b2f AW |
712 | } |
713 | } | |
714 | ||
8b6ff729 BP |
715 | void |
716 | ofproto_set_dp_desc(struct ofproto *p, const char *dp_desc) | |
717 | { | |
718 | free(p->dp_desc); | |
719 | p->dp_desc = dp_desc ? xstrdup(dp_desc) : NULL; | |
720 | } | |
721 | ||
064af421 | 722 | int |
81e2083f | 723 | ofproto_set_snoops(struct ofproto *ofproto, const struct sset *snoops) |
064af421 | 724 | { |
19a87e36 | 725 | return connmgr_set_snoops(ofproto->connmgr, snoops); |
064af421 BP |
726 | } |
727 | ||
728 | int | |
0193b2af JG |
729 | ofproto_set_netflow(struct ofproto *ofproto, |
730 | const struct netflow_options *nf_options) | |
064af421 | 731 | { |
abe529af BP |
732 | if (nf_options && sset_is_empty(&nf_options->collectors)) { |
733 | nf_options = NULL; | |
734 | } | |
735 | ||
736 | if (ofproto->ofproto_class->set_netflow) { | |
737 | return ofproto->ofproto_class->set_netflow(ofproto, nf_options); | |
064af421 | 738 | } else { |
abe529af | 739 | return nf_options ? EOPNOTSUPP : 0; |
064af421 BP |
740 | } |
741 | } | |
742 | ||
abe529af | 743 | int |
72b06300 BP |
744 | ofproto_set_sflow(struct ofproto *ofproto, |
745 | const struct ofproto_sflow_options *oso) | |
746 | { | |
abe529af BP |
747 | if (oso && sset_is_empty(&oso->targets)) { |
748 | oso = NULL; | |
749 | } | |
72b06300 | 750 | |
abe529af BP |
751 | if (ofproto->ofproto_class->set_sflow) { |
752 | return ofproto->ofproto_class->set_sflow(ofproto, oso); | |
72b06300 | 753 | } else { |
abe529af | 754 | return oso ? EOPNOTSUPP : 0; |
72b06300 BP |
755 | } |
756 | } | |
29089a54 RL |
757 | |
758 | int | |
759 | ofproto_set_ipfix(struct ofproto *ofproto, | |
760 | const struct ofproto_ipfix_bridge_exporter_options *bo, | |
761 | const struct ofproto_ipfix_flow_exporter_options *fo, | |
762 | size_t n_fo) | |
763 | { | |
764 | if (ofproto->ofproto_class->set_ipfix) { | |
765 | return ofproto->ofproto_class->set_ipfix(ofproto, bo, fo, n_fo); | |
766 | } else { | |
767 | return (bo || fo) ? EOPNOTSUPP : 0; | |
768 | } | |
769 | } | |
40358701 GS |
770 | |
771 | void | |
772 | ofproto_set_flow_restore_wait(bool flow_restore_wait_db) | |
773 | { | |
774 | flow_restore_wait = flow_restore_wait_db; | |
775 | } | |
776 | ||
777 | bool | |
778 | ofproto_get_flow_restore_wait(void) | |
779 | { | |
780 | return flow_restore_wait; | |
781 | } | |
782 | ||
e7934396 | 783 | \f |
21f7563c JP |
784 | /* Spanning Tree Protocol (STP) configuration. */ |
785 | ||
786 | /* Configures STP on 'ofproto' using the settings defined in 's'. If | |
787 | * 's' is NULL, disables STP. | |
788 | * | |
789 | * Returns 0 if successful, otherwise a positive errno value. */ | |
790 | int | |
791 | ofproto_set_stp(struct ofproto *ofproto, | |
792 | const struct ofproto_stp_settings *s) | |
793 | { | |
794 | return (ofproto->ofproto_class->set_stp | |
795 | ? ofproto->ofproto_class->set_stp(ofproto, s) | |
796 | : EOPNOTSUPP); | |
797 | } | |
798 | ||
799 | /* Retrieves STP status of 'ofproto' and stores it in 's'. If the | |
800 | * 'enabled' member of 's' is false, then the other members are not | |
801 | * meaningful. | |
802 | * | |
803 | * Returns 0 if successful, otherwise a positive errno value. */ | |
804 | int | |
805 | ofproto_get_stp_status(struct ofproto *ofproto, | |
806 | struct ofproto_stp_status *s) | |
807 | { | |
808 | return (ofproto->ofproto_class->get_stp_status | |
809 | ? ofproto->ofproto_class->get_stp_status(ofproto, s) | |
810 | : EOPNOTSUPP); | |
811 | } | |
812 | ||
813 | /* Configures STP on 'ofp_port' of 'ofproto' using the settings defined | |
814 | * in 's'. The caller is responsible for assigning STP port numbers | |
815 | * (using the 'port_num' member in the range of 1 through 255, inclusive) | |
816 | * and ensuring there are no duplicates. If the 's' is NULL, then STP | |
817 | * is disabled on the port. | |
818 | * | |
819 | * Returns 0 if successful, otherwise a positive errno value.*/ | |
820 | int | |
4e022ec0 | 821 | ofproto_port_set_stp(struct ofproto *ofproto, ofp_port_t ofp_port, |
21f7563c JP |
822 | const struct ofproto_port_stp_settings *s) |
823 | { | |
824 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); | |
825 | if (!ofport) { | |
826 | VLOG_WARN("%s: cannot configure STP on nonexistent port %"PRIu16, | |
827 | ofproto->name, ofp_port); | |
828 | return ENODEV; | |
829 | } | |
830 | ||
831 | return (ofproto->ofproto_class->set_stp_port | |
832 | ? ofproto->ofproto_class->set_stp_port(ofport, s) | |
833 | : EOPNOTSUPP); | |
834 | } | |
835 | ||
836 | /* Retrieves STP port status of 'ofp_port' on 'ofproto' and stores it in | |
837 | * 's'. If the 'enabled' member in 's' is false, then the other members | |
838 | * are not meaningful. | |
839 | * | |
840 | * Returns 0 if successful, otherwise a positive errno value.*/ | |
841 | int | |
4e022ec0 | 842 | ofproto_port_get_stp_status(struct ofproto *ofproto, ofp_port_t ofp_port, |
21f7563c JP |
843 | struct ofproto_port_stp_status *s) |
844 | { | |
845 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); | |
846 | if (!ofport) { | |
b0a5c43b JP |
847 | VLOG_WARN_RL(&rl, "%s: cannot get STP status on nonexistent " |
848 | "port %"PRIu16, ofproto->name, ofp_port); | |
21f7563c JP |
849 | return ENODEV; |
850 | } | |
851 | ||
852 | return (ofproto->ofproto_class->get_stp_port_status | |
853 | ? ofproto->ofproto_class->get_stp_port_status(ofport, s) | |
854 | : EOPNOTSUPP); | |
855 | } | |
856 | \f | |
8b36f51e EJ |
857 | /* Queue DSCP configuration. */ |
858 | ||
859 | /* Registers meta-data associated with the 'n_qdscp' Qualities of Service | |
860 | * 'queues' attached to 'ofport'. This data is not intended to be sufficient | |
861 | * to implement QoS. Instead, it is used to implement features which require | |
862 | * knowledge of what queues exist on a port, and some basic information about | |
863 | * them. | |
864 | * | |
865 | * Returns 0 if successful, otherwise a positive errno value. */ | |
866 | int | |
4e022ec0 | 867 | ofproto_port_set_queues(struct ofproto *ofproto, ofp_port_t ofp_port, |
8b36f51e EJ |
868 | const struct ofproto_port_queue *queues, |
869 | size_t n_queues) | |
870 | { | |
871 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); | |
872 | ||
873 | if (!ofport) { | |
874 | VLOG_WARN("%s: cannot set queues on nonexistent port %"PRIu16, | |
875 | ofproto->name, ofp_port); | |
876 | return ENODEV; | |
877 | } | |
878 | ||
879 | return (ofproto->ofproto_class->set_queues | |
880 | ? ofproto->ofproto_class->set_queues(ofport, queues, n_queues) | |
881 | : EOPNOTSUPP); | |
882 | } | |
883 | \f | |
e7934396 BP |
884 | /* Connectivity Fault Management configuration. */ |
885 | ||
892815f5 | 886 | /* Clears the CFM configuration from 'ofp_port' on 'ofproto'. */ |
e7934396 | 887 | void |
4e022ec0 | 888 | ofproto_port_clear_cfm(struct ofproto *ofproto, ofp_port_t ofp_port) |
e7934396 | 889 | { |
abe529af BP |
890 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); |
891 | if (ofport && ofproto->ofproto_class->set_cfm) { | |
a5610457 | 892 | ofproto->ofproto_class->set_cfm(ofport, NULL); |
e7934396 BP |
893 | } |
894 | } | |
72b06300 | 895 | |
892815f5 | 896 | /* Configures connectivity fault management on 'ofp_port' in 'ofproto'. Takes |
93b8df38 EJ |
897 | * basic configuration from the configuration members in 'cfm', and the remote |
898 | * maintenance point ID from remote_mpid. Ignores the statistics members of | |
899 | * 'cfm'. | |
e7934396 | 900 | * |
892815f5 | 901 | * This function has no effect if 'ofproto' does not have a port 'ofp_port'. */ |
e7934396 | 902 | void |
4e022ec0 | 903 | ofproto_port_set_cfm(struct ofproto *ofproto, ofp_port_t ofp_port, |
a5610457 | 904 | const struct cfm_settings *s) |
e7934396 BP |
905 | { |
906 | struct ofport *ofport; | |
abe529af | 907 | int error; |
e7934396 | 908 | |
abe529af | 909 | ofport = ofproto_get_port(ofproto, ofp_port); |
e7934396 | 910 | if (!ofport) { |
892815f5 BP |
911 | VLOG_WARN("%s: cannot configure CFM on nonexistent port %"PRIu16, |
912 | ofproto->name, ofp_port); | |
e7934396 BP |
913 | return; |
914 | } | |
915 | ||
93b8df38 EJ |
916 | /* XXX: For configuration simplicity, we only support one remote_mpid |
917 | * outside of the CFM module. It's not clear if this is the correct long | |
918 | * term solution or not. */ | |
abe529af | 919 | error = (ofproto->ofproto_class->set_cfm |
a5610457 | 920 | ? ofproto->ofproto_class->set_cfm(ofport, s) |
abe529af BP |
921 | : EOPNOTSUPP); |
922 | if (error) { | |
923 | VLOG_WARN("%s: CFM configuration on port %"PRIu16" (%s) failed (%s)", | |
924 | ofproto->name, ofp_port, netdev_get_name(ofport->netdev), | |
10a89ef0 | 925 | ovs_strerror(error)); |
e7934396 | 926 | } |
e7934396 | 927 | } |
e7934396 | 928 | |
ccc09689 EJ |
929 | /* Configures BFD on 'ofp_port' in 'ofproto'. This function has no effect if |
930 | * 'ofproto' does not have a port 'ofp_port'. */ | |
931 | void | |
4e022ec0 | 932 | ofproto_port_set_bfd(struct ofproto *ofproto, ofp_port_t ofp_port, |
ccc09689 EJ |
933 | const struct smap *cfg) |
934 | { | |
935 | struct ofport *ofport; | |
936 | int error; | |
937 | ||
938 | ofport = ofproto_get_port(ofproto, ofp_port); | |
939 | if (!ofport) { | |
940 | VLOG_WARN("%s: cannot configure bfd on nonexistent port %"PRIu16, | |
941 | ofproto->name, ofp_port); | |
e8999bdc | 942 | return; |
ccc09689 EJ |
943 | } |
944 | ||
945 | error = (ofproto->ofproto_class->set_bfd | |
946 | ? ofproto->ofproto_class->set_bfd(ofport, cfg) | |
947 | : EOPNOTSUPP); | |
948 | if (error) { | |
949 | VLOG_WARN("%s: bfd configuration on port %"PRIu16" (%s) failed (%s)", | |
950 | ofproto->name, ofp_port, netdev_get_name(ofport->netdev), | |
10a89ef0 | 951 | ovs_strerror(error)); |
ccc09689 EJ |
952 | } |
953 | } | |
954 | ||
955 | /* Populates 'status' with key value pairs indicating the status of the BFD | |
956 | * session on 'ofp_port'. This information is intended to be populated in the | |
957 | * OVS database. Has no effect if 'ofp_port' is not na OpenFlow port in | |
958 | * 'ofproto'. */ | |
959 | int | |
4e022ec0 | 960 | ofproto_port_get_bfd_status(struct ofproto *ofproto, ofp_port_t ofp_port, |
ccc09689 EJ |
961 | struct smap *status) |
962 | { | |
963 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); | |
964 | return (ofport && ofproto->ofproto_class->get_bfd_status | |
965 | ? ofproto->ofproto_class->get_bfd_status(ofport, status) | |
966 | : EOPNOTSUPP); | |
967 | } | |
968 | ||
fa066f01 BP |
969 | /* Checks the status of LACP negotiation for 'ofp_port' within ofproto. |
970 | * Returns 1 if LACP partner information for 'ofp_port' is up-to-date, | |
971 | * 0 if LACP partner information is not current (generally indicating a | |
972 | * connectivity problem), or -1 if LACP is not enabled on 'ofp_port'. */ | |
973 | int | |
4e022ec0 | 974 | ofproto_port_is_lacp_current(struct ofproto *ofproto, ofp_port_t ofp_port) |
fa066f01 | 975 | { |
abe529af BP |
976 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); |
977 | return (ofport && ofproto->ofproto_class->port_is_lacp_current | |
978 | ? ofproto->ofproto_class->port_is_lacp_current(ofport) | |
fa066f01 | 979 | : -1); |
e7934396 | 980 | } |
e7934396 | 981 | \f |
fa066f01 | 982 | /* Bundles. */ |
e7934396 | 983 | |
abe529af BP |
984 | /* Registers a "bundle" associated with client data pointer 'aux' in 'ofproto'. |
985 | * A bundle is the same concept as a Port in OVSDB, that is, it consists of one | |
986 | * or more "slave" devices (Interfaces, in OVSDB) along with a VLAN | |
987 | * configuration plus, if there is more than one slave, a bonding | |
988 | * configuration. | |
989 | * | |
990 | * If 'aux' is already registered then this function updates its configuration | |
991 | * to 's'. Otherwise, this function registers a new bundle. | |
992 | * | |
993 | * Bundles only affect the NXAST_AUTOPATH action and output to the OFPP_NORMAL | |
994 | * port. */ | |
995 | int | |
996 | ofproto_bundle_register(struct ofproto *ofproto, void *aux, | |
997 | const struct ofproto_bundle_settings *s) | |
fa066f01 | 998 | { |
abe529af BP |
999 | return (ofproto->ofproto_class->bundle_set |
1000 | ? ofproto->ofproto_class->bundle_set(ofproto, aux, s) | |
1001 | : EOPNOTSUPP); | |
fa066f01 BP |
1002 | } |
1003 | ||
abe529af BP |
1004 | /* Unregisters the bundle registered on 'ofproto' with auxiliary data 'aux'. |
1005 | * If no such bundle has been registered, this has no effect. */ | |
1006 | int | |
1007 | ofproto_bundle_unregister(struct ofproto *ofproto, void *aux) | |
e7934396 | 1008 | { |
abe529af | 1009 | return ofproto_bundle_register(ofproto, aux, NULL); |
e7934396 | 1010 | } |
fa066f01 | 1011 | |
e7934396 | 1012 | \f |
abe529af BP |
1013 | /* Registers a mirror associated with client data pointer 'aux' in 'ofproto'. |
1014 | * If 'aux' is already registered then this function updates its configuration | |
c06bba01 | 1015 | * to 's'. Otherwise, this function registers a new mirror. */ |
abe529af BP |
1016 | int |
1017 | ofproto_mirror_register(struct ofproto *ofproto, void *aux, | |
1018 | const struct ofproto_mirror_settings *s) | |
064af421 | 1019 | { |
abe529af BP |
1020 | return (ofproto->ofproto_class->mirror_set |
1021 | ? ofproto->ofproto_class->mirror_set(ofproto, aux, s) | |
1022 | : EOPNOTSUPP); | |
064af421 BP |
1023 | } |
1024 | ||
abe529af BP |
1025 | /* Unregisters the mirror registered on 'ofproto' with auxiliary data 'aux'. |
1026 | * If no mirror has been registered, this has no effect. */ | |
1027 | int | |
1028 | ofproto_mirror_unregister(struct ofproto *ofproto, void *aux) | |
064af421 | 1029 | { |
abe529af | 1030 | return ofproto_mirror_register(ofproto, aux, NULL); |
064af421 BP |
1031 | } |
1032 | ||
9d24de3b JP |
1033 | /* Retrieves statistics from mirror associated with client data pointer |
1034 | * 'aux' in 'ofproto'. Stores packet and byte counts in 'packets' and | |
1035 | * 'bytes', respectively. If a particular counters is not supported, | |
1036 | * the appropriate argument is set to UINT64_MAX. */ | |
1037 | int | |
1038 | ofproto_mirror_get_stats(struct ofproto *ofproto, void *aux, | |
1039 | uint64_t *packets, uint64_t *bytes) | |
1040 | { | |
1041 | if (!ofproto->ofproto_class->mirror_get_stats) { | |
1042 | *packets = *bytes = UINT64_MAX; | |
1043 | return EOPNOTSUPP; | |
1044 | } | |
1045 | ||
1046 | return ofproto->ofproto_class->mirror_get_stats(ofproto, aux, | |
1047 | packets, bytes); | |
1048 | } | |
1049 | ||
abe529af BP |
1050 | /* Configures the VLANs whose bits are set to 1 in 'flood_vlans' as VLANs on |
1051 | * which all packets are flooded, instead of using MAC learning. If | |
1052 | * 'flood_vlans' is NULL, then MAC learning applies to all VLANs. | |
1053 | * | |
1054 | * Flood VLANs affect only the treatment of packets output to the OFPP_NORMAL | |
1055 | * port. */ | |
1056 | int | |
1057 | ofproto_set_flood_vlans(struct ofproto *ofproto, unsigned long *flood_vlans) | |
abdfe474 | 1058 | { |
abe529af BP |
1059 | return (ofproto->ofproto_class->set_flood_vlans |
1060 | ? ofproto->ofproto_class->set_flood_vlans(ofproto, flood_vlans) | |
1061 | : EOPNOTSUPP); | |
abdfe474 JP |
1062 | } |
1063 | ||
abe529af BP |
1064 | /* Returns true if 'aux' is a registered bundle that is currently in use as the |
1065 | * output for a mirror. */ | |
1066 | bool | |
b4affc74 | 1067 | ofproto_is_mirror_output_bundle(const struct ofproto *ofproto, void *aux) |
abe529af BP |
1068 | { |
1069 | return (ofproto->ofproto_class->is_mirror_output_bundle | |
1070 | ? ofproto->ofproto_class->is_mirror_output_bundle(ofproto, aux) | |
1071 | : false); | |
1072 | } | |
1073 | \f | |
254750ce BP |
1074 | /* Configuration of OpenFlow tables. */ |
1075 | ||
1076 | /* Returns the number of OpenFlow tables in 'ofproto'. */ | |
1077 | int | |
1078 | ofproto_get_n_tables(const struct ofproto *ofproto) | |
1079 | { | |
1080 | return ofproto->n_tables; | |
1081 | } | |
1082 | ||
1083 | /* Configures the OpenFlow table in 'ofproto' with id 'table_id' with the | |
1084 | * settings from 's'. 'table_id' must be in the range 0 through the number of | |
1085 | * OpenFlow tables in 'ofproto' minus 1, inclusive. | |
1086 | * | |
1087 | * For read-only tables, only the name may be configured. */ | |
1088 | void | |
1089 | ofproto_configure_table(struct ofproto *ofproto, int table_id, | |
1090 | const struct ofproto_table_settings *s) | |
1091 | { | |
1092 | struct oftable *table; | |
1093 | ||
cb22974d | 1094 | ovs_assert(table_id >= 0 && table_id < ofproto->n_tables); |
254750ce BP |
1095 | table = &ofproto->tables[table_id]; |
1096 | ||
1097 | oftable_set_name(table, s->name); | |
1098 | ||
1099 | if (table->flags & OFTABLE_READONLY) { | |
1100 | return; | |
1101 | } | |
1102 | ||
1103 | if (s->groups) { | |
1104 | oftable_enable_eviction(table, s->groups, s->n_groups); | |
1105 | } else { | |
1106 | oftable_disable_eviction(table); | |
1107 | } | |
1108 | ||
1109 | table->max_flows = s->max_flows; | |
0b4f2078 | 1110 | ovs_rwlock_rdlock(&table->cls.rwlock); |
254750ce BP |
1111 | if (classifier_count(&table->cls) > table->max_flows |
1112 | && table->eviction_fields) { | |
1113 | /* 'table' contains more flows than allowed. We might not be able to | |
1114 | * evict them right away because of the asynchronous nature of flow | |
1115 | * table changes. Schedule eviction for later. */ | |
1116 | switch (ofproto->state) { | |
1117 | case S_OPENFLOW: | |
1118 | ofproto->state = S_EVICT; | |
1119 | break; | |
1120 | case S_EVICT: | |
1121 | case S_FLUSH: | |
1122 | /* We're already deleting flows, nothing more to do. */ | |
1123 | break; | |
1124 | } | |
1125 | } | |
0b4f2078 | 1126 | ovs_rwlock_unlock(&table->cls.rwlock); |
254750ce BP |
1127 | } |
1128 | \f | |
81e2083f BP |
1129 | bool |
1130 | ofproto_has_snoops(const struct ofproto *ofproto) | |
1131 | { | |
1132 | return connmgr_has_snoops(ofproto->connmgr); | |
1133 | } | |
1134 | ||
064af421 | 1135 | void |
81e2083f | 1136 | ofproto_get_snoops(const struct ofproto *ofproto, struct sset *snoops) |
064af421 | 1137 | { |
19a87e36 | 1138 | connmgr_get_snoops(ofproto->connmgr, snoops); |
064af421 BP |
1139 | } |
1140 | ||
15aaf599 | 1141 | static void |
8b81d1ef | 1142 | ofproto_rule_delete__(struct ofproto *ofproto, struct rule *rule) |
15aaf599 BP |
1143 | OVS_REQUIRES(ofproto_mutex) |
1144 | { | |
1145 | struct ofopgroup *group; | |
1146 | ||
1147 | ovs_assert(!rule->pending); | |
15aaf599 BP |
1148 | |
1149 | group = ofopgroup_create_unattached(ofproto); | |
1150 | delete_flow__(rule, group, OFPRR_DELETE); | |
1151 | ofopgroup_submit(group); | |
1152 | } | |
1153 | ||
8037acb4 BP |
1154 | /* Deletes 'rule' from 'cls' within 'ofproto'. |
1155 | * | |
b74cb5d8 BP |
1156 | * Within an ofproto implementation, this function allows an ofproto |
1157 | * implementation to destroy any rules that remain when its ->destruct() | |
1158 | * function is called. This function is not suitable for use elsewhere in an | |
1159 | * ofproto implementation. | |
1160 | * | |
b74cb5d8 | 1161 | * This function implements steps 4.4 and 4.5 in the section titled "Rule Life |
8b81d1ef | 1162 | * Cycle" in ofproto-provider.h. */ |
b74cb5d8 | 1163 | void |
8b81d1ef | 1164 | ofproto_rule_delete(struct ofproto *ofproto, struct rule *rule) |
15aaf599 | 1165 | OVS_EXCLUDED(ofproto_mutex) |
7ee20df1 | 1166 | { |
7ee20df1 | 1167 | struct ofopgroup *group; |
8037acb4 | 1168 | |
15aaf599 | 1169 | ovs_mutex_lock(&ofproto_mutex); |
8037acb4 | 1170 | ovs_assert(!rule->pending); |
8037acb4 BP |
1171 | |
1172 | group = ofopgroup_create_unattached(ofproto); | |
1173 | ofoperation_create(group, rule, OFOPERATION_DELETE, OFPRR_DELETE); | |
8b81d1ef | 1174 | oftable_remove_rule__(ofproto, rule); |
8037acb4 BP |
1175 | ofproto->ofproto_class->rule_delete(rule); |
1176 | ofopgroup_submit(group); | |
15aaf599 BP |
1177 | |
1178 | ovs_mutex_unlock(&ofproto_mutex); | |
8037acb4 BP |
1179 | } |
1180 | ||
1181 | static void | |
1182 | ofproto_flush__(struct ofproto *ofproto) | |
15aaf599 | 1183 | OVS_EXCLUDED(ofproto_mutex) |
8037acb4 | 1184 | { |
d0918789 | 1185 | struct oftable *table; |
7ee20df1 BP |
1186 | |
1187 | if (ofproto->ofproto_class->flush) { | |
1188 | ofproto->ofproto_class->flush(ofproto); | |
1189 | } | |
1190 | ||
15aaf599 | 1191 | ovs_mutex_lock(&ofproto_mutex); |
b772ded8 | 1192 | OFPROTO_FOR_EACH_TABLE (table, ofproto) { |
7ee20df1 BP |
1193 | struct rule *rule, *next_rule; |
1194 | struct cls_cursor cursor; | |
1195 | ||
5c67e4af BP |
1196 | if (table->flags & OFTABLE_HIDDEN) { |
1197 | continue; | |
1198 | } | |
1199 | ||
15aaf599 | 1200 | ovs_rwlock_rdlock(&table->cls.rwlock); |
d0918789 | 1201 | cls_cursor_init(&cursor, &table->cls, NULL); |
15aaf599 | 1202 | ovs_rwlock_unlock(&table->cls.rwlock); |
7ee20df1 BP |
1203 | CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, cr, &cursor) { |
1204 | if (!rule->pending) { | |
8b81d1ef | 1205 | ofproto_rule_delete__(ofproto, rule); |
7ee20df1 BP |
1206 | } |
1207 | } | |
1208 | } | |
15aaf599 | 1209 | ovs_mutex_unlock(&ofproto_mutex); |
7ee20df1 BP |
1210 | } |
1211 | ||
7395c052 NZ |
1212 | static void delete_group(struct ofproto *ofproto, uint32_t group_id); |
1213 | ||
abe529af BP |
1214 | static void |
1215 | ofproto_destroy__(struct ofproto *ofproto) | |
15aaf599 | 1216 | OVS_EXCLUDED(ofproto_mutex) |
abe529af | 1217 | { |
d0918789 | 1218 | struct oftable *table; |
6c1491fb | 1219 | |
cb22974d | 1220 | ovs_assert(list_is_empty(&ofproto->pending)); |
7ee20df1 | 1221 | |
35412852 BP |
1222 | destroy_rule_executes(ofproto); |
1223 | guarded_list_destroy(&ofproto->rule_executes); | |
1224 | ||
7395c052 NZ |
1225 | delete_group(ofproto, OFPG_ALL); |
1226 | ovs_rwlock_destroy(&ofproto->groups_rwlock); | |
1227 | hmap_destroy(&ofproto->groups); | |
1228 | ||
abe529af | 1229 | connmgr_destroy(ofproto->connmgr); |
fa066f01 | 1230 | |
abe529af BP |
1231 | hmap_remove(&all_ofprotos, &ofproto->hmap_node); |
1232 | free(ofproto->name); | |
955a7127 | 1233 | free(ofproto->type); |
abe529af BP |
1234 | free(ofproto->mfr_desc); |
1235 | free(ofproto->hw_desc); | |
1236 | free(ofproto->sw_desc); | |
1237 | free(ofproto->serial_desc); | |
1238 | free(ofproto->dp_desc); | |
abe529af | 1239 | hmap_destroy(&ofproto->ports); |
fdcea803 | 1240 | hmap_destroy(&ofproto->ofport_usage); |
abe529af | 1241 | shash_destroy(&ofproto->port_by_name); |
e1b1d06a | 1242 | simap_destroy(&ofproto->ofp_requests); |
6c1491fb | 1243 | |
b772ded8 | 1244 | OFPROTO_FOR_EACH_TABLE (table, ofproto) { |
d0918789 | 1245 | oftable_destroy(table); |
6c1491fb BP |
1246 | } |
1247 | free(ofproto->tables); | |
fa066f01 | 1248 | |
7ee20df1 BP |
1249 | hmap_destroy(&ofproto->deletions); |
1250 | ||
52a90c29 BP |
1251 | free(ofproto->vlan_bitmap); |
1252 | ||
abe529af BP |
1253 | ofproto->ofproto_class->dealloc(ofproto); |
1254 | } | |
fa066f01 | 1255 | |
064af421 BP |
1256 | void |
1257 | ofproto_destroy(struct ofproto *p) | |
15aaf599 | 1258 | OVS_EXCLUDED(ofproto_mutex) |
064af421 | 1259 | { |
ca0f572c | 1260 | struct ofport *ofport, *next_ofport; |
fdcea803 | 1261 | struct ofport_usage *usage, *next_usage; |
064af421 BP |
1262 | |
1263 | if (!p) { | |
1264 | return; | |
1265 | } | |
1266 | ||
717de9ff JR |
1267 | if (p->meters) { |
1268 | meter_delete(p, 1, p->meter_features.max_meters); | |
1269 | p->meter_features.max_meters = 0; | |
1270 | free(p->meters); | |
1271 | p->meters = NULL; | |
1272 | } | |
1273 | ||
7ee20df1 | 1274 | ofproto_flush__(p); |
4e8e4213 | 1275 | HMAP_FOR_EACH_SAFE (ofport, next_ofport, hmap_node, &p->ports) { |
abe529af | 1276 | ofport_destroy(ofport); |
064af421 | 1277 | } |
064af421 | 1278 | |
fdcea803 GS |
1279 | HMAP_FOR_EACH_SAFE (usage, next_usage, hmap_node, &p->ofport_usage) { |
1280 | hmap_remove(&p->ofport_usage, &usage->hmap_node); | |
1281 | free(usage); | |
1282 | } | |
1283 | ||
abe529af BP |
1284 | p->ofproto_class->destruct(p); |
1285 | ofproto_destroy__(p); | |
064af421 BP |
1286 | } |
1287 | ||
abe529af BP |
1288 | /* Destroys the datapath with the respective 'name' and 'type'. With the Linux |
1289 | * kernel datapath, for example, this destroys the datapath in the kernel, and | |
1290 | * with the netdev-based datapath, it tears down the data structures that | |
1291 | * represent the datapath. | |
1292 | * | |
1293 | * The datapath should not be currently open as an ofproto. */ | |
064af421 | 1294 | int |
abe529af | 1295 | ofproto_delete(const char *name, const char *type) |
064af421 | 1296 | { |
abe529af BP |
1297 | const struct ofproto_class *class = ofproto_class_find__(type); |
1298 | return (!class ? EAFNOSUPPORT | |
1299 | : !class->del ? EACCES | |
1300 | : class->del(type, name)); | |
064af421 BP |
1301 | } |
1302 | ||
e9e28be3 BP |
1303 | static void |
1304 | process_port_change(struct ofproto *ofproto, int error, char *devname) | |
1305 | { | |
1306 | if (error == ENOBUFS) { | |
1307 | reinit_ports(ofproto); | |
1308 | } else if (!error) { | |
1309 | update_port(ofproto, devname); | |
1310 | free(devname); | |
1311 | } | |
1312 | } | |
1313 | ||
11a574a7 JP |
1314 | int |
1315 | ofproto_type_run(const char *datapath_type) | |
1316 | { | |
1317 | const struct ofproto_class *class; | |
1318 | int error; | |
1319 | ||
1320 | datapath_type = ofproto_normalize_type(datapath_type); | |
1321 | class = ofproto_class_find__(datapath_type); | |
1322 | ||
1323 | error = class->type_run ? class->type_run(datapath_type) : 0; | |
1324 | if (error && error != EAGAIN) { | |
1325 | VLOG_ERR_RL(&rl, "%s: type_run failed (%s)", | |
10a89ef0 | 1326 | datapath_type, ovs_strerror(error)); |
11a574a7 JP |
1327 | } |
1328 | return error; | |
1329 | } | |
1330 | ||
1331 | int | |
1332 | ofproto_type_run_fast(const char *datapath_type) | |
1333 | { | |
1334 | const struct ofproto_class *class; | |
1335 | int error; | |
1336 | ||
1337 | datapath_type = ofproto_normalize_type(datapath_type); | |
1338 | class = ofproto_class_find__(datapath_type); | |
1339 | ||
1340 | error = class->type_run_fast ? class->type_run_fast(datapath_type) : 0; | |
1341 | if (error && error != EAGAIN) { | |
1342 | VLOG_ERR_RL(&rl, "%s: type_run_fast failed (%s)", | |
10a89ef0 | 1343 | datapath_type, ovs_strerror(error)); |
11a574a7 JP |
1344 | } |
1345 | return error; | |
1346 | } | |
1347 | ||
1348 | void | |
1349 | ofproto_type_wait(const char *datapath_type) | |
1350 | { | |
1351 | const struct ofproto_class *class; | |
1352 | ||
1353 | datapath_type = ofproto_normalize_type(datapath_type); | |
1354 | class = ofproto_class_find__(datapath_type); | |
1355 | ||
1356 | if (class->type_wait) { | |
1357 | class->type_wait(datapath_type); | |
1358 | } | |
1359 | } | |
1360 | ||
06b79a9a BP |
1361 | static bool |
1362 | any_pending_ops(const struct ofproto *p) | |
15aaf599 | 1363 | OVS_EXCLUDED(ofproto_mutex) |
06b79a9a | 1364 | { |
15aaf599 BP |
1365 | bool b; |
1366 | ||
1367 | ovs_mutex_lock(&ofproto_mutex); | |
1368 | b = !list_is_empty(&p->pending); | |
1369 | ovs_mutex_unlock(&ofproto_mutex); | |
1370 | ||
1371 | return b; | |
06b79a9a BP |
1372 | } |
1373 | ||
064af421 | 1374 | int |
abe529af | 1375 | ofproto_run(struct ofproto *p) |
064af421 | 1376 | { |
7436ed80 BP |
1377 | struct sset changed_netdevs; |
1378 | const char *changed_netdev; | |
031d8bff | 1379 | struct ofport *ofport; |
064af421 | 1380 | int error; |
064af421 | 1381 | |
abe529af | 1382 | error = p->ofproto_class->run(p); |
5fcc0d00 | 1383 | if (error && error != EAGAIN) { |
10a89ef0 | 1384 | VLOG_ERR_RL(&rl, "%s: run failed (%s)", p->name, ovs_strerror(error)); |
149f577a JG |
1385 | } |
1386 | ||
35412852 BP |
1387 | run_rule_executes(p); |
1388 | ||
448c2fa8 EJ |
1389 | /* Restore the eviction group heap invariant occasionally. */ |
1390 | if (p->eviction_group_timer < time_msec()) { | |
1391 | size_t i; | |
1392 | ||
1393 | p->eviction_group_timer = time_msec() + 1000; | |
1394 | ||
1395 | for (i = 0; i < p->n_tables; i++) { | |
1396 | struct oftable *table = &p->tables[i]; | |
1397 | struct eviction_group *evg; | |
1398 | struct cls_cursor cursor; | |
448c2fa8 EJ |
1399 | struct rule *rule; |
1400 | ||
1401 | if (!table->eviction_fields) { | |
1402 | continue; | |
1403 | } | |
1404 | ||
15aaf599 | 1405 | ovs_mutex_lock(&ofproto_mutex); |
448c2fa8 EJ |
1406 | HEAP_FOR_EACH (evg, size_node, &table->eviction_groups_by_size) { |
1407 | heap_rebuild(&evg->rules); | |
1408 | } | |
1409 | ||
1410 | ovs_rwlock_rdlock(&table->cls.rwlock); | |
c4ea31b1 | 1411 | cls_cursor_init(&cursor, &table->cls, NULL); |
448c2fa8 EJ |
1412 | CLS_CURSOR_FOR_EACH (rule, cr, &cursor) { |
1413 | if (!rule->eviction_group | |
1414 | && (rule->idle_timeout || rule->hard_timeout)) { | |
1415 | eviction_group_add_rule(rule); | |
1416 | } | |
1417 | } | |
1418 | ovs_rwlock_unlock(&table->cls.rwlock); | |
15aaf599 | 1419 | ovs_mutex_unlock(&ofproto_mutex); |
448c2fa8 EJ |
1420 | } |
1421 | } | |
1422 | ||
5bf0e941 | 1423 | if (p->ofproto_class->port_poll) { |
7436ed80 BP |
1424 | char *devname; |
1425 | ||
5bf0e941 BP |
1426 | while ((error = p->ofproto_class->port_poll(p, &devname)) != EAGAIN) { |
1427 | process_port_change(p, error, devname); | |
064af421 | 1428 | } |
e9e28be3 | 1429 | } |
031d8bff | 1430 | |
7436ed80 BP |
1431 | /* Update OpenFlow port status for any port whose netdev has changed. |
1432 | * | |
1433 | * Refreshing a given 'ofport' can cause an arbitrary ofport to be | |
1434 | * destroyed, so it's not safe to update ports directly from the | |
1435 | * HMAP_FOR_EACH loop, or even to use HMAP_FOR_EACH_SAFE. Instead, we | |
1436 | * need this two-phase approach. */ | |
1437 | sset_init(&changed_netdevs); | |
031d8bff EJ |
1438 | HMAP_FOR_EACH (ofport, hmap_node, &p->ports) { |
1439 | unsigned int change_seq = netdev_change_seq(ofport->netdev); | |
1440 | if (ofport->change_seq != change_seq) { | |
1441 | ofport->change_seq = change_seq; | |
7436ed80 | 1442 | sset_add(&changed_netdevs, netdev_get_name(ofport->netdev)); |
031d8bff | 1443 | } |
064af421 | 1444 | } |
7436ed80 BP |
1445 | SSET_FOR_EACH (changed_netdev, &changed_netdevs) { |
1446 | update_port(p, changed_netdev); | |
1447 | } | |
1448 | sset_destroy(&changed_netdevs); | |
064af421 | 1449 | |
7ee20df1 BP |
1450 | switch (p->state) { |
1451 | case S_OPENFLOW: | |
1452 | connmgr_run(p->connmgr, handle_openflow); | |
1453 | break; | |
1454 | ||
254750ce BP |
1455 | case S_EVICT: |
1456 | connmgr_run(p->connmgr, NULL); | |
1457 | ofproto_evict(p); | |
06b79a9a | 1458 | if (!any_pending_ops(p)) { |
254750ce BP |
1459 | p->state = S_OPENFLOW; |
1460 | } | |
1461 | break; | |
1462 | ||
7ee20df1 BP |
1463 | case S_FLUSH: |
1464 | connmgr_run(p->connmgr, NULL); | |
1465 | ofproto_flush__(p); | |
06b79a9a | 1466 | if (!any_pending_ops(p)) { |
7ee20df1 BP |
1467 | connmgr_flushed(p->connmgr); |
1468 | p->state = S_OPENFLOW; | |
1469 | } | |
1470 | break; | |
1471 | ||
1472 | default: | |
1473 | NOT_REACHED(); | |
1474 | } | |
064af421 | 1475 | |
a5b8d268 BP |
1476 | if (time_msec() >= p->next_op_report) { |
1477 | long long int ago = (time_msec() - p->first_op) / 1000; | |
1478 | long long int interval = (p->last_op - p->first_op) / 1000; | |
1479 | struct ds s; | |
1480 | ||
1481 | ds_init(&s); | |
1482 | ds_put_format(&s, "%d flow_mods ", | |
1483 | p->n_add + p->n_delete + p->n_modify); | |
1484 | if (interval == ago) { | |
1485 | ds_put_format(&s, "in the last %lld s", ago); | |
1486 | } else if (interval) { | |
1487 | ds_put_format(&s, "in the %lld s starting %lld s ago", | |
1488 | interval, ago); | |
1489 | } else { | |
1490 | ds_put_format(&s, "%lld s ago", ago); | |
1491 | } | |
1492 | ||
1493 | ds_put_cstr(&s, " ("); | |
1494 | if (p->n_add) { | |
1495 | ds_put_format(&s, "%d adds, ", p->n_add); | |
1496 | } | |
1497 | if (p->n_delete) { | |
1498 | ds_put_format(&s, "%d deletes, ", p->n_delete); | |
1499 | } | |
1500 | if (p->n_modify) { | |
1501 | ds_put_format(&s, "%d modifications, ", p->n_modify); | |
1502 | } | |
1503 | s.length -= 2; | |
1504 | ds_put_char(&s, ')'); | |
1505 | ||
fbfa2911 | 1506 | VLOG_INFO("%s: %s", p->name, ds_cstr(&s)); |
a5b8d268 BP |
1507 | ds_destroy(&s); |
1508 | ||
1509 | p->n_add = p->n_delete = p->n_modify = 0; | |
1510 | p->next_op_report = LLONG_MAX; | |
1511 | } | |
1512 | ||
5fcc0d00 BP |
1513 | return error; |
1514 | } | |
1515 | ||
1516 | /* Performs periodic activity required by 'ofproto' that needs to be done | |
1517 | * with the least possible latency. | |
1518 | * | |
1519 | * It makes sense to call this function a couple of times per poll loop, to | |
1520 | * provide a significant performance boost on some benchmarks with the | |
1521 | * ofproto-dpif implementation. */ | |
1522 | int | |
1523 | ofproto_run_fast(struct ofproto *p) | |
1524 | { | |
1525 | int error; | |
1526 | ||
1527 | error = p->ofproto_class->run_fast ? p->ofproto_class->run_fast(p) : 0; | |
1528 | if (error && error != EAGAIN) { | |
1529 | VLOG_ERR_RL(&rl, "%s: fastpath run failed (%s)", | |
10a89ef0 | 1530 | p->name, ovs_strerror(error)); |
5fcc0d00 BP |
1531 | } |
1532 | return error; | |
064af421 BP |
1533 | } |
1534 | ||
1535 | void | |
1536 | ofproto_wait(struct ofproto *p) | |
1537 | { | |
031d8bff EJ |
1538 | struct ofport *ofport; |
1539 | ||
abe529af | 1540 | p->ofproto_class->wait(p); |
5bf0e941 BP |
1541 | if (p->ofproto_class->port_poll_wait) { |
1542 | p->ofproto_class->port_poll_wait(p); | |
e7934396 | 1543 | } |
031d8bff EJ |
1544 | |
1545 | HMAP_FOR_EACH (ofport, hmap_node, &p->ports) { | |
1546 | if (ofport->change_seq != netdev_change_seq(ofport->netdev)) { | |
1547 | poll_immediate_wake(); | |
1548 | } | |
1549 | } | |
7ee20df1 BP |
1550 | |
1551 | switch (p->state) { | |
1552 | case S_OPENFLOW: | |
1553 | connmgr_wait(p->connmgr, true); | |
1554 | break; | |
1555 | ||
254750ce | 1556 | case S_EVICT: |
7ee20df1 BP |
1557 | case S_FLUSH: |
1558 | connmgr_wait(p->connmgr, false); | |
06b79a9a | 1559 | if (!any_pending_ops(p)) { |
7ee20df1 BP |
1560 | poll_immediate_wake(); |
1561 | } | |
1562 | break; | |
1563 | } | |
064af421 BP |
1564 | } |
1565 | ||
064af421 BP |
1566 | bool |
1567 | ofproto_is_alive(const struct ofproto *p) | |
1568 | { | |
19a87e36 | 1569 | return connmgr_has_controllers(p->connmgr); |
064af421 BP |
1570 | } |
1571 | ||
0d085684 BP |
1572 | /* Adds some memory usage statistics for 'ofproto' into 'usage', for use with |
1573 | * memory_report(). */ | |
1574 | void | |
1575 | ofproto_get_memory_usage(const struct ofproto *ofproto, struct simap *usage) | |
1576 | { | |
1577 | const struct oftable *table; | |
1578 | unsigned int n_rules; | |
1579 | ||
1580 | simap_increase(usage, "ports", hmap_count(&ofproto->ports)); | |
15aaf599 BP |
1581 | |
1582 | ovs_mutex_lock(&ofproto_mutex); | |
0d085684 BP |
1583 | simap_increase(usage, "ops", |
1584 | ofproto->n_pending + hmap_count(&ofproto->deletions)); | |
15aaf599 | 1585 | ovs_mutex_unlock(&ofproto_mutex); |
0d085684 BP |
1586 | |
1587 | n_rules = 0; | |
1588 | OFPROTO_FOR_EACH_TABLE (table, ofproto) { | |
0b4f2078 | 1589 | ovs_rwlock_rdlock(&table->cls.rwlock); |
0d085684 | 1590 | n_rules += classifier_count(&table->cls); |
0b4f2078 | 1591 | ovs_rwlock_unlock(&table->cls.rwlock); |
0d085684 BP |
1592 | } |
1593 | simap_increase(usage, "rules", n_rules); | |
1594 | ||
1595 | if (ofproto->ofproto_class->get_memory_usage) { | |
1596 | ofproto->ofproto_class->get_memory_usage(ofproto, usage); | |
1597 | } | |
1598 | ||
1599 | connmgr_get_memory_usage(ofproto->connmgr, usage); | |
1600 | } | |
1601 | ||
bffc0589 | 1602 | void |
2cdcb898 | 1603 | ofproto_get_ofproto_controller_info(const struct ofproto *ofproto, |
bffc0589 AE |
1604 | struct shash *info) |
1605 | { | |
19a87e36 | 1606 | connmgr_get_controller_info(ofproto->connmgr, info); |
bffc0589 AE |
1607 | } |
1608 | ||
1609 | void | |
1610 | ofproto_free_ofproto_controller_info(struct shash *info) | |
1611 | { | |
72ba2ed3 | 1612 | connmgr_free_controller_info(info); |
bffc0589 AE |
1613 | } |
1614 | ||
b5827b24 BP |
1615 | /* Makes a deep copy of 'old' into 'port'. */ |
1616 | void | |
1617 | ofproto_port_clone(struct ofproto_port *port, const struct ofproto_port *old) | |
1618 | { | |
1619 | port->name = xstrdup(old->name); | |
1620 | port->type = xstrdup(old->type); | |
1621 | port->ofp_port = old->ofp_port; | |
1622 | } | |
1623 | ||
1624 | /* Frees memory allocated to members of 'ofproto_port'. | |
3a6ccc8c | 1625 | * |
b5827b24 BP |
1626 | * Do not call this function on an ofproto_port obtained from |
1627 | * ofproto_port_dump_next(): that function retains ownership of the data in the | |
1628 | * ofproto_port. */ | |
1629 | void | |
1630 | ofproto_port_destroy(struct ofproto_port *ofproto_port) | |
1631 | { | |
1632 | free(ofproto_port->name); | |
1633 | free(ofproto_port->type); | |
1634 | } | |
1635 | ||
b5827b24 | 1636 | /* Initializes 'dump' to begin dumping the ports in an ofproto. |
3a6ccc8c | 1637 | * |
b5827b24 BP |
1638 | * This function provides no status indication. An error status for the entire |
1639 | * dump operation is provided when it is completed by calling | |
1640 | * ofproto_port_dump_done(). | |
1641 | */ | |
1642 | void | |
1643 | ofproto_port_dump_start(struct ofproto_port_dump *dump, | |
1644 | const struct ofproto *ofproto) | |
1645 | { | |
abe529af BP |
1646 | dump->ofproto = ofproto; |
1647 | dump->error = ofproto->ofproto_class->port_dump_start(ofproto, | |
1648 | &dump->state); | |
b5827b24 BP |
1649 | } |
1650 | ||
1651 | /* Attempts to retrieve another port from 'dump', which must have been created | |
1652 | * with ofproto_port_dump_start(). On success, stores a new ofproto_port into | |
1653 | * 'port' and returns true. On failure, returns false. | |
1654 | * | |
1655 | * Failure might indicate an actual error or merely that the last port has been | |
1656 | * dumped. An error status for the entire dump operation is provided when it | |
1657 | * is completed by calling ofproto_port_dump_done(). | |
1658 | * | |
1659 | * The ofproto owns the data stored in 'port'. It will remain valid until at | |
1660 | * least the next time 'dump' is passed to ofproto_port_dump_next() or | |
1661 | * ofproto_port_dump_done(). */ | |
1662 | bool | |
1663 | ofproto_port_dump_next(struct ofproto_port_dump *dump, | |
1664 | struct ofproto_port *port) | |
1665 | { | |
abe529af BP |
1666 | const struct ofproto *ofproto = dump->ofproto; |
1667 | ||
1668 | if (dump->error) { | |
1669 | return false; | |
1670 | } | |
b5827b24 | 1671 | |
abe529af BP |
1672 | dump->error = ofproto->ofproto_class->port_dump_next(ofproto, dump->state, |
1673 | port); | |
1674 | if (dump->error) { | |
1675 | ofproto->ofproto_class->port_dump_done(ofproto, dump->state); | |
1676 | return false; | |
b5827b24 | 1677 | } |
abe529af | 1678 | return true; |
b5827b24 BP |
1679 | } |
1680 | ||
1681 | /* Completes port table dump operation 'dump', which must have been created | |
1682 | * with ofproto_port_dump_start(). Returns 0 if the dump operation was | |
1683 | * error-free, otherwise a positive errno value describing the problem. */ | |
3a6ccc8c | 1684 | int |
b5827b24 | 1685 | ofproto_port_dump_done(struct ofproto_port_dump *dump) |
3a6ccc8c | 1686 | { |
abe529af BP |
1687 | const struct ofproto *ofproto = dump->ofproto; |
1688 | if (!dump->error) { | |
1689 | dump->error = ofproto->ofproto_class->port_dump_done(ofproto, | |
1690 | dump->state); | |
1691 | } | |
1692 | return dump->error == EOF ? 0 : dump->error; | |
b5827b24 BP |
1693 | } |
1694 | ||
0aeaabc8 JP |
1695 | /* Returns the type to pass to netdev_open() when a datapath of type |
1696 | * 'datapath_type' has a port of type 'port_type', for a few special | |
1697 | * cases when a netdev type differs from a port type. For example, when | |
1698 | * using the userspace datapath, a port of type "internal" needs to be | |
1699 | * opened as "tap". | |
1700 | * | |
1701 | * Returns either 'type' itself or a string literal, which must not be | |
1702 | * freed. */ | |
1703 | const char * | |
1704 | ofproto_port_open_type(const char *datapath_type, const char *port_type) | |
1705 | { | |
1706 | const struct ofproto_class *class; | |
1707 | ||
1708 | datapath_type = ofproto_normalize_type(datapath_type); | |
1709 | class = ofproto_class_find__(datapath_type); | |
1710 | if (!class) { | |
1711 | return port_type; | |
1712 | } | |
1713 | ||
1714 | return (class->port_open_type | |
1715 | ? class->port_open_type(datapath_type, port_type) | |
1716 | : port_type); | |
1717 | } | |
1718 | ||
81816a5f JP |
1719 | /* Attempts to add 'netdev' as a port on 'ofproto'. If 'ofp_portp' is |
1720 | * non-null and '*ofp_portp' is not OFPP_NONE, attempts to use that as | |
1721 | * the port's OpenFlow port number. | |
1722 | * | |
1723 | * If successful, returns 0 and sets '*ofp_portp' to the new port's | |
1724 | * OpenFlow port number (if 'ofp_portp' is non-null). On failure, | |
1725 | * returns a positive errno value and sets '*ofp_portp' to OFPP_NONE (if | |
1726 | * 'ofp_portp' is non-null). */ | |
b5827b24 BP |
1727 | int |
1728 | ofproto_port_add(struct ofproto *ofproto, struct netdev *netdev, | |
4e022ec0 | 1729 | ofp_port_t *ofp_portp) |
b5827b24 | 1730 | { |
4e022ec0 | 1731 | ofp_port_t ofp_port = ofp_portp ? *ofp_portp : OFPP_NONE; |
3a6ccc8c BP |
1732 | int error; |
1733 | ||
e1b1d06a | 1734 | error = ofproto->ofproto_class->port_add(ofproto, netdev); |
1fa24dea | 1735 | if (!error) { |
e1b1d06a JP |
1736 | const char *netdev_name = netdev_get_name(netdev); |
1737 | ||
4e022ec0 AW |
1738 | simap_put(&ofproto->ofp_requests, netdev_name, |
1739 | ofp_to_u16(ofp_port)); | |
e1b1d06a | 1740 | update_port(ofproto, netdev_name); |
1fa24dea | 1741 | } |
b5827b24 | 1742 | if (ofp_portp) { |
e1b1d06a JP |
1743 | struct ofproto_port ofproto_port; |
1744 | ||
1745 | ofproto_port_query_by_name(ofproto, netdev_get_name(netdev), | |
1746 | &ofproto_port); | |
1747 | *ofp_portp = error ? OFPP_NONE : ofproto_port.ofp_port; | |
1748 | ofproto_port_destroy(&ofproto_port); | |
3a6ccc8c BP |
1749 | } |
1750 | return error; | |
1751 | } | |
1752 | ||
b5827b24 BP |
1753 | /* Looks up a port named 'devname' in 'ofproto'. On success, returns 0 and |
1754 | * initializes '*port' appropriately; on failure, returns a positive errno | |
1755 | * value. | |
1756 | * | |
abe529af | 1757 | * The caller owns the data in 'ofproto_port' and must free it with |
b5827b24 BP |
1758 | * ofproto_port_destroy() when it is no longer needed. */ |
1759 | int | |
1760 | ofproto_port_query_by_name(const struct ofproto *ofproto, const char *devname, | |
1761 | struct ofproto_port *port) | |
a4e2e1f2 | 1762 | { |
b5827b24 BP |
1763 | int error; |
1764 | ||
abe529af BP |
1765 | error = ofproto->ofproto_class->port_query_by_name(ofproto, devname, port); |
1766 | if (error) { | |
1767 | memset(port, 0, sizeof *port); | |
b5827b24 BP |
1768 | } |
1769 | return error; | |
a4e2e1f2 EJ |
1770 | } |
1771 | ||
b5827b24 | 1772 | /* Deletes port number 'ofp_port' from the datapath for 'ofproto'. |
3a6ccc8c | 1773 | * Returns 0 if successful, otherwise a positive errno. */ |
064af421 | 1774 | int |
4e022ec0 | 1775 | ofproto_port_del(struct ofproto *ofproto, ofp_port_t ofp_port) |
064af421 | 1776 | { |
abe529af | 1777 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); |
1264ec95 | 1778 | const char *name = ofport ? netdev_get_name(ofport->netdev) : "<unknown>"; |
e1b1d06a | 1779 | struct simap_node *ofp_request_node; |
3cf10406 | 1780 | int error; |
cdee00fd | 1781 | |
e1b1d06a JP |
1782 | ofp_request_node = simap_find(&ofproto->ofp_requests, name); |
1783 | if (ofp_request_node) { | |
1784 | simap_delete(&ofproto->ofp_requests, ofp_request_node); | |
1785 | } | |
1786 | ||
abe529af | 1787 | error = ofproto->ofproto_class->port_del(ofproto, ofp_port); |
892815f5 | 1788 | if (!error && ofport) { |
1264ec95 BP |
1789 | /* 'name' is the netdev's name and update_port() is going to close the |
1790 | * netdev. Just in case update_port() refers to 'name' after it | |
3a6ccc8c BP |
1791 | * destroys 'ofport', make a copy of it around the update_port() |
1792 | * call. */ | |
1793 | char *devname = xstrdup(name); | |
1794 | update_port(ofproto, devname); | |
1795 | free(devname); | |
3cf10406 BP |
1796 | } |
1797 | return error; | |
064af421 BP |
1798 | } |
1799 | ||
97f63e57 BP |
1800 | static int |
1801 | simple_flow_mod(struct ofproto *ofproto, | |
1802 | const struct match *match, unsigned int priority, | |
1803 | const struct ofpact *ofpacts, size_t ofpacts_len, | |
1804 | enum ofp_flow_mod_command command) | |
1805 | { | |
1806 | struct ofputil_flow_mod fm; | |
1807 | ||
1808 | memset(&fm, 0, sizeof fm); | |
1809 | fm.match = *match; | |
1810 | fm.priority = priority; | |
1811 | fm.cookie = 0; | |
1812 | fm.new_cookie = 0; | |
1813 | fm.modify_cookie = false; | |
1814 | fm.table_id = 0; | |
1815 | fm.command = command; | |
1816 | fm.idle_timeout = 0; | |
1817 | fm.hard_timeout = 0; | |
1818 | fm.buffer_id = UINT32_MAX; | |
1819 | fm.out_port = OFPP_ANY; | |
1820 | fm.out_group = OFPG_ANY; | |
1821 | fm.flags = 0; | |
1822 | fm.ofpacts = CONST_CAST(struct ofpact *, ofpacts); | |
1823 | fm.ofpacts_len = ofpacts_len; | |
15aaf599 | 1824 | |
97f63e57 BP |
1825 | return handle_flow_mod__(ofproto, NULL, &fm, NULL); |
1826 | } | |
1827 | ||
6c1491fb | 1828 | /* Adds a flow to OpenFlow flow table 0 in 'p' that matches 'cls_rule' and |
fa8b054f BP |
1829 | * performs the 'n_actions' actions in 'actions'. The new flow will not |
1830 | * timeout. | |
1831 | * | |
1832 | * If cls_rule->priority is in the range of priorities supported by OpenFlow | |
1833 | * (0...65535, inclusive) then the flow will be visible to OpenFlow | |
1834 | * controllers; otherwise, it will be hidden. | |
1835 | * | |
f25d0cf3 | 1836 | * The caller retains ownership of 'cls_rule' and 'ofpacts'. |
6c1491fb BP |
1837 | * |
1838 | * This is a helper function for in-band control and fail-open. */ | |
064af421 | 1839 | void |
81a76618 BP |
1840 | ofproto_add_flow(struct ofproto *ofproto, const struct match *match, |
1841 | unsigned int priority, | |
f25d0cf3 | 1842 | const struct ofpact *ofpacts, size_t ofpacts_len) |
15aaf599 | 1843 | OVS_EXCLUDED(ofproto_mutex) |
064af421 | 1844 | { |
7ee20df1 | 1845 | const struct rule *rule; |
6f00e29b | 1846 | bool must_add; |
7ee20df1 | 1847 | |
97f63e57 BP |
1848 | /* First do a cheap check whether the rule we're looking for already exists |
1849 | * with the actions that we want. If it does, then we're done. */ | |
0b4f2078 | 1850 | ovs_rwlock_rdlock(&ofproto->tables[0].cls.rwlock); |
81a76618 BP |
1851 | rule = rule_from_cls_rule(classifier_find_match_exactly( |
1852 | &ofproto->tables[0].cls, match, priority)); | |
6f00e29b | 1853 | if (rule) { |
c7c9a7c8 | 1854 | ovs_mutex_lock(&rule->mutex); |
6f00e29b BP |
1855 | must_add = !ofpacts_equal(rule->actions->ofpacts, |
1856 | rule->actions->ofpacts_len, | |
1857 | ofpacts, ofpacts_len); | |
c7c9a7c8 | 1858 | ovs_mutex_unlock(&rule->mutex); |
6f00e29b BP |
1859 | } else { |
1860 | must_add = true; | |
1861 | } | |
0b4f2078 | 1862 | ovs_rwlock_unlock(&ofproto->tables[0].cls.rwlock); |
97f63e57 BP |
1863 | |
1864 | /* If there's no such rule or the rule doesn't have the actions we want, | |
1865 | * fall back to a executing a full flow mod. We can't optimize this at | |
1866 | * all because we didn't take enough locks above to ensure that the flow | |
1867 | * table didn't already change beneath us. */ | |
6f00e29b | 1868 | if (must_add) { |
97f63e57 BP |
1869 | simple_flow_mod(ofproto, match, priority, ofpacts, ofpacts_len, |
1870 | OFPFC_MODIFY_STRICT); | |
7ee20df1 | 1871 | } |
064af421 BP |
1872 | } |
1873 | ||
75a75043 | 1874 | /* Executes the flow modification specified in 'fm'. Returns 0 on success, an |
90bf1e07 BP |
1875 | * OFPERR_* OpenFlow error code on failure, or OFPROTO_POSTPONE if the |
1876 | * operation cannot be initiated now but may be retried later. | |
75a75043 | 1877 | * |
1b63b91e BP |
1878 | * This is a helper function for in-band control and fail-open and the "learn" |
1879 | * action. */ | |
75a75043 | 1880 | int |
e3b56933 | 1881 | ofproto_flow_mod(struct ofproto *ofproto, struct ofputil_flow_mod *fm) |
15aaf599 | 1882 | OVS_EXCLUDED(ofproto_mutex) |
75a75043 BP |
1883 | { |
1884 | return handle_flow_mod__(ofproto, NULL, fm, NULL); | |
1885 | } | |
1886 | ||
6c1491fb BP |
1887 | /* Searches for a rule with matching criteria exactly equal to 'target' in |
1888 | * ofproto's table 0 and, if it finds one, deletes it. | |
1889 | * | |
1890 | * This is a helper function for in-band control and fail-open. */ | |
7ee20df1 | 1891 | bool |
81a76618 BP |
1892 | ofproto_delete_flow(struct ofproto *ofproto, |
1893 | const struct match *target, unsigned int priority) | |
15aaf599 | 1894 | OVS_EXCLUDED(ofproto_mutex) |
064af421 | 1895 | { |
8037acb4 | 1896 | struct classifier *cls = &ofproto->tables[0].cls; |
064af421 BP |
1897 | struct rule *rule; |
1898 | ||
97f63e57 BP |
1899 | /* First do a cheap check whether the rule we're looking for has already |
1900 | * been deleted. If so, then we're done. */ | |
8037acb4 BP |
1901 | ovs_rwlock_rdlock(&cls->rwlock); |
1902 | rule = rule_from_cls_rule(classifier_find_match_exactly(cls, target, | |
1903 | priority)); | |
1904 | ovs_rwlock_unlock(&cls->rwlock); | |
7ee20df1 | 1905 | if (!rule) { |
7ee20df1 | 1906 | return true; |
bcf84111 | 1907 | } |
5ecc9d81 | 1908 | |
97f63e57 BP |
1909 | /* Fall back to a executing a full flow mod. We can't optimize this at all |
1910 | * because we didn't take enough locks above to ensure that the flow table | |
1911 | * didn't already change beneath us. */ | |
1912 | return simple_flow_mod(ofproto, target, priority, NULL, 0, | |
1913 | OFPFC_DELETE_STRICT) != OFPROTO_POSTPONE; | |
142e1f5c BP |
1914 | } |
1915 | ||
7ee20df1 BP |
1916 | /* Starts the process of deleting all of the flows from all of ofproto's flow |
1917 | * tables and then reintroducing the flows required by in-band control and | |
1918 | * fail-open. The process will complete in a later call to ofproto_run(). */ | |
142e1f5c BP |
1919 | void |
1920 | ofproto_flush_flows(struct ofproto *ofproto) | |
1921 | { | |
7ee20df1 BP |
1922 | COVERAGE_INC(ofproto_flush); |
1923 | ofproto->state = S_FLUSH; | |
064af421 BP |
1924 | } |
1925 | \f | |
1926 | static void | |
1927 | reinit_ports(struct ofproto *p) | |
1928 | { | |
abe529af | 1929 | struct ofproto_port_dump dump; |
b3c01ed3 | 1930 | struct sset devnames; |
064af421 | 1931 | struct ofport *ofport; |
abe529af | 1932 | struct ofproto_port ofproto_port; |
b3c01ed3 | 1933 | const char *devname; |
064af421 | 1934 | |
898bf89d JP |
1935 | COVERAGE_INC(ofproto_reinit_ports); |
1936 | ||
b3c01ed3 | 1937 | sset_init(&devnames); |
4e8e4213 | 1938 | HMAP_FOR_EACH (ofport, hmap_node, &p->ports) { |
1264ec95 | 1939 | sset_add(&devnames, netdev_get_name(ofport->netdev)); |
064af421 | 1940 | } |
abe529af BP |
1941 | OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) { |
1942 | sset_add(&devnames, ofproto_port.name); | |
064af421 | 1943 | } |
064af421 | 1944 | |
b3c01ed3 BP |
1945 | SSET_FOR_EACH (devname, &devnames) { |
1946 | update_port(p, devname); | |
064af421 | 1947 | } |
b3c01ed3 | 1948 | sset_destroy(&devnames); |
064af421 BP |
1949 | } |
1950 | ||
4e022ec0 | 1951 | static ofp_port_t |
e1b1d06a JP |
1952 | alloc_ofp_port(struct ofproto *ofproto, const char *netdev_name) |
1953 | { | |
4e022ec0 | 1954 | uint16_t port_idx; |
e1b1d06a | 1955 | |
4e022ec0 | 1956 | port_idx = simap_get(&ofproto->ofp_requests, netdev_name); |
430dbb14 | 1957 | port_idx = port_idx ? port_idx : UINT16_MAX; |
4e022ec0 | 1958 | |
430dbb14 | 1959 | if (port_idx >= ofproto->max_ports |
fdcea803 GS |
1960 | || ofport_get_usage(ofproto, u16_to_ofp(port_idx)) == LLONG_MAX) { |
1961 | uint16_t lru_ofport = 0, end_port_no = ofproto->alloc_port_no; | |
1962 | long long int last_used_at, lru = LLONG_MAX; | |
e1b1d06a | 1963 | |
e1b1d06a JP |
1964 | /* Search for a free OpenFlow port number. We try not to |
1965 | * immediately reuse them to prevent problems due to old | |
1966 | * flows. */ | |
6033d9d9 | 1967 | for (;;) { |
430dbb14 | 1968 | if (++ofproto->alloc_port_no >= ofproto->max_ports) { |
fdcea803 | 1969 | ofproto->alloc_port_no = 1; |
6033d9d9 | 1970 | } |
fdcea803 GS |
1971 | last_used_at = ofport_get_usage(ofproto, |
1972 | u16_to_ofp(ofproto->alloc_port_no)); | |
1973 | if (!last_used_at) { | |
430dbb14 | 1974 | port_idx = ofproto->alloc_port_no; |
6033d9d9 | 1975 | break; |
865b26a4 GS |
1976 | } else if ( last_used_at < time_msec() - 60*60*1000) { |
1977 | /* If the port with ofport 'ofproto->alloc_port_no' was deleted | |
1978 | * more than an hour ago, consider it usable. */ | |
1979 | ofport_remove_usage(ofproto, | |
1980 | u16_to_ofp(ofproto->alloc_port_no)); | |
1981 | port_idx = ofproto->alloc_port_no; | |
1982 | break; | |
fdcea803 GS |
1983 | } else if (last_used_at < lru) { |
1984 | lru = last_used_at; | |
1985 | lru_ofport = ofproto->alloc_port_no; | |
e1b1d06a | 1986 | } |
fdcea803 | 1987 | |
430dbb14 | 1988 | if (ofproto->alloc_port_no == end_port_no) { |
fdcea803 GS |
1989 | if (lru_ofport) { |
1990 | port_idx = lru_ofport; | |
1991 | break; | |
1992 | } | |
6033d9d9 | 1993 | return OFPP_NONE; |
e1b1d06a JP |
1994 | } |
1995 | } | |
1996 | } | |
fdcea803 | 1997 | ofport_set_usage(ofproto, u16_to_ofp(port_idx), LLONG_MAX); |
4e022ec0 | 1998 | return u16_to_ofp(port_idx); |
e1b1d06a JP |
1999 | } |
2000 | ||
2001 | static void | |
fdcea803 | 2002 | dealloc_ofp_port(struct ofproto *ofproto, ofp_port_t ofp_port) |
e1b1d06a | 2003 | { |
430dbb14 | 2004 | if (ofp_to_u16(ofp_port) < ofproto->max_ports) { |
fdcea803 | 2005 | ofport_set_usage(ofproto, ofp_port, time_msec()); |
40fa9417 | 2006 | } |
e1b1d06a JP |
2007 | } |
2008 | ||
fbfa2911 BP |
2009 | /* Opens and returns a netdev for 'ofproto_port' in 'ofproto', or a null |
2010 | * pointer if the netdev cannot be opened. On success, also fills in | |
2011 | * 'opp'. */ | |
b33951b8 | 2012 | static struct netdev * |
e1b1d06a JP |
2013 | ofport_open(struct ofproto *ofproto, |
2014 | struct ofproto_port *ofproto_port, | |
9e1fd49b | 2015 | struct ofputil_phy_port *pp) |
064af421 BP |
2016 | { |
2017 | enum netdev_flags flags; | |
064af421 | 2018 | struct netdev *netdev; |
064af421 BP |
2019 | int error; |
2020 | ||
18812dff | 2021 | error = netdev_open(ofproto_port->name, ofproto_port->type, &netdev); |
064af421 | 2022 | if (error) { |
fbfa2911 | 2023 | VLOG_WARN_RL(&rl, "%s: ignoring port %s (%"PRIu16") because netdev %s " |
064af421 | 2024 | "cannot be opened (%s)", |
fbfa2911 | 2025 | ofproto->name, |
abe529af | 2026 | ofproto_port->name, ofproto_port->ofp_port, |
10a89ef0 | 2027 | ofproto_port->name, ovs_strerror(error)); |
064af421 BP |
2028 | return NULL; |
2029 | } | |
2030 | ||
e1b1d06a JP |
2031 | if (ofproto_port->ofp_port == OFPP_NONE) { |
2032 | if (!strcmp(ofproto->name, ofproto_port->name)) { | |
2033 | ofproto_port->ofp_port = OFPP_LOCAL; | |
2034 | } else { | |
2035 | ofproto_port->ofp_port = alloc_ofp_port(ofproto, | |
2036 | ofproto_port->name); | |
2037 | } | |
2038 | } | |
9e1fd49b BP |
2039 | pp->port_no = ofproto_port->ofp_port; |
2040 | netdev_get_etheraddr(netdev, pp->hw_addr); | |
2041 | ovs_strlcpy(pp->name, ofproto_port->name, sizeof pp->name); | |
064af421 | 2042 | netdev_get_flags(netdev, &flags); |
9e1fd49b BP |
2043 | pp->config = flags & NETDEV_UP ? 0 : OFPUTIL_PC_PORT_DOWN; |
2044 | pp->state = netdev_get_carrier(netdev) ? 0 : OFPUTIL_PS_LINK_DOWN; | |
2045 | netdev_get_features(netdev, &pp->curr, &pp->advertised, | |
2046 | &pp->supported, &pp->peer); | |
cd65125d BP |
2047 | pp->curr_speed = netdev_features_to_bps(pp->curr, 0) / 1000; |
2048 | pp->max_speed = netdev_features_to_bps(pp->supported, 0) / 1000; | |
118c4676 | 2049 | |
b33951b8 | 2050 | return netdev; |
064af421 BP |
2051 | } |
2052 | ||
b33951b8 | 2053 | /* Returns true if most fields of 'a' and 'b' are equal. Differences in name, |
9e1fd49b | 2054 | * port number, and 'config' bits other than OFPUTIL_PS_LINK_DOWN are |
b33951b8 BP |
2055 | * disregarded. */ |
2056 | static bool | |
9e1fd49b BP |
2057 | ofport_equal(const struct ofputil_phy_port *a, |
2058 | const struct ofputil_phy_port *b) | |
064af421 | 2059 | { |
9e1fd49b | 2060 | return (eth_addr_equals(a->hw_addr, b->hw_addr) |
064af421 | 2061 | && a->state == b->state |
9e1fd49b | 2062 | && !((a->config ^ b->config) & OFPUTIL_PC_PORT_DOWN) |
064af421 BP |
2063 | && a->curr == b->curr |
2064 | && a->advertised == b->advertised | |
2065 | && a->supported == b->supported | |
9e1fd49b BP |
2066 | && a->peer == b->peer |
2067 | && a->curr_speed == b->curr_speed | |
2068 | && a->max_speed == b->max_speed); | |
064af421 BP |
2069 | } |
2070 | ||
b33951b8 BP |
2071 | /* Adds an ofport to 'p' initialized based on the given 'netdev' and 'opp'. |
2072 | * The caller must ensure that 'p' does not have a conflicting ofport (that is, | |
2073 | * one with the same name or port number). */ | |
064af421 | 2074 | static void |
b33951b8 | 2075 | ofport_install(struct ofproto *p, |
9e1fd49b | 2076 | struct netdev *netdev, const struct ofputil_phy_port *pp) |
064af421 | 2077 | { |
b33951b8 BP |
2078 | const char *netdev_name = netdev_get_name(netdev); |
2079 | struct ofport *ofport; | |
abe529af | 2080 | int error; |
72b06300 | 2081 | |
b33951b8 | 2082 | /* Create ofport. */ |
abe529af BP |
2083 | ofport = p->ofproto_class->port_alloc(); |
2084 | if (!ofport) { | |
2085 | error = ENOMEM; | |
2086 | goto error; | |
2087 | } | |
0f7d71a5 | 2088 | ofport->ofproto = p; |
b33951b8 | 2089 | ofport->netdev = netdev; |
031d8bff | 2090 | ofport->change_seq = netdev_change_seq(netdev); |
9e1fd49b BP |
2091 | ofport->pp = *pp; |
2092 | ofport->ofp_port = pp->port_no; | |
65e0be10 | 2093 | ofport->created = time_msec(); |
b33951b8 BP |
2094 | |
2095 | /* Add port to 'p'. */ | |
4e022ec0 | 2096 | hmap_insert(&p->ports, &ofport->hmap_node, |
f9c0c3ec | 2097 | hash_ofp_port(ofport->ofp_port)); |
72b06300 | 2098 | shash_add(&p->port_by_name, netdev_name, ofport); |
abe529af | 2099 | |
ada3428f | 2100 | update_mtu(p, ofport); |
9197df76 | 2101 | |
abe529af BP |
2102 | /* Let the ofproto_class initialize its private data. */ |
2103 | error = p->ofproto_class->port_construct(ofport); | |
2104 | if (error) { | |
2105 | goto error; | |
2106 | } | |
9e1fd49b | 2107 | connmgr_send_port_status(p->connmgr, pp, OFPPR_ADD); |
abe529af BP |
2108 | return; |
2109 | ||
2110 | error: | |
2111 | VLOG_WARN_RL(&rl, "%s: could not add port %s (%s)", | |
10a89ef0 | 2112 | p->name, netdev_name, ovs_strerror(error)); |
abe529af BP |
2113 | if (ofport) { |
2114 | ofport_destroy__(ofport); | |
2115 | } else { | |
2116 | netdev_close(netdev); | |
72b06300 | 2117 | } |
064af421 BP |
2118 | } |
2119 | ||
b33951b8 | 2120 | /* Removes 'ofport' from 'p' and destroys it. */ |
064af421 | 2121 | static void |
0f7d71a5 | 2122 | ofport_remove(struct ofport *ofport) |
064af421 | 2123 | { |
9e1fd49b | 2124 | connmgr_send_port_status(ofport->ofproto->connmgr, &ofport->pp, |
fa066f01 | 2125 | OFPPR_DELETE); |
abe529af | 2126 | ofport_destroy(ofport); |
b33951b8 BP |
2127 | } |
2128 | ||
2129 | /* If 'ofproto' contains an ofport named 'name', removes it from 'ofproto' and | |
2130 | * destroys it. */ | |
2131 | static void | |
2132 | ofport_remove_with_name(struct ofproto *ofproto, const char *name) | |
2133 | { | |
2134 | struct ofport *port = shash_find_data(&ofproto->port_by_name, name); | |
2135 | if (port) { | |
0f7d71a5 | 2136 | ofport_remove(port); |
b33951b8 BP |
2137 | } |
2138 | } | |
2139 | ||
9e1fd49b | 2140 | /* Updates 'port' with new 'pp' description. |
b33951b8 BP |
2141 | * |
2142 | * Does not handle a name or port number change. The caller must implement | |
2143 | * such a change as a delete followed by an add. */ | |
2144 | static void | |
9e1fd49b | 2145 | ofport_modified(struct ofport *port, struct ofputil_phy_port *pp) |
b33951b8 | 2146 | { |
9e1fd49b BP |
2147 | memcpy(port->pp.hw_addr, pp->hw_addr, ETH_ADDR_LEN); |
2148 | port->pp.config = ((port->pp.config & ~OFPUTIL_PC_PORT_DOWN) | |
2149 | | (pp->config & OFPUTIL_PC_PORT_DOWN)); | |
2150 | port->pp.state = pp->state; | |
2151 | port->pp.curr = pp->curr; | |
2152 | port->pp.advertised = pp->advertised; | |
2153 | port->pp.supported = pp->supported; | |
2154 | port->pp.peer = pp->peer; | |
2155 | port->pp.curr_speed = pp->curr_speed; | |
2156 | port->pp.max_speed = pp->max_speed; | |
b33951b8 | 2157 | |
9e1fd49b | 2158 | connmgr_send_port_status(port->ofproto->connmgr, &port->pp, OFPPR_MODIFY); |
064af421 BP |
2159 | } |
2160 | ||
5a2dfd47 JP |
2161 | /* Update OpenFlow 'state' in 'port' and notify controller. */ |
2162 | void | |
9e1fd49b | 2163 | ofproto_port_set_state(struct ofport *port, enum ofputil_port_state state) |
5a2dfd47 | 2164 | { |
9e1fd49b BP |
2165 | if (port->pp.state != state) { |
2166 | port->pp.state = state; | |
2167 | connmgr_send_port_status(port->ofproto->connmgr, &port->pp, | |
5a2dfd47 JP |
2168 | OFPPR_MODIFY); |
2169 | } | |
2170 | } | |
2171 | ||
abe529af | 2172 | void |
4e022ec0 | 2173 | ofproto_port_unregister(struct ofproto *ofproto, ofp_port_t ofp_port) |
e7934396 | 2174 | { |
abe529af BP |
2175 | struct ofport *port = ofproto_get_port(ofproto, ofp_port); |
2176 | if (port) { | |
52a90c29 BP |
2177 | if (port->ofproto->ofproto_class->set_realdev) { |
2178 | port->ofproto->ofproto_class->set_realdev(port, 0, 0); | |
2179 | } | |
b308140a JP |
2180 | if (port->ofproto->ofproto_class->set_stp_port) { |
2181 | port->ofproto->ofproto_class->set_stp_port(port, NULL); | |
2182 | } | |
abe529af | 2183 | if (port->ofproto->ofproto_class->set_cfm) { |
a5610457 | 2184 | port->ofproto->ofproto_class->set_cfm(port, NULL); |
abe529af BP |
2185 | } |
2186 | if (port->ofproto->ofproto_class->bundle_remove) { | |
2187 | port->ofproto->ofproto_class->bundle_remove(port); | |
e7934396 BP |
2188 | } |
2189 | } | |
2190 | } | |
2191 | ||
2192 | static void | |
abe529af | 2193 | ofport_destroy__(struct ofport *port) |
e7934396 | 2194 | { |
abe529af BP |
2195 | struct ofproto *ofproto = port->ofproto; |
2196 | const char *name = netdev_get_name(port->netdev); | |
fa066f01 | 2197 | |
abe529af BP |
2198 | hmap_remove(&ofproto->ports, &port->hmap_node); |
2199 | shash_delete(&ofproto->port_by_name, | |
2200 | shash_find(&ofproto->port_by_name, name)); | |
fa066f01 | 2201 | |
abe529af BP |
2202 | netdev_close(port->netdev); |
2203 | ofproto->ofproto_class->port_dealloc(port); | |
e7934396 BP |
2204 | } |
2205 | ||
064af421 | 2206 | static void |
abe529af | 2207 | ofport_destroy(struct ofport *port) |
064af421 | 2208 | { |
fa066f01 | 2209 | if (port) { |
e1b1d06a | 2210 | dealloc_ofp_port(port->ofproto, port->ofp_port); |
abe529af BP |
2211 | port->ofproto->ofproto_class->port_destruct(port); |
2212 | ofport_destroy__(port); | |
2213 | } | |
064af421 BP |
2214 | } |
2215 | ||
abe529af | 2216 | struct ofport * |
4e022ec0 | 2217 | ofproto_get_port(const struct ofproto *ofproto, ofp_port_t ofp_port) |
ca0f572c BP |
2218 | { |
2219 | struct ofport *port; | |
2220 | ||
f9c0c3ec | 2221 | HMAP_FOR_EACH_IN_BUCKET (port, hmap_node, hash_ofp_port(ofp_port), |
4e022ec0 | 2222 | &ofproto->ports) { |
abe529af | 2223 | if (port->ofp_port == ofp_port) { |
ca0f572c BP |
2224 | return port; |
2225 | } | |
2226 | } | |
2227 | return NULL; | |
2228 | } | |
2229 | ||
fdcea803 GS |
2230 | static long long int |
2231 | ofport_get_usage(const struct ofproto *ofproto, ofp_port_t ofp_port) | |
2232 | { | |
2233 | struct ofport_usage *usage; | |
2234 | ||
2235 | HMAP_FOR_EACH_IN_BUCKET (usage, hmap_node, hash_ofp_port(ofp_port), | |
2236 | &ofproto->ofport_usage) { | |
2237 | if (usage->ofp_port == ofp_port) { | |
2238 | return usage->last_used; | |
2239 | } | |
2240 | } | |
2241 | return 0; | |
2242 | } | |
2243 | ||
2244 | static void | |
2245 | ofport_set_usage(struct ofproto *ofproto, ofp_port_t ofp_port, | |
2246 | long long int last_used) | |
2247 | { | |
2248 | struct ofport_usage *usage; | |
2249 | HMAP_FOR_EACH_IN_BUCKET (usage, hmap_node, hash_ofp_port(ofp_port), | |
2250 | &ofproto->ofport_usage) { | |
2251 | if (usage->ofp_port == ofp_port) { | |
2252 | usage->last_used = last_used; | |
2253 | return; | |
2254 | } | |
2255 | } | |
2256 | ovs_assert(last_used == LLONG_MAX); | |
2257 | ||
2258 | usage = xmalloc(sizeof *usage); | |
2259 | usage->ofp_port = ofp_port; | |
2260 | usage->last_used = last_used; | |
2261 | hmap_insert(&ofproto->ofport_usage, &usage->hmap_node, | |
2262 | hash_ofp_port(ofp_port)); | |
2263 | } | |
2264 | ||
865b26a4 GS |
2265 | static void |
2266 | ofport_remove_usage(struct ofproto *ofproto, ofp_port_t ofp_port) | |
2267 | { | |
2268 | struct ofport_usage *usage; | |
2269 | HMAP_FOR_EACH_IN_BUCKET (usage, hmap_node, hash_ofp_port(ofp_port), | |
2270 | &ofproto->ofport_usage) { | |
2271 | if (usage->ofp_port == ofp_port) { | |
2272 | hmap_remove(&ofproto->ofport_usage, &usage->hmap_node); | |
2273 | free(usage); | |
2274 | break; | |
2275 | } | |
2276 | } | |
2277 | } | |
2278 | ||
6527c598 PS |
2279 | int |
2280 | ofproto_port_get_stats(const struct ofport *port, struct netdev_stats *stats) | |
2281 | { | |
2282 | struct ofproto *ofproto = port->ofproto; | |
2283 | int error; | |
2284 | ||
2285 | if (ofproto->ofproto_class->port_get_stats) { | |
2286 | error = ofproto->ofproto_class->port_get_stats(port, stats); | |
2287 | } else { | |
2288 | error = EOPNOTSUPP; | |
2289 | } | |
2290 | ||
2291 | return error; | |
2292 | } | |
2293 | ||
064af421 | 2294 | static void |
b33951b8 | 2295 | update_port(struct ofproto *ofproto, const char *name) |
064af421 | 2296 | { |
abe529af | 2297 | struct ofproto_port ofproto_port; |
9e1fd49b | 2298 | struct ofputil_phy_port pp; |
b33951b8 BP |
2299 | struct netdev *netdev; |
2300 | struct ofport *port; | |
064af421 BP |
2301 | |
2302 | COVERAGE_INC(ofproto_update_port); | |
c874dc6d | 2303 | |
b33951b8 | 2304 | /* Fetch 'name''s location and properties from the datapath. */ |
abe529af | 2305 | netdev = (!ofproto_port_query_by_name(ofproto, name, &ofproto_port) |
fbfa2911 | 2306 | ? ofport_open(ofproto, &ofproto_port, &pp) |
b33951b8 | 2307 | : NULL); |
4e022ec0 | 2308 | |
b33951b8 | 2309 | if (netdev) { |
abe529af | 2310 | port = ofproto_get_port(ofproto, ofproto_port.ofp_port); |
b33951b8 | 2311 | if (port && !strcmp(netdev_get_name(port->netdev), name)) { |
e65942a0 BP |
2312 | struct netdev *old_netdev = port->netdev; |
2313 | ||
b33951b8 | 2314 | /* 'name' hasn't changed location. Any properties changed? */ |
9e1fd49b BP |
2315 | if (!ofport_equal(&port->pp, &pp)) { |
2316 | ofport_modified(port, &pp); | |
abe529af BP |
2317 | } |
2318 | ||
ada3428f | 2319 | update_mtu(ofproto, port); |
9197df76 | 2320 | |
e65942a0 BP |
2321 | /* Install the newly opened netdev in case it has changed. |
2322 | * Don't close the old netdev yet in case port_modified has to | |
2323 | * remove a retained reference to it.*/ | |
abe529af | 2324 | port->netdev = netdev; |
031d8bff | 2325 | port->change_seq = netdev_change_seq(netdev); |
abe529af BP |
2326 | |
2327 | if (port->ofproto->ofproto_class->port_modified) { | |
2328 | port->ofproto->ofproto_class->port_modified(port); | |
b33951b8 | 2329 | } |
e65942a0 BP |
2330 | |
2331 | netdev_close(old_netdev); | |
b33951b8 BP |
2332 | } else { |
2333 | /* If 'port' is nonnull then its name differs from 'name' and thus | |
2334 | * we should delete it. If we think there's a port named 'name' | |
2335 | * then its port number must be wrong now so delete it too. */ | |
2336 | if (port) { | |
0f7d71a5 | 2337 | ofport_remove(port); |
b33951b8 BP |
2338 | } |
2339 | ofport_remove_with_name(ofproto, name); | |
9e1fd49b | 2340 | ofport_install(ofproto, netdev, &pp); |
c874dc6d | 2341 | } |
b33951b8 BP |
2342 | } else { |
2343 | /* Any port named 'name' is gone now. */ | |
2344 | ofport_remove_with_name(ofproto, name); | |
c874dc6d | 2345 | } |
abe529af | 2346 | ofproto_port_destroy(&ofproto_port); |
064af421 BP |
2347 | } |
2348 | ||
2349 | static int | |
2350 | init_ports(struct ofproto *p) | |
2351 | { | |
abe529af BP |
2352 | struct ofproto_port_dump dump; |
2353 | struct ofproto_port ofproto_port; | |
e1b1d06a | 2354 | struct shash_node *node, *next; |
abe529af BP |
2355 | |
2356 | OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, p) { | |
e1b1d06a JP |
2357 | const char *name = ofproto_port.name; |
2358 | ||
2359 | if (shash_find(&p->port_by_name, name)) { | |
fbfa2911 | 2360 | VLOG_WARN_RL(&rl, "%s: ignoring duplicate device %s in datapath", |
e1b1d06a | 2361 | p->name, name); |
abe529af | 2362 | } else { |
9e1fd49b | 2363 | struct ofputil_phy_port pp; |
b33951b8 BP |
2364 | struct netdev *netdev; |
2365 | ||
e1b1d06a JP |
2366 | /* Check if an OpenFlow port number had been requested. */ |
2367 | node = shash_find(&init_ofp_ports, name); | |
2368 | if (node) { | |
2369 | const struct iface_hint *iface_hint = node->data; | |
4e022ec0 AW |
2370 | simap_put(&p->ofp_requests, name, |
2371 | ofp_to_u16(iface_hint->ofp_port)); | |
e1b1d06a JP |
2372 | } |
2373 | ||
fbfa2911 | 2374 | netdev = ofport_open(p, &ofproto_port, &pp); |
b33951b8 | 2375 | if (netdev) { |
9e1fd49b | 2376 | ofport_install(p, netdev, &pp); |
430dbb14 | 2377 | if (ofp_to_u16(ofproto_port.ofp_port) < p->max_ports) { |
7c35397c | 2378 | p->alloc_port_no = MAX(p->alloc_port_no, |
430dbb14 | 2379 | ofp_to_u16(ofproto_port.ofp_port)); |
7c35397c | 2380 | } |
064af421 BP |
2381 | } |
2382 | } | |
2383 | } | |
b0ec0f27 | 2384 | |
e1b1d06a | 2385 | SHASH_FOR_EACH_SAFE(node, next, &init_ofp_ports) { |
4f9e08a5 | 2386 | struct iface_hint *iface_hint = node->data; |
e1b1d06a JP |
2387 | |
2388 | if (!strcmp(iface_hint->br_name, p->name)) { | |
2389 | free(iface_hint->br_name); | |
2390 | free(iface_hint->br_type); | |
4f9e08a5 | 2391 | free(iface_hint); |
e1b1d06a JP |
2392 | shash_delete(&init_ofp_ports, node); |
2393 | } | |
2394 | } | |
2395 | ||
064af421 BP |
2396 | return 0; |
2397 | } | |
9197df76 JP |
2398 | |
2399 | /* Find the minimum MTU of all non-datapath devices attached to 'p'. | |
2400 | * Returns ETH_PAYLOAD_MAX or the minimum of the ports. */ | |
2401 | static int | |
2402 | find_min_mtu(struct ofproto *p) | |
2403 | { | |
2404 | struct ofport *ofport; | |
2405 | int mtu = 0; | |
2406 | ||
2407 | HMAP_FOR_EACH (ofport, hmap_node, &p->ports) { | |
2408 | struct netdev *netdev = ofport->netdev; | |
2409 | int dev_mtu; | |
2410 | ||
2411 | /* Skip any internal ports, since that's what we're trying to | |
2412 | * set. */ | |
2413 | if (!strcmp(netdev_get_type(netdev), "internal")) { | |
2414 | continue; | |
2415 | } | |
2416 | ||
2417 | if (netdev_get_mtu(netdev, &dev_mtu)) { | |
2418 | continue; | |
2419 | } | |
2420 | if (!mtu || dev_mtu < mtu) { | |
2421 | mtu = dev_mtu; | |
2422 | } | |
2423 | } | |
2424 | ||
2425 | return mtu ? mtu: ETH_PAYLOAD_MAX; | |
2426 | } | |
2427 | ||
ada3428f PS |
2428 | /* Update MTU of all datapath devices on 'p' to the minimum of the |
2429 | * non-datapath ports in event of 'port' added or changed. */ | |
9197df76 | 2430 | static void |
ada3428f | 2431 | update_mtu(struct ofproto *p, struct ofport *port) |
9197df76 JP |
2432 | { |
2433 | struct ofport *ofport; | |
ada3428f PS |
2434 | struct netdev *netdev = port->netdev; |
2435 | int dev_mtu, old_min; | |
2436 | ||
2437 | if (netdev_get_mtu(netdev, &dev_mtu)) { | |
2438 | port->mtu = 0; | |
2439 | return; | |
2440 | } | |
2441 | if (!strcmp(netdev_get_type(port->netdev), "internal")) { | |
2442 | if (dev_mtu > p->min_mtu) { | |
2443 | if (!netdev_set_mtu(port->netdev, p->min_mtu)) { | |
2444 | dev_mtu = p->min_mtu; | |
2445 | } | |
2446 | } | |
2447 | port->mtu = dev_mtu; | |
2448 | return; | |
2449 | } | |
2450 | ||
2451 | /* For non-internal port find new min mtu. */ | |
2452 | old_min = p->min_mtu; | |
2453 | port->mtu = dev_mtu; | |
2454 | p->min_mtu = find_min_mtu(p); | |
2455 | if (p->min_mtu == old_min) { | |
2456 | return; | |
2457 | } | |
9197df76 JP |
2458 | |
2459 | HMAP_FOR_EACH (ofport, hmap_node, &p->ports) { | |
2460 | struct netdev *netdev = ofport->netdev; | |
2461 | ||
2462 | if (!strcmp(netdev_get_type(netdev), "internal")) { | |
ada3428f PS |
2463 | if (!netdev_set_mtu(netdev, p->min_mtu)) { |
2464 | ofport->mtu = p->min_mtu; | |
2465 | } | |
9197df76 JP |
2466 | } |
2467 | } | |
2468 | } | |
064af421 | 2469 | \f |
a2143702 BP |
2470 | void |
2471 | ofproto_rule_ref(struct rule *rule) | |
064af421 | 2472 | { |
1eae3d33 | 2473 | if (rule) { |
a2143702 BP |
2474 | unsigned int orig; |
2475 | ||
2476 | atomic_add(&rule->ref_count, 1, &orig); | |
2477 | ovs_assert(orig != 0); | |
2478 | } | |
2479 | } | |
2480 | ||
2481 | void | |
2482 | ofproto_rule_unref(struct rule *rule) | |
2483 | { | |
2484 | if (rule) { | |
2485 | unsigned int orig; | |
2486 | ||
2487 | atomic_sub(&rule->ref_count, 1, &orig); | |
2488 | if (orig == 1) { | |
2489 | rule->ofproto->ofproto_class->rule_destruct(rule); | |
2490 | ofproto_rule_destroy__(rule); | |
2491 | } else { | |
2492 | ovs_assert(orig != 0); | |
2493 | } | |
1eae3d33 | 2494 | } |
064af421 BP |
2495 | } |
2496 | ||
15aaf599 BP |
2497 | struct rule_actions * |
2498 | rule_get_actions(const struct rule *rule) | |
2499 | OVS_EXCLUDED(rule->mutex) | |
2500 | { | |
2501 | struct rule_actions *actions; | |
2502 | ||
2503 | ovs_mutex_lock(&rule->mutex); | |
2504 | actions = rule_get_actions__(rule); | |
2505 | ovs_mutex_unlock(&rule->mutex); | |
2506 | ||
2507 | return actions; | |
2508 | } | |
2509 | ||
2510 | struct rule_actions * | |
2511 | rule_get_actions__(const struct rule *rule) | |
2512 | OVS_REQUIRES(rule->mutex) | |
2513 | { | |
2514 | rule_actions_ref(rule->actions); | |
2515 | return rule->actions; | |
2516 | } | |
2517 | ||
392caac0 BP |
2518 | static void |
2519 | ofproto_rule_destroy__(struct rule *rule) | |
15aaf599 | 2520 | OVS_NO_THREAD_SAFETY_ANALYSIS |
392caac0 | 2521 | { |
49a0e0eb | 2522 | cls_rule_destroy(CONST_CAST(struct cls_rule *, &rule->cr)); |
6f00e29b | 2523 | rule_actions_unref(rule->actions); |
c7c9a7c8 | 2524 | ovs_mutex_destroy(&rule->mutex); |
392caac0 BP |
2525 | rule->ofproto->ofproto_class->rule_dealloc(rule); |
2526 | } | |
2527 | ||
65efd2ab JR |
2528 | static uint32_t get_provider_meter_id(const struct ofproto *, |
2529 | uint32_t of_meter_id); | |
2530 | ||
6f00e29b BP |
2531 | /* Creates and returns a new 'struct rule_actions', with a ref_count of 1, |
2532 | * whose actions are a copy of from the 'ofpacts_len' bytes of 'ofpacts'. */ | |
2533 | struct rule_actions * | |
65efd2ab JR |
2534 | rule_actions_create(const struct ofproto *ofproto, |
2535 | const struct ofpact *ofpacts, size_t ofpacts_len) | |
6f00e29b BP |
2536 | { |
2537 | struct rule_actions *actions; | |
2538 | ||
2539 | actions = xmalloc(sizeof *actions); | |
2540 | atomic_init(&actions->ref_count, 1); | |
2541 | actions->ofpacts = xmemdup(ofpacts, ofpacts_len); | |
2542 | actions->ofpacts_len = ofpacts_len; | |
65efd2ab JR |
2543 | actions->provider_meter_id |
2544 | = get_provider_meter_id(ofproto, | |
2545 | ofpacts_get_meter(ofpacts, ofpacts_len)); | |
2546 | ||
6f00e29b BP |
2547 | return actions; |
2548 | } | |
2549 | ||
2550 | /* Increments 'actions''s ref_count. */ | |
2551 | void | |
2552 | rule_actions_ref(struct rule_actions *actions) | |
2553 | { | |
2554 | if (actions) { | |
2555 | unsigned int orig; | |
2556 | ||
2557 | atomic_add(&actions->ref_count, 1, &orig); | |
2558 | ovs_assert(orig != 0); | |
2559 | } | |
2560 | } | |
2561 | ||
2562 | /* Decrements 'actions''s ref_count and frees 'actions' if the ref_count | |
2563 | * reaches 0. */ | |
2564 | void | |
2565 | rule_actions_unref(struct rule_actions *actions) | |
2566 | { | |
2567 | if (actions) { | |
2568 | unsigned int orig; | |
2569 | ||
2570 | atomic_sub(&actions->ref_count, 1, &orig); | |
2571 | if (orig == 1) { | |
bab3c0b8 | 2572 | free(actions->ofpacts); |
6f00e29b BP |
2573 | free(actions); |
2574 | } else { | |
2575 | ovs_assert(orig != 0); | |
2576 | } | |
2577 | } | |
2578 | } | |
2579 | ||
bcf84111 | 2580 | /* Returns true if 'rule' has an OpenFlow OFPAT_OUTPUT or OFPAT_ENQUEUE action |
f25d0cf3 | 2581 | * that outputs to 'port' (output to OFPP_FLOOD and OFPP_ALL doesn't count). */ |
74e79b7c | 2582 | static bool |
4e022ec0 | 2583 | ofproto_rule_has_out_port(const struct rule *rule, ofp_port_t port) |
15aaf599 | 2584 | OVS_REQUIRES(ofproto_mutex) |
064af421 | 2585 | { |
7f05e7ab | 2586 | return (port == OFPP_ANY |
6f00e29b BP |
2587 | || ofpacts_output_to_port(rule->actions->ofpacts, |
2588 | rule->actions->ofpacts_len, port)); | |
064af421 BP |
2589 | } |
2590 | ||
7395c052 | 2591 | /* Returns true if 'rule' has group and equals group_id. */ |
74e79b7c | 2592 | static bool |
7395c052 | 2593 | ofproto_rule_has_out_group(const struct rule *rule, uint32_t group_id) |
15aaf599 | 2594 | OVS_REQUIRES(ofproto_mutex) |
7395c052 NZ |
2595 | { |
2596 | return (group_id == OFPG11_ANY | |
6f00e29b BP |
2597 | || ofpacts_output_to_group(rule->actions->ofpacts, |
2598 | rule->actions->ofpacts_len, group_id)); | |
7395c052 NZ |
2599 | } |
2600 | ||
2b07c8b1 BP |
2601 | /* Returns true if a rule related to 'op' has an OpenFlow OFPAT_OUTPUT or |
2602 | * OFPAT_ENQUEUE action that outputs to 'out_port'. */ | |
2603 | bool | |
4e022ec0 | 2604 | ofoperation_has_out_port(const struct ofoperation *op, ofp_port_t out_port) |
15aaf599 | 2605 | OVS_REQUIRES(ofproto_mutex) |
2b07c8b1 BP |
2606 | { |
2607 | if (ofproto_rule_has_out_port(op->rule, out_port)) { | |
2608 | return true; | |
2609 | } | |
2610 | ||
2611 | switch (op->type) { | |
2612 | case OFOPERATION_ADD: | |
2b07c8b1 BP |
2613 | case OFOPERATION_DELETE: |
2614 | return false; | |
2615 | ||
2616 | case OFOPERATION_MODIFY: | |
b277c7cc | 2617 | case OFOPERATION_REPLACE: |
6f00e29b BP |
2618 | return ofpacts_output_to_port(op->actions->ofpacts, |
2619 | op->actions->ofpacts_len, out_port); | |
2b07c8b1 BP |
2620 | } |
2621 | ||
2622 | NOT_REACHED(); | |
2623 | } | |
2624 | ||
35412852 BP |
2625 | static void |
2626 | rule_execute_destroy(struct rule_execute *e) | |
eedc0097 | 2627 | { |
35412852 BP |
2628 | ofproto_rule_unref(e->rule); |
2629 | list_remove(&e->list_node); | |
2630 | free(e); | |
2631 | } | |
eedc0097 | 2632 | |
35412852 BP |
2633 | /* Executes all "rule_execute" operations queued up in ofproto->rule_executes, |
2634 | * by passing them to the ofproto provider. */ | |
2635 | static void | |
2636 | run_rule_executes(struct ofproto *ofproto) | |
15aaf599 | 2637 | OVS_EXCLUDED(ofproto_mutex) |
35412852 BP |
2638 | { |
2639 | struct rule_execute *e, *next; | |
2640 | struct list executes; | |
2641 | ||
2642 | guarded_list_pop_all(&ofproto->rule_executes, &executes); | |
2643 | LIST_FOR_EACH_SAFE (e, next, list_node, &executes) { | |
2644 | union flow_in_port in_port_; | |
2645 | struct flow flow; | |
2646 | ||
2647 | in_port_.ofp_port = e->in_port; | |
2648 | flow_extract(e->packet, 0, 0, NULL, &in_port_, &flow); | |
2649 | ofproto->ofproto_class->rule_execute(e->rule, &flow, e->packet); | |
2650 | ||
2651 | rule_execute_destroy(e); | |
2652 | } | |
2653 | } | |
2654 | ||
2655 | /* Destroys and discards all "rule_execute" operations queued up in | |
2656 | * ofproto->rule_executes. */ | |
2657 | static void | |
2658 | destroy_rule_executes(struct ofproto *ofproto) | |
2659 | { | |
2660 | struct rule_execute *e, *next; | |
2661 | struct list executes; | |
2662 | ||
2663 | guarded_list_pop_all(&ofproto->rule_executes, &executes); | |
2664 | LIST_FOR_EACH_SAFE (e, next, list_node, &executes) { | |
2665 | ofpbuf_delete(e->packet); | |
2666 | rule_execute_destroy(e); | |
2667 | } | |
350a665f BP |
2668 | } |
2669 | ||
abe529af BP |
2670 | /* Returns true if 'rule' should be hidden from the controller. |
2671 | * | |
2672 | * Rules with priority higher than UINT16_MAX are set up by ofproto itself | |
2673 | * (e.g. by in-band control) and are intentionally hidden from the | |
2674 | * controller. */ | |
74e79b7c | 2675 | static bool |
2b07c8b1 | 2676 | ofproto_rule_is_hidden(const struct rule *rule) |
b6c9e612 | 2677 | { |
abe529af | 2678 | return rule->cr.priority > UINT16_MAX; |
7b064a79 | 2679 | } |
5c67e4af BP |
2680 | |
2681 | static enum oftable_flags | |
2682 | rule_get_flags(const struct rule *rule) | |
2683 | { | |
2684 | return rule->ofproto->tables[rule->table_id].flags; | |
2685 | } | |
2686 | ||
2687 | static bool | |
2688 | rule_is_modifiable(const struct rule *rule) | |
2689 | { | |
2690 | return !(rule_get_flags(rule) & OFTABLE_READONLY); | |
2691 | } | |
fa066f01 | 2692 | \f |
90bf1e07 | 2693 | static enum ofperr |
d1e2cf21 | 2694 | handle_echo_request(struct ofconn *ofconn, const struct ofp_header *oh) |
064af421 | 2695 | { |
b0421aa2 | 2696 | ofconn_send_reply(ofconn, make_echo_reply(oh)); |
064af421 | 2697 | return 0; |
064af421 BP |
2698 | } |
2699 | ||
90bf1e07 | 2700 | static enum ofperr |
d1e2cf21 | 2701 | handle_features_request(struct ofconn *ofconn, const struct ofp_header *oh) |
064af421 | 2702 | { |
64ff1d96 | 2703 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
9e1fd49b | 2704 | struct ofputil_switch_features features; |
064af421 | 2705 | struct ofport *port; |
6c1491fb | 2706 | bool arp_match_ip; |
9e1fd49b | 2707 | struct ofpbuf *b; |
c2f0373a BP |
2708 | int n_tables; |
2709 | int i; | |
064af421 | 2710 | |
9e1fd49b BP |
2711 | ofproto->ofproto_class->get_features(ofproto, &arp_match_ip, |
2712 | &features.actions); | |
cb22974d | 2713 | ovs_assert(features.actions & OFPUTIL_A_OUTPUT); /* sanity check */ |
064af421 | 2714 | |
c2f0373a BP |
2715 | /* Count only non-hidden tables in the number of tables. (Hidden tables, |
2716 | * if present, are always at the end.) */ | |
2717 | n_tables = ofproto->n_tables; | |
2718 | for (i = 0; i < ofproto->n_tables; i++) { | |
2719 | if (ofproto->tables[i].flags & OFTABLE_HIDDEN) { | |
2720 | n_tables = i; | |
2721 | break; | |
2722 | } | |
2723 | } | |
2724 | ||
9e1fd49b BP |
2725 | features.datapath_id = ofproto->datapath_id; |
2726 | features.n_buffers = pktbuf_capacity(); | |
c2f0373a | 2727 | features.n_tables = n_tables; |
9e1fd49b BP |
2728 | features.capabilities = (OFPUTIL_C_FLOW_STATS | OFPUTIL_C_TABLE_STATS | |
2729 | OFPUTIL_C_PORT_STATS | OFPUTIL_C_QUEUE_STATS); | |
6c1491fb | 2730 | if (arp_match_ip) { |
9e1fd49b | 2731 | features.capabilities |= OFPUTIL_C_ARP_MATCH_IP; |
6c1491fb | 2732 | } |
2e1ae200 JR |
2733 | /* FIXME: Fill in proper features.auxiliary_id for auxiliary connections */ |
2734 | features.auxiliary_id = 0; | |
9e1fd49b BP |
2735 | b = ofputil_encode_switch_features(&features, ofconn_get_protocol(ofconn), |
2736 | oh->xid); | |
64ff1d96 | 2737 | HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) { |
9e1fd49b | 2738 | ofputil_put_switch_features_port(&port->pp, b); |
064af421 | 2739 | } |
064af421 | 2740 | |
9e1fd49b | 2741 | ofconn_send_reply(ofconn, b); |
064af421 BP |
2742 | return 0; |
2743 | } | |
064af421 | 2744 | |
90bf1e07 | 2745 | static enum ofperr |
d1e2cf21 | 2746 | handle_get_config_request(struct ofconn *ofconn, const struct ofp_header *oh) |
064af421 | 2747 | { |
64ff1d96 | 2748 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
064af421 | 2749 | struct ofp_switch_config *osc; |
f0fd1a17 | 2750 | enum ofp_config_flags flags; |
7257b535 | 2751 | struct ofpbuf *buf; |
959a2ecd | 2752 | |
064af421 | 2753 | /* Send reply. */ |
982697a4 BP |
2754 | buf = ofpraw_alloc_reply(OFPRAW_OFPT_GET_CONFIG_REPLY, oh, 0); |
2755 | osc = ofpbuf_put_uninit(buf, sizeof *osc); | |
f0fd1a17 | 2756 | flags = ofproto->frag_handling; |
2e1ae200 JR |
2757 | /* OFPC_INVALID_TTL_TO_CONTROLLER is deprecated in OF 1.3 */ |
2758 | if (oh->version < OFP13_VERSION | |
2759 | && ofconn_get_invalid_ttl_to_controller(ofconn)) { | |
f0fd1a17 PS |
2760 | flags |= OFPC_INVALID_TTL_TO_CONTROLLER; |
2761 | } | |
2762 | osc->flags = htons(flags); | |
810605a2 | 2763 | osc->miss_send_len = htons(ofconn_get_miss_send_len(ofconn)); |
b0421aa2 | 2764 | ofconn_send_reply(ofconn, buf); |
2d70a31a | 2765 | |
064af421 BP |
2766 | return 0; |
2767 | } | |
064af421 | 2768 | |
90bf1e07 | 2769 | static enum ofperr |
982697a4 | 2770 | handle_set_config(struct ofconn *ofconn, const struct ofp_header *oh) |
064af421 | 2771 | { |
982697a4 | 2772 | const struct ofp_switch_config *osc = ofpmsg_body(oh); |
64ff1d96 | 2773 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
d1e2cf21 | 2774 | uint16_t flags = ntohs(osc->flags); |
2d70a31a | 2775 | |
7257b535 | 2776 | if (ofconn_get_type(ofconn) != OFCONN_PRIMARY |
f4f1ea7e | 2777 | || ofconn_get_role(ofconn) != OFPCR12_ROLE_SLAVE) { |
7257b535 BP |
2778 | enum ofp_config_flags cur = ofproto->frag_handling; |
2779 | enum ofp_config_flags next = flags & OFPC_FRAG_MASK; | |
2780 | ||
cb22974d | 2781 | ovs_assert((cur & OFPC_FRAG_MASK) == cur); |
7257b535 BP |
2782 | if (cur != next) { |
2783 | if (ofproto->ofproto_class->set_frag_handling(ofproto, next)) { | |
2784 | ofproto->frag_handling = next; | |
2785 | } else { | |
2786 | VLOG_WARN_RL(&rl, "%s: unsupported fragment handling mode %s", | |
2787 | ofproto->name, | |
2788 | ofputil_frag_handling_to_string(next)); | |
2789 | } | |
064af421 BP |
2790 | } |
2791 | } | |
2e1ae200 | 2792 | /* OFPC_INVALID_TTL_TO_CONTROLLER is deprecated in OF 1.3 */ |
f0fd1a17 | 2793 | ofconn_set_invalid_ttl_to_controller(ofconn, |
2e1ae200 JR |
2794 | (oh->version < OFP13_VERSION |
2795 | && flags & OFPC_INVALID_TTL_TO_CONTROLLER)); | |
ebe482fd | 2796 | |
810605a2 | 2797 | ofconn_set_miss_send_len(ofconn, ntohs(osc->miss_send_len)); |
0ad9b732 | 2798 | |
064af421 | 2799 | return 0; |
064af421 BP |
2800 | } |
2801 | ||
9deba63b | 2802 | /* Checks whether 'ofconn' is a slave controller. If so, returns an OpenFlow |
90bf1e07 BP |
2803 | * error message code for the caller to propagate upward. Otherwise, returns |
2804 | * 0. | |
2805 | * | |
2806 | * The log message mentions 'msg_type'. */ | |
2807 | static enum ofperr | |
2808 | reject_slave_controller(struct ofconn *ofconn) | |
9deba63b | 2809 | { |
1ce0a5fa | 2810 | if (ofconn_get_type(ofconn) == OFCONN_PRIMARY |
f4f1ea7e | 2811 | && ofconn_get_role(ofconn) == OFPCR12_ROLE_SLAVE) { |
90bf1e07 | 2812 | return OFPERR_OFPBRC_EPERM; |
9deba63b BP |
2813 | } else { |
2814 | return 0; | |
2815 | } | |
2816 | } | |
2817 | ||
e3b56933 JR |
2818 | /* Checks that the 'ofpacts_len' bytes of actions in 'ofpacts' are appropriate |
2819 | * for a packet with the prerequisites satisfied by 'flow' in table 'table_id'. | |
9cae45dc JR |
2820 | * 'flow' may be temporarily modified, but is restored at return. |
2821 | */ | |
2822 | static enum ofperr | |
2823 | ofproto_check_ofpacts(struct ofproto *ofproto, | |
2824 | const struct ofpact ofpacts[], size_t ofpacts_len, | |
e3b56933 | 2825 | struct flow *flow, uint8_t table_id) |
9cae45dc JR |
2826 | { |
2827 | enum ofperr error; | |
2828 | uint32_t mid; | |
2829 | ||
430dbb14 AW |
2830 | error = ofpacts_check(ofpacts, ofpacts_len, flow, |
2831 | u16_to_ofp(ofproto->max_ports), table_id); | |
9cae45dc JR |
2832 | if (error) { |
2833 | return error; | |
2834 | } | |
2835 | ||
d6fb622d | 2836 | mid = ofpacts_get_meter(ofpacts, ofpacts_len); |
65efd2ab | 2837 | if (mid && get_provider_meter_id(ofproto, mid) == UINT32_MAX) { |
9cae45dc JR |
2838 | return OFPERR_OFPMMFC_INVALID_METER; |
2839 | } | |
2840 | return 0; | |
2841 | } | |
2842 | ||
90bf1e07 | 2843 | static enum ofperr |
982697a4 | 2844 | handle_packet_out(struct ofconn *ofconn, const struct ofp_header *oh) |
064af421 | 2845 | { |
64ff1d96 | 2846 | struct ofproto *p = ofconn_get_ofproto(ofconn); |
c6a93eb7 BP |
2847 | struct ofputil_packet_out po; |
2848 | struct ofpbuf *payload; | |
f25d0cf3 BP |
2849 | uint64_t ofpacts_stub[1024 / 8]; |
2850 | struct ofpbuf ofpacts; | |
ae412e7d | 2851 | struct flow flow; |
4e022ec0 | 2852 | union flow_in_port in_port_; |
90bf1e07 | 2853 | enum ofperr error; |
064af421 | 2854 | |
ac51afaf BP |
2855 | COVERAGE_INC(ofproto_packet_out); |
2856 | ||
76589937 | 2857 | error = reject_slave_controller(ofconn); |
9deba63b | 2858 | if (error) { |
f25d0cf3 | 2859 | goto exit; |
9deba63b BP |
2860 | } |
2861 | ||
c6a93eb7 | 2862 | /* Decode message. */ |
f25d0cf3 | 2863 | ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub); |
982697a4 | 2864 | error = ofputil_decode_packet_out(&po, oh, &ofpacts); |
064af421 | 2865 | if (error) { |
f25d0cf3 | 2866 | goto exit_free_ofpacts; |
064af421 | 2867 | } |
430dbb14 | 2868 | if (ofp_to_u16(po.in_port) >= p->max_ports |
4e022ec0 | 2869 | && ofp_to_u16(po.in_port) < ofp_to_u16(OFPP_MAX)) { |
2e1bfcb6 | 2870 | error = OFPERR_OFPBRC_BAD_PORT; |
91858960 BP |
2871 | goto exit_free_ofpacts; |
2872 | } | |
064af421 | 2873 | |
5cb7a798 | 2874 | |
ac51afaf | 2875 | /* Get payload. */ |
c6a93eb7 BP |
2876 | if (po.buffer_id != UINT32_MAX) { |
2877 | error = ofconn_pktbuf_retrieve(ofconn, po.buffer_id, &payload, NULL); | |
2878 | if (error || !payload) { | |
f25d0cf3 | 2879 | goto exit_free_ofpacts; |
064af421 | 2880 | } |
064af421 | 2881 | } else { |
bb622f82 BP |
2882 | /* Ensure that the L3 header is 32-bit aligned. */ |
2883 | payload = ofpbuf_clone_data_with_headroom(po.packet, po.packet_len, 2); | |
e1154f71 BP |
2884 | } |
2885 | ||
548de4dd | 2886 | /* Verify actions against packet, then send packet if successful. */ |
4e022ec0 AW |
2887 | in_port_.ofp_port = po.in_port; |
2888 | flow_extract(payload, 0, 0, NULL, &in_port_, &flow); | |
e3b56933 | 2889 | error = ofproto_check_ofpacts(p, po.ofpacts, po.ofpacts_len, &flow, 0); |
548de4dd BP |
2890 | if (!error) { |
2891 | error = p->ofproto_class->packet_out(p, payload, &flow, | |
2892 | po.ofpacts, po.ofpacts_len); | |
2893 | } | |
c6a93eb7 | 2894 | ofpbuf_delete(payload); |
abe529af | 2895 | |
f25d0cf3 BP |
2896 | exit_free_ofpacts: |
2897 | ofpbuf_uninit(&ofpacts); | |
2898 | exit: | |
abe529af | 2899 | return error; |
064af421 BP |
2900 | } |
2901 | ||
2902 | static void | |
9e1fd49b BP |
2903 | update_port_config(struct ofport *port, |
2904 | enum ofputil_port_config config, | |
2905 | enum ofputil_port_config mask) | |
064af421 | 2906 | { |
9e1fd49b BP |
2907 | enum ofputil_port_config old_config = port->pp.config; |
2908 | enum ofputil_port_config toggle; | |
abe529af | 2909 | |
9e1fd49b BP |
2910 | toggle = (config ^ port->pp.config) & mask; |
2911 | if (toggle & OFPUTIL_PC_PORT_DOWN) { | |
2912 | if (config & OFPUTIL_PC_PORT_DOWN) { | |
4b609110 | 2913 | netdev_turn_flags_off(port->netdev, NETDEV_UP, NULL); |
064af421 | 2914 | } else { |
4b609110 | 2915 | netdev_turn_flags_on(port->netdev, NETDEV_UP, NULL); |
064af421 | 2916 | } |
9e1fd49b | 2917 | toggle &= ~OFPUTIL_PC_PORT_DOWN; |
064af421 | 2918 | } |
abe529af | 2919 | |
9e1fd49b BP |
2920 | port->pp.config ^= toggle; |
2921 | if (port->pp.config != old_config) { | |
abe529af | 2922 | port->ofproto->ofproto_class->port_reconfigured(port, old_config); |
064af421 BP |
2923 | } |
2924 | } | |
2925 | ||
90bf1e07 | 2926 | static enum ofperr |
d1e2cf21 | 2927 | handle_port_mod(struct ofconn *ofconn, const struct ofp_header *oh) |
064af421 | 2928 | { |
64ff1d96 | 2929 | struct ofproto *p = ofconn_get_ofproto(ofconn); |
9e1fd49b | 2930 | struct ofputil_port_mod pm; |
064af421 | 2931 | struct ofport *port; |
9e1fd49b | 2932 | enum ofperr error; |
064af421 | 2933 | |
76589937 | 2934 | error = reject_slave_controller(ofconn); |
9deba63b BP |
2935 | if (error) { |
2936 | return error; | |
2937 | } | |
064af421 | 2938 | |
9e1fd49b BP |
2939 | error = ofputil_decode_port_mod(oh, &pm); |
2940 | if (error) { | |
2941 | return error; | |
2942 | } | |
2943 | ||
2944 | port = ofproto_get_port(p, pm.port_no); | |
064af421 | 2945 | if (!port) { |
90bf1e07 | 2946 | return OFPERR_OFPPMFC_BAD_PORT; |
9e1fd49b | 2947 | } else if (!eth_addr_equals(port->pp.hw_addr, pm.hw_addr)) { |
90bf1e07 | 2948 | return OFPERR_OFPPMFC_BAD_HW_ADDR; |
064af421 | 2949 | } else { |
9e1fd49b BP |
2950 | update_port_config(port, pm.config, pm.mask); |
2951 | if (pm.advertise) { | |
2952 | netdev_set_advertisements(port->netdev, pm.advertise); | |
064af421 BP |
2953 | } |
2954 | } | |
2955 | return 0; | |
2956 | } | |
2957 | ||
90bf1e07 | 2958 | static enum ofperr |
3269c562 | 2959 | handle_desc_stats_request(struct ofconn *ofconn, |
982697a4 | 2960 | const struct ofp_header *request) |
064af421 | 2961 | { |
061bfea4 BP |
2962 | static const char *default_mfr_desc = "Nicira, Inc."; |
2963 | static const char *default_hw_desc = "Open vSwitch"; | |
2964 | static const char *default_sw_desc = VERSION; | |
2965 | static const char *default_serial_desc = "None"; | |
2966 | static const char *default_dp_desc = "None"; | |
2967 | ||
64ff1d96 | 2968 | struct ofproto *p = ofconn_get_ofproto(ofconn); |
064af421 BP |
2969 | struct ofp_desc_stats *ods; |
2970 | struct ofpbuf *msg; | |
2971 | ||
982697a4 BP |
2972 | msg = ofpraw_alloc_stats_reply(request, 0); |
2973 | ods = ofpbuf_put_zeros(msg, sizeof *ods); | |
061bfea4 BP |
2974 | ovs_strlcpy(ods->mfr_desc, p->mfr_desc ? p->mfr_desc : default_mfr_desc, |
2975 | sizeof ods->mfr_desc); | |
2976 | ovs_strlcpy(ods->hw_desc, p->hw_desc ? p->hw_desc : default_hw_desc, | |
2977 | sizeof ods->hw_desc); | |
2978 | ovs_strlcpy(ods->sw_desc, p->sw_desc ? p->sw_desc : default_sw_desc, | |
2979 | sizeof ods->sw_desc); | |
2980 | ovs_strlcpy(ods->serial_num, | |
2981 | p->serial_desc ? p->serial_desc : default_serial_desc, | |
2982 | sizeof ods->serial_num); | |
2983 | ovs_strlcpy(ods->dp_desc, p->dp_desc ? p->dp_desc : default_dp_desc, | |
2984 | sizeof ods->dp_desc); | |
b0421aa2 | 2985 | ofconn_send_reply(ofconn, msg); |
064af421 BP |
2986 | |
2987 | return 0; | |
2988 | } | |
2989 | ||
90bf1e07 | 2990 | static enum ofperr |
3269c562 | 2991 | handle_table_stats_request(struct ofconn *ofconn, |
982697a4 | 2992 | const struct ofp_header *request) |
064af421 | 2993 | { |
64ff1d96 | 2994 | struct ofproto *p = ofconn_get_ofproto(ofconn); |
307975da | 2995 | struct ofp12_table_stats *ots; |
064af421 | 2996 | struct ofpbuf *msg; |
c2f0373a | 2997 | int n_tables; |
6c1491fb | 2998 | size_t i; |
064af421 | 2999 | |
307975da SH |
3000 | /* Set up default values. |
3001 | * | |
3002 | * ofp12_table_stats is used as a generic structure as | |
3003 | * it is able to hold all the fields for ofp10_table_stats | |
3004 | * and ofp11_table_stats (and of course itself). | |
3005 | */ | |
3006 | ots = xcalloc(p->n_tables, sizeof *ots); | |
6c1491fb BP |
3007 | for (i = 0; i < p->n_tables; i++) { |
3008 | ots[i].table_id = i; | |
35aa7a21 | 3009 | sprintf(ots[i].name, "table%zu", i); |
6240624b BP |
3010 | ots[i].match = htonll(OFPXMT13_MASK); |
3011 | ots[i].wildcards = htonll(OFPXMT13_MASK); | |
e78b61f6 BP |
3012 | ots[i].write_actions = htonl(OFPAT11_OUTPUT); |
3013 | ots[i].apply_actions = htonl(OFPAT11_OUTPUT); | |
6240624b BP |
3014 | ots[i].write_setfields = htonll(OFPXMT13_MASK); |
3015 | ots[i].apply_setfields = htonll(OFPXMT13_MASK); | |
b8266395 BP |
3016 | ots[i].metadata_match = OVS_BE64_MAX; |
3017 | ots[i].metadata_write = OVS_BE64_MAX; | |
307975da SH |
3018 | ots[i].instructions = htonl(OFPIT11_ALL); |
3019 | ots[i].config = htonl(OFPTC11_TABLE_MISS_MASK); | |
6c1491fb | 3020 | ots[i].max_entries = htonl(1000000); /* An arbitrary big number. */ |
0b4f2078 | 3021 | ovs_rwlock_rdlock(&p->tables[i].cls.rwlock); |
d0918789 | 3022 | ots[i].active_count = htonl(classifier_count(&p->tables[i].cls)); |
0b4f2078 | 3023 | ovs_rwlock_unlock(&p->tables[i].cls.rwlock); |
6c1491fb | 3024 | } |
064af421 | 3025 | |
6c1491fb | 3026 | p->ofproto_class->get_tables(p, ots); |
064af421 | 3027 | |
c2f0373a BP |
3028 | /* Post-process the tables, dropping hidden tables. */ |
3029 | n_tables = p->n_tables; | |
254750ce BP |
3030 | for (i = 0; i < p->n_tables; i++) { |
3031 | const struct oftable *table = &p->tables[i]; | |
3032 | ||
c2f0373a BP |
3033 | if (table->flags & OFTABLE_HIDDEN) { |
3034 | n_tables = i; | |
3035 | break; | |
3036 | } | |
3037 | ||
254750ce BP |
3038 | if (table->name) { |
3039 | ovs_strzcpy(ots[i].name, table->name, sizeof ots[i].name); | |
3040 | } | |
3041 | ||
3042 | if (table->max_flows < ntohl(ots[i].max_entries)) { | |
3043 | ots[i].max_entries = htonl(table->max_flows); | |
3044 | } | |
3045 | } | |
3046 | ||
c2f0373a | 3047 | msg = ofputil_encode_table_stats_reply(ots, n_tables, request); |
b0421aa2 | 3048 | ofconn_send_reply(ofconn, msg); |
307975da SH |
3049 | |
3050 | free(ots); | |
3051 | ||
064af421 BP |
3052 | return 0; |
3053 | } | |
3054 | ||
abaad8cf | 3055 | static void |
63f2140a | 3056 | append_port_stat(struct ofport *port, struct list *replies) |
abaad8cf | 3057 | { |
f8e4867e | 3058 | struct ofputil_port_stats ops = { .port_no = port->pp.port_no }; |
abaad8cf | 3059 | |
65e0be10 BP |
3060 | calc_duration(port->created, time_msec(), |
3061 | &ops.duration_sec, &ops.duration_nsec); | |
3062 | ||
d295e8e9 JP |
3063 | /* Intentionally ignore return value, since errors will set |
3064 | * 'stats' to all-1s, which is correct for OpenFlow, and | |
abaad8cf | 3065 | * netdev_get_stats() will log errors. */ |
f8e4867e SH |
3066 | ofproto_port_get_stats(port, &ops.stats); |
3067 | ||
3068 | ofputil_append_port_stat(replies, &ops); | |
abaad8cf JP |
3069 | } |
3070 | ||
90bf1e07 | 3071 | static enum ofperr |
63f2140a | 3072 | handle_port_stats_request(struct ofconn *ofconn, |
982697a4 | 3073 | const struct ofp_header *request) |
064af421 | 3074 | { |
64ff1d96 | 3075 | struct ofproto *p = ofconn_get_ofproto(ofconn); |
064af421 | 3076 | struct ofport *port; |
63f2140a | 3077 | struct list replies; |
4e022ec0 | 3078 | ofp_port_t port_no; |
f8e4867e SH |
3079 | enum ofperr error; |
3080 | ||
3081 | error = ofputil_decode_port_stats_request(request, &port_no); | |
3082 | if (error) { | |
3083 | return error; | |
3084 | } | |
064af421 | 3085 | |
982697a4 | 3086 | ofpmp_init(&replies, request); |
7f05e7ab | 3087 | if (port_no != OFPP_ANY) { |
f8e4867e | 3088 | port = ofproto_get_port(p, port_no); |
abaad8cf | 3089 | if (port) { |
63f2140a | 3090 | append_port_stat(port, &replies); |
abaad8cf JP |
3091 | } |
3092 | } else { | |
4e8e4213 | 3093 | HMAP_FOR_EACH (port, hmap_node, &p->ports) { |
63f2140a | 3094 | append_port_stat(port, &replies); |
abaad8cf | 3095 | } |
064af421 BP |
3096 | } |
3097 | ||
63f2140a | 3098 | ofconn_send_replies(ofconn, &replies); |
064af421 BP |
3099 | return 0; |
3100 | } | |
3101 | ||
2be393ed JP |
3102 | static enum ofperr |
3103 | handle_port_desc_stats_request(struct ofconn *ofconn, | |
982697a4 | 3104 | const struct ofp_header *request) |
2be393ed JP |
3105 | { |
3106 | struct ofproto *p = ofconn_get_ofproto(ofconn); | |
2e3fa633 | 3107 | enum ofp_version version; |
2be393ed JP |
3108 | struct ofport *port; |
3109 | struct list replies; | |
3110 | ||
982697a4 | 3111 | ofpmp_init(&replies, request); |
2be393ed | 3112 | |
2e3fa633 | 3113 | version = ofputil_protocol_to_ofp_version(ofconn_get_protocol(ofconn)); |
2be393ed | 3114 | HMAP_FOR_EACH (port, hmap_node, &p->ports) { |
2e3fa633 | 3115 | ofputil_append_port_desc_stats_reply(version, &port->pp, &replies); |
2be393ed JP |
3116 | } |
3117 | ||
3118 | ofconn_send_replies(ofconn, &replies); | |
3119 | return 0; | |
3120 | } | |
3121 | ||
98eaac36 JR |
3122 | static uint32_t |
3123 | hash_cookie(ovs_be64 cookie) | |
3124 | { | |
3125 | return hash_2words((OVS_FORCE uint64_t)cookie >> 32, | |
3126 | (OVS_FORCE uint64_t)cookie); | |
3127 | } | |
3128 | ||
3129 | static void | |
3130 | cookies_insert(struct ofproto *ofproto, struct rule *rule) | |
2c916028 | 3131 | OVS_REQUIRES(ofproto_mutex) |
98eaac36 JR |
3132 | { |
3133 | hindex_insert(&ofproto->cookies, &rule->cookie_node, | |
3134 | hash_cookie(rule->flow_cookie)); | |
3135 | } | |
3136 | ||
3137 | static void | |
3138 | cookies_remove(struct ofproto *ofproto, struct rule *rule) | |
2c916028 | 3139 | OVS_REQUIRES(ofproto_mutex) |
98eaac36 JR |
3140 | { |
3141 | hindex_remove(&ofproto->cookies, &rule->cookie_node); | |
3142 | } | |
3143 | ||
3144 | static void | |
3145 | ofproto_rule_change_cookie(struct ofproto *ofproto, struct rule *rule, | |
3146 | ovs_be64 new_cookie) | |
15aaf599 | 3147 | OVS_REQUIRES(ofproto_mutex) |
98eaac36 JR |
3148 | { |
3149 | if (new_cookie != rule->flow_cookie) { | |
3150 | cookies_remove(ofproto, rule); | |
3151 | ||
c7c9a7c8 | 3152 | ovs_mutex_lock(&rule->mutex); |
98eaac36 | 3153 | rule->flow_cookie = new_cookie; |
c7c9a7c8 | 3154 | ovs_mutex_unlock(&rule->mutex); |
98eaac36 JR |
3155 | |
3156 | cookies_insert(ofproto, rule); | |
3157 | } | |
3158 | } | |
3159 | ||
c6ebb8fb | 3160 | static void |
65e0be10 BP |
3161 | calc_duration(long long int start, long long int now, |
3162 | uint32_t *sec, uint32_t *nsec) | |
c6ebb8fb | 3163 | { |
f27f2134 | 3164 | long long int msecs = now - start; |
588cd7b5 BP |
3165 | *sec = msecs / 1000; |
3166 | *nsec = (msecs % 1000) * (1000 * 1000); | |
3167 | } | |
3168 | ||
48266274 BP |
3169 | /* Checks whether 'table_id' is 0xff or a valid table ID in 'ofproto'. Returns |
3170 | * 0 if 'table_id' is OK, otherwise an OpenFlow error code. */ | |
90bf1e07 | 3171 | static enum ofperr |
48266274 BP |
3172 | check_table_id(const struct ofproto *ofproto, uint8_t table_id) |
3173 | { | |
3174 | return (table_id == 0xff || table_id < ofproto->n_tables | |
3175 | ? 0 | |
c22c56bd | 3176 | : OFPERR_OFPBRC_BAD_TABLE_ID); |
48266274 BP |
3177 | |
3178 | } | |
3179 | ||
5c67e4af | 3180 | static struct oftable * |
d4ce8a49 | 3181 | next_visible_table(const struct ofproto *ofproto, uint8_t table_id) |
5c67e4af BP |
3182 | { |
3183 | struct oftable *table; | |
3184 | ||
3185 | for (table = &ofproto->tables[table_id]; | |
3186 | table < &ofproto->tables[ofproto->n_tables]; | |
3187 | table++) { | |
3188 | if (!(table->flags & OFTABLE_HIDDEN)) { | |
3189 | return table; | |
3190 | } | |
3191 | } | |
3192 | ||
3193 | return NULL; | |
3194 | } | |
3195 | ||
d0918789 | 3196 | static struct oftable * |
d4ce8a49 | 3197 | first_matching_table(const struct ofproto *ofproto, uint8_t table_id) |
064af421 | 3198 | { |
6c1491fb | 3199 | if (table_id == 0xff) { |
5c67e4af | 3200 | return next_visible_table(ofproto, 0); |
6c1491fb BP |
3201 | } else if (table_id < ofproto->n_tables) { |
3202 | return &ofproto->tables[table_id]; | |
a02e5331 | 3203 | } else { |
6c1491fb | 3204 | return NULL; |
a02e5331 | 3205 | } |
064af421 BP |
3206 | } |
3207 | ||
d0918789 | 3208 | static struct oftable * |
d4ce8a49 BP |
3209 | next_matching_table(const struct ofproto *ofproto, |
3210 | const struct oftable *table, uint8_t table_id) | |
6c1491fb | 3211 | { |
5c67e4af BP |
3212 | return (table_id == 0xff |
3213 | ? next_visible_table(ofproto, (table - ofproto->tables) + 1) | |
6c1491fb BP |
3214 | : NULL); |
3215 | } | |
3216 | ||
d0918789 | 3217 | /* Assigns TABLE to each oftable, in turn, that matches TABLE_ID in OFPROTO: |
6c1491fb BP |
3218 | * |
3219 | * - If TABLE_ID is 0xff, this iterates over every classifier table in | |
5c67e4af | 3220 | * OFPROTO, skipping tables marked OFTABLE_HIDDEN. |
6c1491fb BP |
3221 | * |
3222 | * - If TABLE_ID is the number of a table in OFPROTO, then the loop iterates | |
5c67e4af BP |
3223 | * only once, for that table. (This can be used to access tables marked |
3224 | * OFTABLE_HIDDEN.) | |
6c1491fb | 3225 | * |
48266274 BP |
3226 | * - Otherwise, TABLE_ID isn't valid for OFPROTO, so the loop won't be |
3227 | * entered at all. (Perhaps you should have validated TABLE_ID with | |
3228 | * check_table_id().) | |
6c1491fb BP |
3229 | * |
3230 | * All parameters are evaluated multiple times. | |
3231 | */ | |
d0918789 BP |
3232 | #define FOR_EACH_MATCHING_TABLE(TABLE, TABLE_ID, OFPROTO) \ |
3233 | for ((TABLE) = first_matching_table(OFPROTO, TABLE_ID); \ | |
3234 | (TABLE) != NULL; \ | |
3235 | (TABLE) = next_matching_table(OFPROTO, TABLE, TABLE_ID)) | |
6c1491fb | 3236 | |
a9b22b7f BP |
3237 | /* Initializes 'criteria' in a straightforward way based on the other |
3238 | * parameters. | |
3239 | * | |
3240 | * For "loose" matching, the 'priority' parameter is unimportant and may be | |
3241 | * supplied as 0. */ | |
3242 | static void | |
3243 | rule_criteria_init(struct rule_criteria *criteria, uint8_t table_id, | |
3244 | const struct match *match, unsigned int priority, | |
3245 | ovs_be64 cookie, ovs_be64 cookie_mask, | |
3246 | ofp_port_t out_port, uint32_t out_group) | |
3247 | { | |
3248 | criteria->table_id = table_id; | |
3249 | cls_rule_init(&criteria->cr, match, priority); | |
3250 | criteria->cookie = cookie; | |
3251 | criteria->cookie_mask = cookie_mask; | |
3252 | criteria->out_port = out_port; | |
3253 | criteria->out_group = out_group; | |
3254 | } | |
3255 | ||
3256 | static void | |
3257 | rule_criteria_destroy(struct rule_criteria *criteria) | |
3258 | { | |
3259 | cls_rule_destroy(&criteria->cr); | |
3260 | } | |
3261 | ||
a8e547c1 BP |
3262 | void |
3263 | rule_collection_init(struct rule_collection *rules) | |
3264 | { | |
3265 | rules->rules = rules->stub; | |
3266 | rules->n = 0; | |
3267 | rules->capacity = ARRAY_SIZE(rules->stub); | |
3268 | } | |
3269 | ||
3270 | void | |
3271 | rule_collection_add(struct rule_collection *rules, struct rule *rule) | |
3272 | { | |
3273 | if (rules->n >= rules->capacity) { | |
3274 | size_t old_size, new_size; | |
3275 | ||
3276 | old_size = rules->capacity * sizeof *rules->rules; | |
3277 | rules->capacity *= 2; | |
3278 | new_size = rules->capacity * sizeof *rules->rules; | |
3279 | ||
3280 | if (rules->rules == rules->stub) { | |
3281 | rules->rules = xmalloc(new_size); | |
3282 | memcpy(rules->rules, rules->stub, old_size); | |
3283 | } else { | |
3284 | rules->rules = xrealloc(rules->rules, new_size); | |
3285 | } | |
3286 | } | |
3287 | ||
3288 | rules->rules[rules->n++] = rule; | |
3289 | } | |
3290 | ||
15aaf599 BP |
3291 | void |
3292 | rule_collection_ref(struct rule_collection *rules) | |
3293 | OVS_REQUIRES(ofproto_mutex) | |
3294 | { | |
3295 | size_t i; | |
3296 | ||
3297 | for (i = 0; i < rules->n; i++) { | |
3298 | ofproto_rule_ref(rules->rules[i]); | |
3299 | } | |
3300 | } | |
3301 | ||
3302 | void | |
3303 | rule_collection_unref(struct rule_collection *rules) | |
3304 | { | |
3305 | size_t i; | |
3306 | ||
3307 | for (i = 0; i < rules->n; i++) { | |
3308 | ofproto_rule_unref(rules->rules[i]); | |
3309 | } | |
3310 | } | |
3311 | ||
a8e547c1 BP |
3312 | void |
3313 | rule_collection_destroy(struct rule_collection *rules) | |
3314 | { | |
3315 | if (rules->rules != rules->stub) { | |
3316 | free(rules->rules); | |
3317 | } | |
3318 | } | |
3319 | ||
6c1d7642 | 3320 | static enum ofperr |
a9b22b7f | 3321 | collect_rule(struct rule *rule, const struct rule_criteria *c, |
a8e547c1 | 3322 | struct rule_collection *rules) |
15aaf599 | 3323 | OVS_REQUIRES(ofproto_mutex) |
6c1d7642 | 3324 | { |
3e8116d9 BP |
3325 | /* We ordinarily want to skip hidden rules, but there has to be a way for |
3326 | * code internal to OVS to modify and delete them, so if the criteria | |
3327 | * specify a priority that can only be for a hidden flow, then allow hidden | |
3328 | * rules to be selected. (This doesn't allow OpenFlow clients to meddle | |
3329 | * with hidden flows because OpenFlow uses only a 16-bit field to specify | |
3330 | * priority.) */ | |
3331 | if (ofproto_rule_is_hidden(rule) && c->cr.priority <= UINT16_MAX) { | |
6c1d7642 BP |
3332 | return 0; |
3333 | } else if (rule->pending) { | |
3334 | return OFPROTO_POSTPONE; | |
3335 | } else { | |
a9b22b7f BP |
3336 | if ((c->table_id == rule->table_id || c->table_id == 0xff) |
3337 | && ofproto_rule_has_out_port(rule, c->out_port) | |
3338 | && ofproto_rule_has_out_group(rule, c->out_group) | |
3339 | && !((rule->flow_cookie ^ c->cookie) & c->cookie_mask)) { | |
a8e547c1 | 3340 | rule_collection_add(rules, rule); |
6c1d7642 BP |
3341 | } |
3342 | return 0; | |
3343 | } | |
3344 | } | |
3345 | ||
a9b22b7f BP |
3346 | /* Searches 'ofproto' for rules that match the criteria in 'criteria'. Matches |
3347 | * on classifiers rules are done in the "loose" way required for OpenFlow | |
3348 | * OFPFC_MODIFY and OFPFC_DELETE requests. Puts the selected rules on list | |
9ed18e46 BP |
3349 | * 'rules'. |
3350 | * | |
9ed18e46 BP |
3351 | * Hidden rules are always omitted. |
3352 | * | |
3353 | * Returns 0 on success, otherwise an OpenFlow error code. */ | |
90bf1e07 | 3354 | static enum ofperr |
a9b22b7f | 3355 | collect_rules_loose(struct ofproto *ofproto, |
a8e547c1 BP |
3356 | const struct rule_criteria *criteria, |
3357 | struct rule_collection *rules) | |
15aaf599 | 3358 | OVS_REQUIRES(ofproto_mutex) |
9ed18e46 | 3359 | { |
d0918789 | 3360 | struct oftable *table; |
90bf1e07 | 3361 | enum ofperr error; |
48266274 | 3362 | |
a8e547c1 BP |
3363 | rule_collection_init(rules); |
3364 | ||
a9b22b7f | 3365 | error = check_table_id(ofproto, criteria->table_id); |
48266274 | 3366 | if (error) { |
a8e547c1 | 3367 | goto exit; |
48266274 | 3368 | } |
9ed18e46 | 3369 | |
b8266395 | 3370 | if (criteria->cookie_mask == OVS_BE64_MAX) { |
98eaac36 JR |
3371 | struct rule *rule; |
3372 | ||
a9b22b7f BP |
3373 | HINDEX_FOR_EACH_WITH_HASH (rule, cookie_node, |
3374 | hash_cookie(criteria->cookie), | |
98eaac36 | 3375 | &ofproto->cookies) { |
a9b22b7f BP |
3376 | if (cls_rule_is_loose_match(&rule->cr, &criteria->cr.match)) { |
3377 | error = collect_rule(rule, criteria, rules); | |
6c1d7642 BP |
3378 | if (error) { |
3379 | break; | |
98eaac36 JR |
3380 | } |
3381 | } | |
3382 | } | |
6c1d7642 | 3383 | } else { |
a9b22b7f | 3384 | FOR_EACH_MATCHING_TABLE (table, criteria->table_id, ofproto) { |
6c1d7642 BP |
3385 | struct cls_cursor cursor; |
3386 | struct rule *rule; | |
9ed18e46 | 3387 | |
6c1d7642 | 3388 | ovs_rwlock_rdlock(&table->cls.rwlock); |
a9b22b7f | 3389 | cls_cursor_init(&cursor, &table->cls, &criteria->cr); |
6c1d7642 | 3390 | CLS_CURSOR_FOR_EACH (rule, cr, &cursor) { |
a9b22b7f | 3391 | error = collect_rule(rule, criteria, rules); |
6c1d7642 BP |
3392 | if (error) { |
3393 | break; | |
3394 | } | |
9ed18e46 | 3395 | } |
6c1d7642 | 3396 | ovs_rwlock_unlock(&table->cls.rwlock); |
9ed18e46 BP |
3397 | } |
3398 | } | |
48d28ac1 | 3399 | |
a8e547c1 BP |
3400 | exit: |
3401 | if (error) { | |
3402 | rule_collection_destroy(rules); | |
3403 | } | |
48d28ac1 | 3404 | return error; |
9ed18e46 BP |
3405 | } |
3406 | ||
a9b22b7f BP |
3407 | /* Searches 'ofproto' for rules that match the criteria in 'criteria'. Matches |
3408 | * on classifiers rules are done in the "strict" way required for OpenFlow | |
3409 | * OFPFC_MODIFY_STRICT and OFPFC_DELETE_STRICT requests. Puts the selected | |
3410 | * rules on list 'rules'. | |
9ed18e46 BP |
3411 | * |
3412 | * Hidden rules are always omitted. | |
3413 | * | |
3414 | * Returns 0 on success, otherwise an OpenFlow error code. */ | |
90bf1e07 | 3415 | static enum ofperr |
a9b22b7f | 3416 | collect_rules_strict(struct ofproto *ofproto, |
a8e547c1 BP |
3417 | const struct rule_criteria *criteria, |
3418 | struct rule_collection *rules) | |
15aaf599 | 3419 | OVS_REQUIRES(ofproto_mutex) |
9ed18e46 | 3420 | { |
d0918789 | 3421 | struct oftable *table; |
48266274 BP |
3422 | int error; |
3423 | ||
a8e547c1 BP |
3424 | rule_collection_init(rules); |
3425 | ||
a9b22b7f | 3426 | error = check_table_id(ofproto, criteria->table_id); |
48266274 | 3427 | if (error) { |
a8e547c1 | 3428 | goto exit; |
48266274 | 3429 | } |
9ed18e46 | 3430 | |
b8266395 | 3431 | if (criteria->cookie_mask == OVS_BE64_MAX) { |
98eaac36 JR |
3432 | struct rule *rule; |
3433 | ||
a9b22b7f BP |
3434 | HINDEX_FOR_EACH_WITH_HASH (rule, cookie_node, |
3435 | hash_cookie(criteria->cookie), | |
98eaac36 | 3436 | &ofproto->cookies) { |
a9b22b7f BP |
3437 | if (cls_rule_equal(&rule->cr, &criteria->cr)) { |
3438 | error = collect_rule(rule, criteria, rules); | |
6c1d7642 BP |
3439 | if (error) { |
3440 | break; | |
98eaac36 JR |
3441 | } |
3442 | } | |
3443 | } | |
6c1d7642 | 3444 | } else { |
a9b22b7f | 3445 | FOR_EACH_MATCHING_TABLE (table, criteria->table_id, ofproto) { |
6c1d7642 | 3446 | struct rule *rule; |
9ed18e46 | 3447 | |
6c1d7642 | 3448 | ovs_rwlock_rdlock(&table->cls.rwlock); |
a9b22b7f BP |
3449 | rule = rule_from_cls_rule(classifier_find_rule_exactly( |
3450 | &table->cls, &criteria->cr)); | |
6c1d7642 BP |
3451 | ovs_rwlock_unlock(&table->cls.rwlock); |
3452 | if (rule) { | |
a9b22b7f | 3453 | error = collect_rule(rule, criteria, rules); |
6c1d7642 BP |
3454 | if (error) { |
3455 | break; | |
3456 | } | |
9ed18e46 BP |
3457 | } |
3458 | } | |
3459 | } | |
48d28ac1 | 3460 | |
a8e547c1 BP |
3461 | exit: |
3462 | if (error) { | |
3463 | rule_collection_destroy(rules); | |
3464 | } | |
6c1d7642 | 3465 | return error; |
9ed18e46 BP |
3466 | } |
3467 | ||
f27f2134 BP |
3468 | /* Returns 'age_ms' (a duration in milliseconds), converted to seconds and |
3469 | * forced into the range of a uint16_t. */ | |
3470 | static int | |
3471 | age_secs(long long int age_ms) | |
3472 | { | |
3473 | return (age_ms < 0 ? 0 | |
3474 | : age_ms >= UINT16_MAX * 1000 ? UINT16_MAX | |
3475 | : (unsigned int) age_ms / 1000); | |
3476 | } | |
3477 | ||
90bf1e07 | 3478 | static enum ofperr |
63f2140a | 3479 | handle_flow_stats_request(struct ofconn *ofconn, |
982697a4 | 3480 | const struct ofp_header *request) |
15aaf599 | 3481 | OVS_EXCLUDED(ofproto_mutex) |
064af421 | 3482 | { |
64ff1d96 | 3483 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
81d1ea94 | 3484 | struct ofputil_flow_stats_request fsr; |
a9b22b7f | 3485 | struct rule_criteria criteria; |
a8e547c1 | 3486 | struct rule_collection rules; |
63f2140a | 3487 | struct list replies; |
90bf1e07 | 3488 | enum ofperr error; |
a8e547c1 | 3489 | size_t i; |
09246b99 | 3490 | |
982697a4 | 3491 | error = ofputil_decode_flow_stats_request(&fsr, request); |
09246b99 BP |
3492 | if (error) { |
3493 | return error; | |
3494 | } | |
3495 | ||
a9b22b7f BP |
3496 | rule_criteria_init(&criteria, fsr.table_id, &fsr.match, 0, fsr.cookie, |
3497 | fsr.cookie_mask, fsr.out_port, fsr.out_group); | |
15aaf599 BP |
3498 | |
3499 | ovs_mutex_lock(&ofproto_mutex); | |
a9b22b7f BP |
3500 | error = collect_rules_loose(ofproto, &criteria, &rules); |
3501 | rule_criteria_destroy(&criteria); | |
15aaf599 BP |
3502 | if (!error) { |
3503 | rule_collection_ref(&rules); | |
3504 | } | |
3505 | ovs_mutex_unlock(&ofproto_mutex); | |
3506 | ||
9ed18e46 BP |
3507 | if (error) { |
3508 | return error; | |
3509 | } | |
5ecc9d81 | 3510 | |
982697a4 | 3511 | ofpmp_init(&replies, request); |
a8e547c1 BP |
3512 | for (i = 0; i < rules.n; i++) { |
3513 | struct rule *rule = rules.rules[i]; | |
f27f2134 | 3514 | long long int now = time_msec(); |
9ed18e46 | 3515 | struct ofputil_flow_stats fs; |
15aaf599 BP |
3516 | long long int created, used, modified; |
3517 | struct rule_actions *actions; | |
3518 | enum ofputil_flow_mod_flags flags; | |
a3779dbc | 3519 | |
d10976b7 | 3520 | ovs_mutex_lock(&rule->mutex); |
15aaf599 | 3521 | fs.cookie = rule->flow_cookie; |
a3779dbc EJ |
3522 | fs.idle_timeout = rule->idle_timeout; |
3523 | fs.hard_timeout = rule->hard_timeout; | |
15aaf599 BP |
3524 | created = rule->created; |
3525 | used = rule->used; | |
3526 | modified = rule->modified; | |
3527 | actions = rule_get_actions__(rule); | |
3528 | flags = rule->flags; | |
d10976b7 | 3529 | ovs_mutex_unlock(&rule->mutex); |
a3779dbc | 3530 | |
15aaf599 BP |
3531 | minimatch_expand(&rule->cr.match, &fs.match); |
3532 | fs.table_id = rule->table_id; | |
3533 | calc_duration(created, now, &fs.duration_sec, &fs.duration_nsec); | |
3534 | fs.priority = rule->cr.priority; | |
3535 | fs.idle_age = age_secs(now - used); | |
3536 | fs.hard_age = age_secs(now - modified); | |
3537 | ofproto->ofproto_class->rule_get_stats(rule, &fs.packet_count, | |
3538 | &fs.byte_count); | |
3539 | fs.ofpacts = actions->ofpacts; | |
3540 | fs.ofpacts_len = actions->ofpacts_len; | |
3f517bcd | 3541 | |
15aaf599 | 3542 | fs.flags = flags; |
9ed18e46 | 3543 | ofputil_append_flow_stats_reply(&fs, &replies); |
15aaf599 BP |
3544 | |
3545 | rule_actions_unref(actions); | |
3c4486a5 | 3546 | } |
15aaf599 BP |
3547 | |
3548 | rule_collection_unref(&rules); | |
a8e547c1 BP |
3549 | rule_collection_destroy(&rules); |
3550 | ||
63f2140a | 3551 | ofconn_send_replies(ofconn, &replies); |
5ecc9d81 | 3552 | |
09246b99 BP |
3553 | return 0; |
3554 | } | |
3555 | ||
4f2cad2c | 3556 | static void |
3394b5b6 | 3557 | flow_stats_ds(struct rule *rule, struct ds *results) |
4f2cad2c | 3558 | { |
4f2cad2c | 3559 | uint64_t packet_count, byte_count; |
15aaf599 BP |
3560 | struct rule_actions *actions; |
3561 | long long int created; | |
4f2cad2c | 3562 | |
abe529af BP |
3563 | rule->ofproto->ofproto_class->rule_get_stats(rule, |
3564 | &packet_count, &byte_count); | |
4f2cad2c | 3565 | |
15aaf599 BP |
3566 | ovs_mutex_lock(&rule->mutex); |
3567 | actions = rule_get_actions__(rule); | |
3568 | created = rule->created; | |
3569 | ovs_mutex_unlock(&rule->mutex); | |
3570 | ||
6c1491fb BP |
3571 | if (rule->table_id != 0) { |
3572 | ds_put_format(results, "table_id=%"PRIu8", ", rule->table_id); | |
3573 | } | |
15aaf599 | 3574 | ds_put_format(results, "duration=%llds, ", (time_msec() - created) / 1000); |
4f2cad2c JP |
3575 | ds_put_format(results, "n_packets=%"PRIu64", ", packet_count); |
3576 | ds_put_format(results, "n_bytes=%"PRIu64", ", byte_count); | |
cb833cf6 | 3577 | cls_rule_format(&rule->cr, results); |
a5df0e72 | 3578 | ds_put_char(results, ','); |
15aaf599 | 3579 | |
455ecd77 | 3580 | ds_put_cstr(results, "actions="); |
15aaf599 BP |
3581 | ofpacts_format(actions->ofpacts, actions->ofpacts_len, results); |
3582 | ||
4f2cad2c | 3583 | ds_put_cstr(results, "\n"); |
15aaf599 BP |
3584 | |
3585 | rule_actions_unref(actions); | |
4f2cad2c JP |
3586 | } |
3587 | ||
d295e8e9 | 3588 | /* Adds a pretty-printed description of all flows to 'results', including |
ee8b231c | 3589 | * hidden flows (e.g., set up by in-band control). */ |
4f2cad2c JP |
3590 | void |
3591 | ofproto_get_all_flows(struct ofproto *p, struct ds *results) | |
3592 | { | |
d0918789 | 3593 | struct oftable *table; |
6c1491fb | 3594 | |
d0918789 | 3595 | OFPROTO_FOR_EACH_TABLE (table, p) { |
6c1491fb BP |
3596 | struct cls_cursor cursor; |
3597 | struct rule *rule; | |
064af421 | 3598 | |
0b4f2078 | 3599 | ovs_rwlock_rdlock(&table->cls.rwlock); |
d0918789 | 3600 | cls_cursor_init(&cursor, &table->cls, NULL); |
6c1491fb BP |
3601 | CLS_CURSOR_FOR_EACH (rule, cr, &cursor) { |
3602 | flow_stats_ds(rule, results); | |
3603 | } | |
0b4f2078 | 3604 | ovs_rwlock_unlock(&table->cls.rwlock); |
064af421 | 3605 | } |
064af421 BP |
3606 | } |
3607 | ||
b5827b24 BP |
3608 | /* Obtains the NetFlow engine type and engine ID for 'ofproto' into |
3609 | * '*engine_type' and '*engine_id', respectively. */ | |
3610 | void | |
3611 | ofproto_get_netflow_ids(const struct ofproto *ofproto, | |
3612 | uint8_t *engine_type, uint8_t *engine_id) | |
3613 | { | |
abe529af | 3614 | ofproto->ofproto_class->get_netflow_ids(ofproto, engine_type, engine_id); |
b5827b24 BP |
3615 | } |
3616 | ||
9a9e3786 BP |
3617 | /* Checks the status of CFM configured on 'ofp_port' within 'ofproto'. Returns |
3618 | * true if the port's CFM status was successfully stored into '*status'. | |
3619 | * Returns false if the port did not have CFM configured, in which case | |
3620 | * '*status' is indeterminate. | |
3621 | * | |
13b1b2ae | 3622 | * The caller must provide and owns '*status', and must free 'status->rmps'. */ |
9a9e3786 | 3623 | bool |
4e022ec0 | 3624 | ofproto_port_get_cfm_status(const struct ofproto *ofproto, ofp_port_t ofp_port, |
9a9e3786 | 3625 | struct ofproto_cfm_status *status) |
3967a833 MM |
3626 | { |
3627 | struct ofport *ofport = ofproto_get_port(ofproto, ofp_port); | |
9a9e3786 BP |
3628 | return (ofport |
3629 | && ofproto->ofproto_class->get_cfm_status | |
3630 | && ofproto->ofproto_class->get_cfm_status(ofport, status)); | |
3967a833 MM |
3631 | } |
3632 | ||
90bf1e07 | 3633 | static enum ofperr |
76c93b22 | 3634 | handle_aggregate_stats_request(struct ofconn *ofconn, |
982697a4 | 3635 | const struct ofp_header *oh) |
15aaf599 | 3636 | OVS_EXCLUDED(ofproto_mutex) |
27d34fce | 3637 | { |
76c93b22 | 3638 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
81d1ea94 | 3639 | struct ofputil_flow_stats_request request; |
76c93b22 | 3640 | struct ofputil_aggregate_stats stats; |
5e9d0469 | 3641 | bool unknown_packets, unknown_bytes; |
a9b22b7f | 3642 | struct rule_criteria criteria; |
a8e547c1 | 3643 | struct rule_collection rules; |
76c93b22 | 3644 | struct ofpbuf *reply; |
90bf1e07 | 3645 | enum ofperr error; |
a8e547c1 | 3646 | size_t i; |
27d34fce | 3647 | |
982697a4 | 3648 | error = ofputil_decode_flow_stats_request(&request, oh); |
76c93b22 BP |
3649 | if (error) { |
3650 | return error; | |
3651 | } | |
5ecc9d81 | 3652 | |
a9b22b7f BP |
3653 | rule_criteria_init(&criteria, request.table_id, &request.match, 0, |
3654 | request.cookie, request.cookie_mask, | |
3655 | request.out_port, request.out_group); | |
15aaf599 BP |
3656 | |
3657 | ovs_mutex_lock(&ofproto_mutex); | |
a9b22b7f BP |
3658 | error = collect_rules_loose(ofproto, &criteria, &rules); |
3659 | rule_criteria_destroy(&criteria); | |
15aaf599 BP |
3660 | if (!error) { |
3661 | rule_collection_ref(&rules); | |
3662 | } | |
3663 | ovs_mutex_unlock(&ofproto_mutex); | |
3664 | ||
9ed18e46 BP |
3665 | if (error) { |
3666 | return error; | |
3667 | } | |
3c4486a5 | 3668 | |
9ed18e46 | 3669 | memset(&stats, 0, sizeof stats); |
5e9d0469 | 3670 | unknown_packets = unknown_bytes = false; |
a8e547c1 BP |
3671 | for (i = 0; i < rules.n; i++) { |
3672 | struct rule *rule = rules.rules[i]; | |
9ed18e46 BP |
3673 | uint64_t packet_count; |
3674 | uint64_t byte_count; | |
5ecc9d81 | 3675 | |
9ed18e46 BP |
3676 | ofproto->ofproto_class->rule_get_stats(rule, &packet_count, |
3677 | &byte_count); | |
5ecc9d81 | 3678 | |
5e9d0469 BP |
3679 | if (packet_count == UINT64_MAX) { |
3680 | unknown_packets = true; | |
3681 | } else { | |
3682 | stats.packet_count += packet_count; | |
3683 | } | |
3684 | ||
3685 | if (byte_count == UINT64_MAX) { | |
3686 | unknown_bytes = true; | |
3687 | } else { | |
3688 | stats.byte_count += byte_count; | |
3689 | } | |
3690 | ||
9ed18e46 | 3691 | stats.flow_count++; |
3c4486a5 | 3692 | } |
5e9d0469 BP |
3693 | if (unknown_packets) { |
3694 | stats.packet_count = UINT64_MAX; | |
3695 | } | |
3696 | if (unknown_bytes) { | |
3697 | stats.byte_count = UINT64_MAX; | |
3698 | } | |
27d34fce | 3699 | |
15aaf599 | 3700 | rule_collection_unref(&rules); |
a8e547c1 BP |
3701 | rule_collection_destroy(&rules); |
3702 | ||
982697a4 | 3703 | reply = ofputil_encode_aggregate_stats_reply(&stats, oh); |
76c93b22 | 3704 | ofconn_send_reply(ofconn, reply); |
09246b99 BP |
3705 | |
3706 | return 0; | |
3707 | } | |
3708 | ||
c1c9c9c4 | 3709 | struct queue_stats_cbdata { |
ca0f572c | 3710 | struct ofport *ofport; |
63f2140a | 3711 | struct list replies; |
6dc34a0d | 3712 | long long int now; |
c1c9c9c4 BP |
3713 | }; |
3714 | ||
3715 | static void | |
db9220c3 | 3716 | put_queue_stats(struct queue_stats_cbdata *cbdata, uint32_t queue_id, |
c1c9c9c4 BP |
3717 | const struct netdev_queue_stats *stats) |
3718 | { | |
6dc34a0d | 3719 | struct ofputil_queue_stats oqs; |
c1c9c9c4 | 3720 | |
6dc34a0d BP |
3721 | oqs.port_no = cbdata->ofport->pp.port_no; |
3722 | oqs.queue_id = queue_id; | |
3723 | oqs.tx_bytes = stats->tx_bytes; | |
3724 | oqs.tx_packets = stats->tx_packets; | |
3725 | oqs.tx_errors = stats->tx_errors; | |
3726 | if (stats->created != LLONG_MIN) { | |
3727 | calc_duration(stats->created, cbdata->now, | |
3728 | &oqs.duration_sec, &oqs.duration_nsec); | |
3729 | } else { | |
3730 | oqs.duration_sec = oqs.duration_nsec = UINT32_MAX; | |
3731 | } | |
64626975 | 3732 | ofputil_append_queue_stat(&cbdata->replies, &oqs); |
c1c9c9c4 BP |
3733 | } |
3734 | ||
3735 | static void | |
db9220c3 | 3736 | handle_queue_stats_dump_cb(uint32_t queue_id, |
c1c9c9c4 BP |
3737 | struct netdev_queue_stats *stats, |
3738 | void *cbdata_) | |
3739 | { | |
3740 | struct queue_stats_cbdata *cbdata = cbdata_; | |
3741 | ||
3742 | put_queue_stats(cbdata, queue_id, stats); | |
3743 | } | |
3744 | ||
0414d158 | 3745 | static enum ofperr |
ca0f572c | 3746 | handle_queue_stats_for_port(struct ofport *port, uint32_t queue_id, |
c1c9c9c4 BP |
3747 | struct queue_stats_cbdata *cbdata) |
3748 | { | |
ca0f572c | 3749 | cbdata->ofport = port; |
c1c9c9c4 BP |
3750 | if (queue_id == OFPQ_ALL) { |
3751 | netdev_dump_queue_stats(port->netdev, | |
3752 | handle_queue_stats_dump_cb, cbdata); | |
3753 | } else { | |
3754 | struct netdev_queue_stats stats; | |
3755 | ||
1ac788f6 BP |
3756 | if (!netdev_get_queue_stats(port->netdev, queue_id, &stats)) { |
3757 | put_queue_stats(cbdata, queue_id, &stats); | |
0414d158 BP |
3758 | } else { |
3759 | return OFPERR_OFPQOFC_BAD_QUEUE; | |
1ac788f6 | 3760 | } |
c1c9c9c4 | 3761 | } |
0414d158 | 3762 | return 0; |
c1c9c9c4 BP |
3763 | } |
3764 | ||
90bf1e07 | 3765 | static enum ofperr |
63f2140a | 3766 | handle_queue_stats_request(struct ofconn *ofconn, |
982697a4 | 3767 | const struct ofp_header *rq) |
c1c9c9c4 | 3768 | { |
64ff1d96 | 3769 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
c1c9c9c4 | 3770 | struct queue_stats_cbdata cbdata; |
0414d158 | 3771 | struct ofport *port; |
0414d158 | 3772 | enum ofperr error; |
64626975 | 3773 | struct ofputil_queue_stats_request oqsr; |
c1c9c9c4 | 3774 | |
c1c9c9c4 BP |
3775 | COVERAGE_INC(ofproto_queue_req); |
3776 | ||
982697a4 | 3777 | ofpmp_init(&cbdata.replies, rq); |
6dc34a0d | 3778 | cbdata.now = time_msec(); |
c1c9c9c4 | 3779 | |
64626975 SH |
3780 | error = ofputil_decode_queue_stats_request(rq, &oqsr); |
3781 | if (error) { | |
3782 | return error; | |
3783 | } | |
3784 | ||
7f05e7ab | 3785 | if (oqsr.port_no == OFPP_ANY) { |
0414d158 | 3786 | error = OFPERR_OFPQOFC_BAD_QUEUE; |
4e8e4213 | 3787 | HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) { |
64626975 | 3788 | if (!handle_queue_stats_for_port(port, oqsr.queue_id, &cbdata)) { |
0414d158 BP |
3789 | error = 0; |
3790 | } | |
c1c9c9c4 | 3791 | } |
0414d158 | 3792 | } else { |
64626975 | 3793 | port = ofproto_get_port(ofproto, oqsr.port_no); |
0414d158 | 3794 | error = (port |
64626975 | 3795 | ? handle_queue_stats_for_port(port, oqsr.queue_id, &cbdata) |
0414d158 BP |
3796 | : OFPERR_OFPQOFC_BAD_PORT); |
3797 | } | |
3798 | if (!error) { | |
3799 | ofconn_send_replies(ofconn, &cbdata.replies); | |
c1c9c9c4 | 3800 | } else { |
63f2140a | 3801 | ofpbuf_list_delete(&cbdata.replies); |
c1c9c9c4 | 3802 | } |
c1c9c9c4 | 3803 | |
0414d158 | 3804 | return error; |
c1c9c9c4 | 3805 | } |
7ee20df1 BP |
3806 | |
3807 | static bool | |
3808 | is_flow_deletion_pending(const struct ofproto *ofproto, | |
3809 | const struct cls_rule *cls_rule, | |
3810 | uint8_t table_id) | |
15aaf599 | 3811 | OVS_REQUIRES(ofproto_mutex) |
7ee20df1 BP |
3812 | { |
3813 | if (!hmap_is_empty(&ofproto->deletions)) { | |
3814 | struct ofoperation *op; | |
3815 | ||
3816 | HMAP_FOR_EACH_WITH_HASH (op, hmap_node, | |
3817 | cls_rule_hash(cls_rule, table_id), | |
3818 | &ofproto->deletions) { | |
3819 | if (cls_rule_equal(cls_rule, &op->rule->cr)) { | |
3820 | return true; | |
3821 | } | |
3822 | } | |
3823 | } | |
3824 | ||
3825 | return false; | |
3826 | } | |
3827 | ||
2e31a9b8 BP |
3828 | static bool |
3829 | should_evict_a_rule(struct oftable *table, unsigned int extra_space) | |
15aaf599 BP |
3830 | OVS_REQUIRES(ofproto_mutex) |
3831 | OVS_NO_THREAD_SAFETY_ANALYSIS | |
8037acb4 | 3832 | { |
15aaf599 | 3833 | return classifier_count(&table->cls) + extra_space > table->max_flows; |
2e31a9b8 | 3834 | } |
8037acb4 | 3835 | |
2e31a9b8 BP |
3836 | static enum ofperr |
3837 | evict_rules_from_table(struct ofproto *ofproto, struct oftable *table, | |
3838 | unsigned int extra_space) | |
15aaf599 | 3839 | OVS_REQUIRES(ofproto_mutex) |
2e31a9b8 BP |
3840 | { |
3841 | while (should_evict_a_rule(table, extra_space)) { | |
3842 | struct rule *rule; | |
8037acb4 | 3843 | |
2e31a9b8 BP |
3844 | if (!choose_rule_to_evict(table, &rule)) { |
3845 | return OFPERR_OFPFMFC_TABLE_FULL; | |
3846 | } else if (rule->pending) { | |
2e31a9b8 BP |
3847 | return OFPROTO_POSTPONE; |
3848 | } else { | |
3849 | struct ofopgroup *group = ofopgroup_create_unattached(ofproto); | |
15aaf599 BP |
3850 | delete_flow__(rule, group, OFPRR_EVICTION); |
3851 | ofopgroup_submit(group); | |
2e31a9b8 | 3852 | } |
8037acb4 | 3853 | } |
2e31a9b8 BP |
3854 | |
3855 | return 0; | |
8037acb4 BP |
3856 | } |
3857 | ||
79eee1eb BP |
3858 | /* Implements OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT |
3859 | * in which no matching flow already exists in the flow table. | |
3860 | * | |
3861 | * Adds the flow specified by 'ofm', which is followed by 'n_actions' | |
75a75043 | 3862 | * ofp_actions, to the ofproto's flow table. Returns 0 on success, an OpenFlow |
90bf1e07 BP |
3863 | * error code on failure, or OFPROTO_POSTPONE if the operation cannot be |
3864 | * initiated now but may be retried later. | |
79eee1eb | 3865 | * |
2509616e | 3866 | * The caller retains ownership of 'fm->ofpacts'. |
f25d0cf3 | 3867 | * |
79eee1eb BP |
3868 | * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id, |
3869 | * if any. */ | |
90bf1e07 | 3870 | static enum ofperr |
a9a2da38 | 3871 | add_flow(struct ofproto *ofproto, struct ofconn *ofconn, |
e3b56933 | 3872 | struct ofputil_flow_mod *fm, const struct ofp_header *request) |
15aaf599 | 3873 | OVS_REQUIRES(ofproto_mutex) |
064af421 | 3874 | { |
d0918789 | 3875 | struct oftable *table; |
7ee20df1 | 3876 | struct ofopgroup *group; |
8037acb4 | 3877 | struct cls_rule cr; |
064af421 | 3878 | struct rule *rule; |
5a361239 | 3879 | uint8_t table_id; |
064af421 BP |
3880 | int error; |
3881 | ||
48266274 BP |
3882 | error = check_table_id(ofproto, fm->table_id); |
3883 | if (error) { | |
3884 | return error; | |
3885 | } | |
3886 | ||
7ee20df1 BP |
3887 | /* Pick table. */ |
3888 | if (fm->table_id == 0xff) { | |
13521ff5 | 3889 | if (ofproto->ofproto_class->rule_choose_table) { |
81a76618 BP |
3890 | error = ofproto->ofproto_class->rule_choose_table(ofproto, |
3891 | &fm->match, | |
7ee20df1 BP |
3892 | &table_id); |
3893 | if (error) { | |
3894 | return error; | |
3895 | } | |
cb22974d | 3896 | ovs_assert(table_id < ofproto->n_tables); |
7ee20df1 | 3897 | } else { |
5a361239 | 3898 | table_id = 0; |
7ee20df1 BP |
3899 | } |
3900 | } else if (fm->table_id < ofproto->n_tables) { | |
5a361239 | 3901 | table_id = fm->table_id; |
7ee20df1 | 3902 | } else { |
c22c56bd | 3903 | return OFPERR_OFPBRC_BAD_TABLE_ID; |
064af421 BP |
3904 | } |
3905 | ||
5a361239 JR |
3906 | table = &ofproto->tables[table_id]; |
3907 | ||
5c67e4af BP |
3908 | if (table->flags & OFTABLE_READONLY) { |
3909 | return OFPERR_OFPBRC_EPERM; | |
3910 | } | |
3911 | ||
8037acb4 BP |
3912 | cls_rule_init(&cr, &fm->match, fm->priority); |
3913 | ||
b277c7cc BP |
3914 | /* Transform "add" into "modify" if there's an existing identical flow. */ |
3915 | ovs_rwlock_rdlock(&table->cls.rwlock); | |
8037acb4 | 3916 | rule = rule_from_cls_rule(classifier_find_rule_exactly(&table->cls, &cr)); |
b277c7cc BP |
3917 | ovs_rwlock_unlock(&table->cls.rwlock); |
3918 | if (rule) { | |
8037acb4 | 3919 | cls_rule_destroy(&cr); |
b277c7cc BP |
3920 | if (!rule_is_modifiable(rule)) { |
3921 | return OFPERR_OFPBRC_EPERM; | |
3922 | } else if (rule->pending) { | |
3923 | return OFPROTO_POSTPONE; | |
3924 | } else { | |
a8e547c1 | 3925 | struct rule_collection rules; |
b277c7cc | 3926 | |
a8e547c1 BP |
3927 | rule_collection_init(&rules); |
3928 | rule_collection_add(&rules, rule); | |
b277c7cc | 3929 | fm->modify_cookie = true; |
a8e547c1 BP |
3930 | error = modify_flows__(ofproto, ofconn, fm, request, &rules); |
3931 | rule_collection_destroy(&rules); | |
3932 | ||
3933 | return error; | |
b277c7cc BP |
3934 | } |
3935 | } | |
3936 | ||
e3b56933 JR |
3937 | /* Verify actions. */ |
3938 | error = ofproto_check_ofpacts(ofproto, fm->ofpacts, fm->ofpacts_len, | |
3939 | &fm->match.flow, table_id); | |
3940 | if (error) { | |
3ef7ad56 | 3941 | cls_rule_destroy(&cr); |
e3b56933 JR |
3942 | return error; |
3943 | } | |
3944 | ||
7ee20df1 | 3945 | /* Serialize against pending deletion. */ |
8037acb4 BP |
3946 | if (is_flow_deletion_pending(ofproto, &cr, table_id)) { |
3947 | cls_rule_destroy(&cr); | |
7ee20df1 | 3948 | return OFPROTO_POSTPONE; |
afe75089 | 3949 | } |
064af421 | 3950 | |
81a76618 | 3951 | /* Check for overlap, if requested. */ |
0fb88c18 | 3952 | if (fm->flags & OFPUTIL_FF_CHECK_OVERLAP) { |
c09f755d BP |
3953 | bool overlaps; |
3954 | ||
3955 | ovs_rwlock_rdlock(&table->cls.rwlock); | |
8037acb4 | 3956 | overlaps = classifier_rule_overlaps(&table->cls, &cr); |
c09f755d BP |
3957 | ovs_rwlock_unlock(&table->cls.rwlock); |
3958 | ||
3959 | if (overlaps) { | |
8037acb4 | 3960 | cls_rule_destroy(&cr); |
c09f755d BP |
3961 | return OFPERR_OFPFMFC_OVERLAP; |
3962 | } | |
7ee20df1 | 3963 | } |
81a76618 | 3964 | |
8037acb4 | 3965 | /* If necessary, evict an existing rule to clear out space. */ |
2e31a9b8 | 3966 | error = evict_rules_from_table(ofproto, table, 1); |
8037acb4 BP |
3967 | if (error) { |
3968 | cls_rule_destroy(&cr); | |
3969 | return error; | |
3970 | } | |
2e1ae200 | 3971 | |
8037acb4 BP |
3972 | /* Allocate new rule. */ |
3973 | rule = ofproto->ofproto_class->rule_alloc(); | |
3974 | if (!rule) { | |
3975 | cls_rule_destroy(&cr); | |
3976 | VLOG_WARN_RL(&rl, "%s: failed to create rule (%s)", | |
3977 | ofproto->name, ovs_strerror(error)); | |
3978 | return ENOMEM; | |
3979 | } | |
3980 | ||
3981 | /* Initialize base state. */ | |
49a0e0eb BP |
3982 | *CONST_CAST(struct ofproto **, &rule->ofproto) = ofproto; |
3983 | cls_rule_move(CONST_CAST(struct cls_rule *, &rule->cr), &cr); | |
a2143702 | 3984 | atomic_init(&rule->ref_count, 1); |
7ee20df1 | 3985 | rule->pending = NULL; |
623e1caf | 3986 | rule->flow_cookie = fm->new_cookie; |
1745cd08 | 3987 | rule->created = rule->modified = rule->used = time_msec(); |
a3779dbc | 3988 | |
d10976b7 BP |
3989 | ovs_mutex_init(&rule->mutex); |
3990 | ovs_mutex_lock(&rule->mutex); | |
7ee20df1 BP |
3991 | rule->idle_timeout = fm->idle_timeout; |
3992 | rule->hard_timeout = fm->hard_timeout; | |
d10976b7 | 3993 | ovs_mutex_unlock(&rule->mutex); |
a3779dbc | 3994 | |
49a0e0eb | 3995 | *CONST_CAST(uint8_t *, &rule->table_id) = table - ofproto->tables; |
3f517bcd | 3996 | rule->flags = fm->flags & OFPUTIL_FF_STATE; |
65efd2ab | 3997 | rule->actions = rule_actions_create(ofproto, fm->ofpacts, fm->ofpacts_len); |
9cae45dc | 3998 | list_init(&rule->meter_list_node); |
254750ce | 3999 | rule->eviction_group = NULL; |
e503cc19 | 4000 | list_init(&rule->expirable); |
2b07c8b1 BP |
4001 | rule->monitor_flags = 0; |
4002 | rule->add_seqno = 0; | |
4003 | rule->modify_seqno = 0; | |
7ee20df1 | 4004 | |
8037acb4 | 4005 | /* Construct rule, initializing derived state. */ |
b277c7cc BP |
4006 | error = ofproto->ofproto_class->rule_construct(rule); |
4007 | if (error) { | |
8037acb4 BP |
4008 | ofproto_rule_destroy__(rule); |
4009 | return error; | |
064af421 | 4010 | } |
8037acb4 BP |
4011 | |
4012 | /* Insert rule. */ | |
4013 | oftable_insert_rule(rule); | |
4014 | ||
4015 | group = ofopgroup_create(ofproto, ofconn, request, fm->buffer_id); | |
4016 | ofoperation_create(group, rule, OFOPERATION_ADD, 0); | |
4017 | ofproto->ofproto_class->rule_insert(rule); | |
b277c7cc | 4018 | ofopgroup_submit(group); |
79eee1eb | 4019 | |
7ee20df1 | 4020 | return error; |
064af421 | 4021 | } |
79eee1eb BP |
4022 | \f |
4023 | /* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */ | |
064af421 | 4024 | |
9ed18e46 BP |
4025 | /* Modifies the rules listed in 'rules', changing their actions to match those |
4026 | * in 'fm'. | |
79eee1eb | 4027 | * |
9ed18e46 BP |
4028 | * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id, |
4029 | * if any. | |
4030 | * | |
4031 | * Returns 0 on success, otherwise an OpenFlow error code. */ | |
90bf1e07 | 4032 | static enum ofperr |
7024dffe | 4033 | modify_flows__(struct ofproto *ofproto, struct ofconn *ofconn, |
e3b56933 | 4034 | struct ofputil_flow_mod *fm, const struct ofp_header *request, |
a8e547c1 | 4035 | const struct rule_collection *rules) |
15aaf599 | 4036 | OVS_REQUIRES(ofproto_mutex) |
79eee1eb | 4037 | { |
b277c7cc | 4038 | enum ofoperation_type type; |
7ee20df1 | 4039 | struct ofopgroup *group; |
5c67e4af | 4040 | enum ofperr error; |
a8e547c1 | 4041 | size_t i; |
79eee1eb | 4042 | |
b277c7cc | 4043 | type = fm->command == OFPFC_ADD ? OFOPERATION_REPLACE : OFOPERATION_MODIFY; |
7024dffe | 4044 | group = ofopgroup_create(ofproto, ofconn, request, fm->buffer_id); |
5c67e4af | 4045 | error = OFPERR_OFPBRC_EPERM; |
a8e547c1 BP |
4046 | for (i = 0; i < rules->n; i++) { |
4047 | struct rule *rule = rules->rules[i]; | |
08043761 BP |
4048 | struct ofoperation *op; |
4049 | bool actions_changed; | |
b277c7cc | 4050 | bool reset_counters; |
08043761 | 4051 | |
0fb88c18 | 4052 | /* FIXME: Implement OFPFUTIL_FF_RESET_COUNTS */ |
2e1ae200 | 4053 | |
5c67e4af BP |
4054 | if (rule_is_modifiable(rule)) { |
4055 | /* At least one rule is modifiable, don't report EPERM error. */ | |
4056 | error = 0; | |
4057 | } else { | |
4058 | continue; | |
4059 | } | |
4060 | ||
e3b56933 JR |
4061 | /* Verify actions. */ |
4062 | error = ofpacts_check(fm->ofpacts, fm->ofpacts_len, &fm->match.flow, | |
430dbb14 | 4063 | u16_to_ofp(ofproto->max_ports), rule->table_id); |
e3b56933 JR |
4064 | if (error) { |
4065 | return error; | |
4066 | } | |
4067 | ||
08043761 | 4068 | actions_changed = !ofpacts_equal(fm->ofpacts, fm->ofpacts_len, |
6f00e29b BP |
4069 | rule->actions->ofpacts, |
4070 | rule->actions->ofpacts_len); | |
a7d94793 | 4071 | |
b277c7cc | 4072 | op = ofoperation_create(group, rule, type, 0); |
98eaac36 | 4073 | |
b8266395 | 4074 | if (fm->modify_cookie && fm->new_cookie != OVS_BE64_MAX) { |
98eaac36 JR |
4075 | ofproto_rule_change_cookie(ofproto, rule, fm->new_cookie); |
4076 | } | |
b277c7cc | 4077 | if (type == OFOPERATION_REPLACE) { |
d10976b7 | 4078 | ovs_mutex_lock(&rule->mutex); |
b277c7cc BP |
4079 | rule->idle_timeout = fm->idle_timeout; |
4080 | rule->hard_timeout = fm->hard_timeout; | |
d10976b7 | 4081 | ovs_mutex_unlock(&rule->mutex); |
b277c7cc | 4082 | |
3f517bcd | 4083 | rule->flags = fm->flags & OFPUTIL_FF_STATE; |
b277c7cc BP |
4084 | if (fm->idle_timeout || fm->hard_timeout) { |
4085 | if (!rule->eviction_group) { | |
4086 | eviction_group_add_rule(rule); | |
4087 | } | |
4088 | } else { | |
4089 | eviction_group_remove_rule(rule); | |
4090 | } | |
4091 | } | |
4092 | ||
4093 | reset_counters = (fm->flags & OFPUTIL_FF_RESET_COUNTS) != 0; | |
4094 | if (actions_changed || reset_counters) { | |
6f00e29b BP |
4095 | struct rule_actions *new_actions; |
4096 | ||
4097 | op->actions = rule->actions; | |
65efd2ab JR |
4098 | new_actions = rule_actions_create(ofproto, |
4099 | fm->ofpacts, fm->ofpacts_len); | |
884e1dc4 | 4100 | |
c7c9a7c8 | 4101 | ovs_mutex_lock(&rule->mutex); |
6f00e29b | 4102 | rule->actions = new_actions; |
c7c9a7c8 | 4103 | ovs_mutex_unlock(&rule->mutex); |
884e1dc4 | 4104 | |
b277c7cc BP |
4105 | rule->ofproto->ofproto_class->rule_modify_actions(rule, |
4106 | reset_counters); | |
308881af | 4107 | } else { |
08043761 | 4108 | ofoperation_complete(op, 0); |
623e1caf | 4109 | } |
5ecc9d81 | 4110 | } |
7ee20df1 | 4111 | ofopgroup_submit(group); |
79eee1eb | 4112 | |
5c67e4af | 4113 | return error; |
9ed18e46 BP |
4114 | } |
4115 | ||
9387b970 SH |
4116 | static enum ofperr |
4117 | modify_flows_add(struct ofproto *ofproto, struct ofconn *ofconn, | |
e3b56933 | 4118 | struct ofputil_flow_mod *fm, const struct ofp_header *request) |
15aaf599 | 4119 | OVS_REQUIRES(ofproto_mutex) |
9387b970 | 4120 | { |
b8266395 | 4121 | if (fm->cookie_mask != htonll(0) || fm->new_cookie == OVS_BE64_MAX) { |
9387b970 SH |
4122 | return 0; |
4123 | } | |
4124 | return add_flow(ofproto, ofconn, fm, request); | |
4125 | } | |
4126 | ||
90bf1e07 BP |
4127 | /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code on |
4128 | * failure. | |
9ed18e46 BP |
4129 | * |
4130 | * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id, | |
4131 | * if any. */ | |
90bf1e07 | 4132 | static enum ofperr |
7024dffe | 4133 | modify_flows_loose(struct ofproto *ofproto, struct ofconn *ofconn, |
e3b56933 | 4134 | struct ofputil_flow_mod *fm, |
7ee20df1 | 4135 | const struct ofp_header *request) |
15aaf599 | 4136 | OVS_REQUIRES(ofproto_mutex) |
9ed18e46 | 4137 | { |
a9b22b7f | 4138 | struct rule_criteria criteria; |
a8e547c1 | 4139 | struct rule_collection rules; |
9ed18e46 BP |
4140 | int error; |
4141 | ||
a9b22b7f BP |
4142 | rule_criteria_init(&criteria, fm->table_id, &fm->match, 0, |
4143 | fm->cookie, fm->cookie_mask, OFPP_ANY, OFPG11_ANY); | |
4144 | error = collect_rules_loose(ofproto, &criteria, &rules); | |
4145 | rule_criteria_destroy(&criteria); | |
4146 | ||
a8e547c1 BP |
4147 | if (!error) { |
4148 | error = (rules.n > 0 | |
4149 | ? modify_flows__(ofproto, ofconn, fm, request, &rules) | |
4150 | : modify_flows_add(ofproto, ofconn, fm, request)); | |
623e1caf | 4151 | } |
a8e547c1 BP |
4152 | |
4153 | rule_collection_destroy(&rules); | |
4154 | ||
4155 | return error; | |
79eee1eb BP |
4156 | } |
4157 | ||
4158 | /* Implements OFPFC_MODIFY_STRICT. Returns 0 on success or an OpenFlow error | |
90bf1e07 | 4159 | * code on failure. |
79eee1eb | 4160 | * |
9ed18e46 | 4161 | * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id, |
79eee1eb | 4162 | * if any. */ |
90bf1e07 | 4163 | static enum ofperr |
7024dffe | 4164 | modify_flow_strict(struct ofproto *ofproto, struct ofconn *ofconn, |
e3b56933 | 4165 | struct ofputil_flow_mod *fm, |
7ee20df1 | 4166 | const struct ofp_header *request) |
15aaf599 | 4167 | OVS_REQUIRES(ofproto_mutex) |
79eee1eb | 4168 | { |
a9b22b7f | 4169 | struct rule_criteria criteria; |
a8e547c1 | 4170 | struct rule_collection rules; |
6c1491fb BP |
4171 | int error; |
4172 | ||
a9b22b7f BP |
4173 | rule_criteria_init(&criteria, fm->table_id, &fm->match, fm->priority, |
4174 | fm->cookie, fm->cookie_mask, OFPP_ANY, OFPG11_ANY); | |
4175 | error = collect_rules_strict(ofproto, &criteria, &rules); | |
4176 | rule_criteria_destroy(&criteria); | |
4177 | ||
a8e547c1 BP |
4178 | if (!error) { |
4179 | if (rules.n == 0) { | |
4180 | error = modify_flows_add(ofproto, ofconn, fm, request); | |
4181 | } else if (rules.n == 1) { | |
4182 | error = modify_flows__(ofproto, ofconn, fm, request, &rules); | |
4183 | } | |
623e1caf | 4184 | } |
a8e547c1 BP |
4185 | |
4186 | rule_collection_destroy(&rules); | |
4187 | ||
4188 | return error; | |
79eee1eb | 4189 | } |
9ed18e46 BP |
4190 | \f |
4191 | /* OFPFC_DELETE implementation. */ | |
79eee1eb | 4192 | |
254750ce | 4193 | static void |
ca311b2f JR |
4194 | delete_flow__(struct rule *rule, struct ofopgroup *group, |
4195 | enum ofp_flow_removed_reason reason) | |
15aaf599 | 4196 | OVS_REQUIRES(ofproto_mutex) |
254750ce BP |
4197 | { |
4198 | struct ofproto *ofproto = rule->ofproto; | |
4199 | ||
ca311b2f | 4200 | ofproto_rule_send_removed(rule, reason); |
254750ce | 4201 | |
ca311b2f | 4202 | ofoperation_create(group, rule, OFOPERATION_DELETE, reason); |
254750ce | 4203 | oftable_remove_rule(rule); |
8037acb4 | 4204 | ofproto->ofproto_class->rule_delete(rule); |
254750ce BP |
4205 | } |
4206 | ||
9ed18e46 BP |
4207 | /* Deletes the rules listed in 'rules'. |
4208 | * | |
4209 | * Returns 0 on success, otherwise an OpenFlow error code. */ | |
90bf1e07 | 4210 | static enum ofperr |
7024dffe | 4211 | delete_flows__(struct ofproto *ofproto, struct ofconn *ofconn, |
a8e547c1 BP |
4212 | const struct ofp_header *request, |
4213 | const struct rule_collection *rules, | |
ca311b2f | 4214 | enum ofp_flow_removed_reason reason) |
15aaf599 | 4215 | OVS_REQUIRES(ofproto_mutex) |
064af421 | 4216 | { |
7ee20df1 | 4217 | struct ofopgroup *group; |
a8e547c1 | 4218 | size_t i; |
79eee1eb | 4219 | |
7024dffe | 4220 | group = ofopgroup_create(ofproto, ofconn, request, UINT32_MAX); |
a8e547c1 | 4221 | for (i = 0; i < rules->n; i++) { |
15aaf599 | 4222 | delete_flow__(rules->rules[i], group, reason); |
79eee1eb | 4223 | } |
7ee20df1 | 4224 | ofopgroup_submit(group); |
79eee1eb | 4225 | |
9ed18e46 | 4226 | return 0; |
79eee1eb | 4227 | } |
79eee1eb BP |
4228 | |
4229 | /* Implements OFPFC_DELETE. */ | |
90bf1e07 | 4230 | static enum ofperr |
7024dffe BP |
4231 | delete_flows_loose(struct ofproto *ofproto, struct ofconn *ofconn, |
4232 | const struct ofputil_flow_mod *fm, | |
7ee20df1 | 4233 | const struct ofp_header *request) |
15aaf599 | 4234 | OVS_REQUIRES(ofproto_mutex) |
79eee1eb | 4235 | { |
a9b22b7f | 4236 | struct rule_criteria criteria; |
a8e547c1 | 4237 | struct rule_collection rules; |
90bf1e07 | 4238 | enum ofperr error; |
064af421 | 4239 | |
a9b22b7f BP |
4240 | rule_criteria_init(&criteria, fm->table_id, &fm->match, 0, |
4241 | fm->cookie, fm->cookie_mask, | |
4242 | fm->out_port, fm->out_group); | |
4243 | error = collect_rules_loose(ofproto, &criteria, &rules); | |
4244 | rule_criteria_destroy(&criteria); | |
4245 | ||
a8e547c1 BP |
4246 | if (!error && rules.n > 0) { |
4247 | error = delete_flows__(ofproto, ofconn, request, &rules, OFPRR_DELETE); | |
4248 | } | |
4249 | rule_collection_destroy(&rules); | |
4250 | ||
4251 | return error; | |
064af421 BP |
4252 | } |
4253 | ||
79eee1eb | 4254 | /* Implements OFPFC_DELETE_STRICT. */ |
90bf1e07 | 4255 | static enum ofperr |
7024dffe | 4256 | delete_flow_strict(struct ofproto *ofproto, struct ofconn *ofconn, |
1b9d6420 | 4257 | const struct ofputil_flow_mod *fm, |
7ee20df1 | 4258 | const struct ofp_header *request) |
15aaf599 | 4259 | OVS_REQUIRES(ofproto_mutex) |
79eee1eb | 4260 | { |
a9b22b7f | 4261 | struct rule_criteria criteria; |
a8e547c1 | 4262 | struct rule_collection rules; |
90bf1e07 | 4263 | enum ofperr error; |
79eee1eb | 4264 | |
a9b22b7f BP |
4265 | rule_criteria_init(&criteria, fm->table_id, &fm->match, fm->priority, |
4266 | fm->cookie, fm->cookie_mask, | |
4267 | fm->out_port, fm->out_group); | |
4268 | error = collect_rules_strict(ofproto, &criteria, &rules); | |
4269 | rule_criteria_destroy(&criteria); | |
4270 | ||
a8e547c1 BP |
4271 | if (!error && rules.n > 0) { |
4272 | error = delete_flows__(ofproto, ofconn, request, &rules, OFPRR_DELETE); | |
4273 | } | |
4274 | rule_collection_destroy(&rules); | |
4275 | ||
4276 | return error; | |
abe529af BP |
4277 | } |
4278 | ||
4279 | static void | |
4280 | ofproto_rule_send_removed(struct rule *rule, uint8_t reason) | |
15aaf599 | 4281 | OVS_REQUIRES(ofproto_mutex) |
abe529af BP |
4282 | { |
4283 | struct ofputil_flow_removed fr; | |
4284 | ||
3f517bcd DB |
4285 | if (ofproto_rule_is_hidden(rule) || |
4286 | !(rule->flags & OFPUTIL_FF_SEND_FLOW_REM)) { | |
abe529af BP |
4287 | return; |
4288 | } | |
4289 | ||
5cb7a798 | 4290 | minimatch_expand(&rule->cr.match, &fr.match); |
81a76618 | 4291 | fr.priority = rule->cr.priority; |
abe529af BP |
4292 | fr.cookie = rule->flow_cookie; |
4293 | fr.reason = reason; | |
95216219 | 4294 | fr.table_id = rule->table_id; |
65e0be10 BP |
4295 | calc_duration(rule->created, time_msec(), |
4296 | &fr.duration_sec, &fr.duration_nsec); | |
d10976b7 | 4297 | ovs_mutex_lock(&rule->mutex); |
abe529af | 4298 | fr.idle_timeout = rule->idle_timeout; |
fa2bad0f | 4299 | fr.hard_timeout = rule->hard_timeout; |
d10976b7 | 4300 | ovs_mutex_unlock(&rule->mutex); |
abe529af BP |
4301 | rule->ofproto->ofproto_class->rule_get_stats(rule, &fr.packet_count, |
4302 | &fr.byte_count); | |
4303 | ||
4304 | connmgr_send_flow_removed(rule->ofproto->connmgr, &fr); | |
4305 | } | |
4306 | ||
4307 | /* Sends an OpenFlow "flow removed" message with the given 'reason' (either | |
4308 | * OFPRR_HARD_TIMEOUT or OFPRR_IDLE_TIMEOUT), and then removes 'rule' from its | |
4309 | * ofproto. | |
4310 | * | |
e2a3d183 BP |
4311 | * 'rule' must not have a pending operation (that is, 'rule->pending' must be |
4312 | * NULL). | |
4313 | * | |
abe529af BP |
4314 | * ofproto implementation ->run() functions should use this function to expire |
4315 | * OpenFlow flows. */ | |
4316 | void | |
4317 | ofproto_rule_expire(struct rule *rule, uint8_t reason) | |
15aaf599 | 4318 | OVS_REQUIRES(ofproto_mutex) |
abe529af | 4319 | { |
7ee20df1 | 4320 | struct ofproto *ofproto = rule->ofproto; |
7ee20df1 | 4321 | |
7395c052 NZ |
4322 | ovs_assert(reason == OFPRR_HARD_TIMEOUT || reason == OFPRR_IDLE_TIMEOUT |
4323 | || reason == OFPRR_DELETE || reason == OFPRR_GROUP_DELETE); | |
7ee20df1 | 4324 | |
15aaf599 | 4325 | ofproto_rule_send_removed(rule, reason); |
8b81d1ef | 4326 | ofproto_rule_delete__(ofproto, rule); |
79eee1eb | 4327 | } |
994c9973 BP |
4328 | |
4329 | /* Reduces '*timeout' to no more than 'max'. A value of zero in either case | |
4330 | * means "infinite". */ | |
4331 | static void | |
4332 | reduce_timeout(uint16_t max, uint16_t *timeout) | |
4333 | { | |
4334 | if (max && (!*timeout || *timeout > max)) { | |
4335 | *timeout = max; | |
4336 | } | |
4337 | } | |
4338 | ||
4339 | /* If 'idle_timeout' is nonzero, and 'rule' has no idle timeout or an idle | |
4340 | * timeout greater than 'idle_timeout', lowers 'rule''s idle timeout to | |
4341 | * 'idle_timeout' seconds. Similarly for 'hard_timeout'. | |
4342 | * | |
4343 | * Suitable for implementing OFPACT_FIN_TIMEOUT. */ | |
4344 | void | |
4345 | ofproto_rule_reduce_timeouts(struct rule *rule, | |
4346 | uint16_t idle_timeout, uint16_t hard_timeout) | |
d10976b7 | 4347 | OVS_EXCLUDED(ofproto_mutex, rule->mutex) |
994c9973 BP |
4348 | { |
4349 | if (!idle_timeout && !hard_timeout) { | |
4350 | return; | |
4351 | } | |
4352 | ||
abe7b10f | 4353 | ovs_mutex_lock(&ofproto_mutex); |
994c9973 BP |
4354 | if (list_is_empty(&rule->expirable)) { |
4355 | list_insert(&rule->ofproto->expirable, &rule->expirable); | |
4356 | } | |
abe7b10f | 4357 | ovs_mutex_unlock(&ofproto_mutex); |
994c9973 | 4358 | |
d10976b7 | 4359 | ovs_mutex_lock(&rule->mutex); |
994c9973 BP |
4360 | reduce_timeout(idle_timeout, &rule->idle_timeout); |
4361 | reduce_timeout(hard_timeout, &rule->hard_timeout); | |
d10976b7 | 4362 | ovs_mutex_unlock(&rule->mutex); |
994c9973 | 4363 | } |
79eee1eb | 4364 | \f |
90bf1e07 | 4365 | static enum ofperr |
2e4f5fcf | 4366 | handle_flow_mod(struct ofconn *ofconn, const struct ofp_header *oh) |
15aaf599 | 4367 | OVS_EXCLUDED(ofproto_mutex) |
064af421 | 4368 | { |
a5b8d268 | 4369 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); |
a9a2da38 | 4370 | struct ofputil_flow_mod fm; |
f25d0cf3 BP |
4371 | uint64_t ofpacts_stub[1024 / 8]; |
4372 | struct ofpbuf ofpacts; | |
90bf1e07 | 4373 | enum ofperr error; |
a5b8d268 | 4374 | long long int now; |
064af421 | 4375 | |
76589937 | 4376 | error = reject_slave_controller(ofconn); |
9deba63b | 4377 | if (error) { |
f25d0cf3 | 4378 | goto exit; |
9deba63b | 4379 | } |
3052b0c5 | 4380 | |
f25d0cf3 BP |
4381 | ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub); |
4382 | error = ofputil_decode_flow_mod(&fm, oh, ofconn_get_protocol(ofconn), | |
4383 | &ofpacts); | |
548de4dd | 4384 | if (!error) { |
f1822532 | 4385 | error = handle_flow_mod__(ofproto, ofconn, &fm, oh); |
49bdc010 | 4386 | } |
a5b8d268 | 4387 | if (error) { |
f25d0cf3 | 4388 | goto exit_free_ofpacts; |
a5b8d268 | 4389 | } |
2e310801 | 4390 | |
a5b8d268 BP |
4391 | /* Record the operation for logging a summary report. */ |
4392 | switch (fm.command) { | |
4393 | case OFPFC_ADD: | |
4394 | ofproto->n_add++; | |
4395 | break; | |
4396 | ||
4397 | case OFPFC_MODIFY: | |
4398 | case OFPFC_MODIFY_STRICT: | |
4399 | ofproto->n_modify++; | |
4400 | break; | |
4401 | ||
4402 | case OFPFC_DELETE: | |
4403 | case OFPFC_DELETE_STRICT: | |
4404 | ofproto->n_delete++; | |
4405 | break; | |
4406 | } | |
4407 | ||
4408 | now = time_msec(); | |
4409 | if (ofproto->next_op_report == LLONG_MAX) { | |
4410 | ofproto->first_op = now; | |
4411 | ofproto->next_op_report = MAX(now + 10 * 1000, | |
4412 | ofproto->op_backoff); | |
4413 | ofproto->op_backoff = ofproto->next_op_report + 60 * 1000; | |
4414 | } | |
4415 | ofproto->last_op = now; | |
4416 | ||
f25d0cf3 BP |
4417 | exit_free_ofpacts: |
4418 | ofpbuf_uninit(&ofpacts); | |
4419 | exit: | |
4420 | return error; | |
75a75043 BP |
4421 | } |
4422 | ||
90bf1e07 | 4423 | static enum ofperr |
75a75043 | 4424 | handle_flow_mod__(struct ofproto *ofproto, struct ofconn *ofconn, |
e3b56933 | 4425 | struct ofputil_flow_mod *fm, const struct ofp_header *oh) |
15aaf599 | 4426 | OVS_EXCLUDED(ofproto_mutex) |
75a75043 | 4427 | { |
35412852 | 4428 | enum ofperr error; |
75a75043 | 4429 | |
15aaf599 | 4430 | ovs_mutex_lock(&ofproto_mutex); |
35412852 BP |
4431 | if (ofproto->n_pending < 50) { |
4432 | switch (fm->command) { | |
4433 | case OFPFC_ADD: | |
4434 | error = add_flow(ofproto, ofconn, fm, oh); | |
4435 | break; | |
3052b0c5 | 4436 | |
35412852 BP |
4437 | case OFPFC_MODIFY: |
4438 | error = modify_flows_loose(ofproto, ofconn, fm, oh); | |
4439 | break; | |
3052b0c5 | 4440 | |
35412852 BP |
4441 | case OFPFC_MODIFY_STRICT: |
4442 | error = modify_flow_strict(ofproto, ofconn, fm, oh); | |
4443 | break; | |
3052b0c5 | 4444 | |
35412852 BP |
4445 | case OFPFC_DELETE: |
4446 | error = delete_flows_loose(ofproto, ofconn, fm, oh); | |
4447 | break; | |
3052b0c5 | 4448 | |
35412852 BP |
4449 | case OFPFC_DELETE_STRICT: |
4450 | error = delete_flow_strict(ofproto, ofconn, fm, oh); | |
4451 | break; | |
3052b0c5 | 4452 | |
35412852 BP |
4453 | default: |
4454 | if (fm->command > 0xff) { | |
4455 | VLOG_WARN_RL(&rl, "%s: flow_mod has explicit table_id but " | |
4456 | "flow_mod_table_id extension is not enabled", | |
4457 | ofproto->name); | |
4458 | } | |
4459 | error = OFPERR_OFPFMFC_BAD_COMMAND; | |
4460 | break; | |
6c1491fb | 4461 | } |
35412852 BP |
4462 | } else { |
4463 | ovs_assert(!list_is_empty(&ofproto->pending)); | |
4464 | error = OFPROTO_POSTPONE; | |
3052b0c5 | 4465 | } |
15aaf599 | 4466 | ovs_mutex_unlock(&ofproto_mutex); |
35412852 BP |
4467 | |
4468 | run_rule_executes(ofproto); | |
4469 | return error; | |
3052b0c5 BP |
4470 | } |
4471 | ||
90bf1e07 | 4472 | static enum ofperr |
d1e2cf21 | 4473 | handle_role_request(struct ofconn *ofconn, const struct ofp_header *oh) |
9deba63b | 4474 | { |
f4f1ea7e BP |
4475 | struct ofputil_role_request request; |
4476 | struct ofputil_role_request reply; | |
9deba63b | 4477 | struct ofpbuf *buf; |
6ea4776b JR |
4478 | enum ofperr error; |
4479 | ||
f4f1ea7e | 4480 | error = ofputil_decode_role_message(oh, &request); |
6ea4776b JR |
4481 | if (error) { |
4482 | return error; | |
4483 | } | |
9deba63b | 4484 | |
f4f1ea7e BP |
4485 | if (request.role != OFPCR12_ROLE_NOCHANGE) { |
4486 | if (ofconn_get_role(ofconn) != request.role | |
4487 | && ofconn_has_pending_opgroups(ofconn)) { | |
4488 | return OFPROTO_POSTPONE; | |
4489 | } | |
7ee20df1 | 4490 | |
f4f1ea7e BP |
4491 | if (request.have_generation_id |
4492 | && !ofconn_set_master_election_id(ofconn, request.generation_id)) { | |
4493 | return OFPERR_OFPRRFC_STALE; | |
6ea4776b | 4494 | } |
6ea4776b | 4495 | |
f4f1ea7e BP |
4496 | ofconn_set_role(ofconn, request.role); |
4497 | } | |
9deba63b | 4498 | |
f4f1ea7e | 4499 | reply.role = ofconn_get_role(ofconn); |
147cc9d3 BP |
4500 | reply.have_generation_id = ofconn_get_master_election_id( |
4501 | ofconn, &reply.generation_id); | |
f4f1ea7e | 4502 | buf = ofputil_encode_role_reply(oh, &reply); |
b0421aa2 | 4503 | ofconn_send_reply(ofconn, buf); |
9deba63b BP |
4504 | |
4505 | return 0; | |
4506 | } | |
4507 | ||
90bf1e07 | 4508 | static enum ofperr |
6c1491fb BP |
4509 | handle_nxt_flow_mod_table_id(struct ofconn *ofconn, |
4510 | const struct ofp_header *oh) | |
4511 | { | |
982697a4 | 4512 | const struct nx_flow_mod_table_id *msg = ofpmsg_body(oh); |
27527aa0 BP |
4513 | enum ofputil_protocol cur, next; |
4514 | ||
4515 | cur = ofconn_get_protocol(ofconn); | |
4516 | next = ofputil_protocol_set_tid(cur, msg->set != 0); | |
4517 | ofconn_set_protocol(ofconn, next); | |
6c1491fb | 4518 | |
6c1491fb BP |
4519 | return 0; |
4520 | } | |
4521 | ||
90bf1e07 | 4522 | static enum ofperr |
d1e2cf21 | 4523 | handle_nxt_set_flow_format(struct ofconn *ofconn, const struct ofp_header *oh) |
09246b99 | 4524 | { |
982697a4 | 4525 | const struct nx_set_flow_format *msg = ofpmsg_body(oh); |
27527aa0 BP |
4526 | enum ofputil_protocol cur, next; |
4527 | enum ofputil_protocol next_base; | |
09246b99 | 4528 | |
27527aa0 BP |
4529 | next_base = ofputil_nx_flow_format_to_protocol(ntohl(msg->format)); |
4530 | if (!next_base) { | |
90bf1e07 | 4531 | return OFPERR_OFPBRC_EPERM; |
09246b99 | 4532 | } |
7ee20df1 | 4533 | |
27527aa0 BP |
4534 | cur = ofconn_get_protocol(ofconn); |
4535 | next = ofputil_protocol_set_base(cur, next_base); | |
4536 | if (cur != next && ofconn_has_pending_opgroups(ofconn)) { | |
4537 | /* Avoid sending async messages in surprising protocol. */ | |
7ee20df1 BP |
4538 | return OFPROTO_POSTPONE; |
4539 | } | |
4540 | ||
27527aa0 | 4541 | ofconn_set_protocol(ofconn, next); |
7ee20df1 | 4542 | return 0; |
09246b99 BP |
4543 | } |
4544 | ||
90bf1e07 | 4545 | static enum ofperr |
54834960 EJ |
4546 | handle_nxt_set_packet_in_format(struct ofconn *ofconn, |
4547 | const struct ofp_header *oh) | |
4548 | { | |
982697a4 | 4549 | const struct nx_set_packet_in_format *msg = ofpmsg_body(oh); |
54834960 EJ |
4550 | uint32_t format; |
4551 | ||
54834960 | 4552 | format = ntohl(msg->format); |
a15f0eeb | 4553 | if (format != NXPIF_OPENFLOW10 && format != NXPIF_NXM) { |
90bf1e07 | 4554 | return OFPERR_OFPBRC_EPERM; |
54834960 EJ |
4555 | } |
4556 | ||
4557 | if (format != ofconn_get_packet_in_format(ofconn) | |
4558 | && ofconn_has_pending_opgroups(ofconn)) { | |
4559 | /* Avoid sending async message in surprsing packet in format. */ | |
4560 | return OFPROTO_POSTPONE; | |
4561 | } | |
4562 | ||
4563 | ofconn_set_packet_in_format(ofconn, format); | |
4564 | return 0; | |
4565 | } | |
4566 | ||
80d5aefd BP |
4567 | static enum ofperr |
4568 | handle_nxt_set_async_config(struct ofconn *ofconn, const struct ofp_header *oh) | |
4569 | { | |
982697a4 | 4570 | const struct nx_async_config *msg = ofpmsg_body(oh); |
80d5aefd BP |
4571 | uint32_t master[OAM_N_TYPES]; |
4572 | uint32_t slave[OAM_N_TYPES]; | |
4573 | ||
4574 | master[OAM_PACKET_IN] = ntohl(msg->packet_in_mask[0]); | |
4575 | master[OAM_PORT_STATUS] = ntohl(msg->port_status_mask[0]); | |
4576 | master[OAM_FLOW_REMOVED] = ntohl(msg->flow_removed_mask[0]); | |
4577 | ||
4578 | slave[OAM_PACKET_IN] = ntohl(msg->packet_in_mask[1]); | |
4579 | slave[OAM_PORT_STATUS] = ntohl(msg->port_status_mask[1]); | |
4580 | slave[OAM_FLOW_REMOVED] = ntohl(msg->flow_removed_mask[1]); | |
4581 | ||
4582 | ofconn_set_async_config(ofconn, master, slave); | |
4550b647 MM |
4583 | if (ofconn_get_type(ofconn) == OFCONN_SERVICE && |
4584 | !ofconn_get_miss_send_len(ofconn)) { | |
4585 | ofconn_set_miss_send_len(ofconn, OFP_DEFAULT_MISS_SEND_LEN); | |
4586 | } | |
80d5aefd BP |
4587 | |
4588 | return 0; | |
4589 | } | |
4590 | ||
c423b3b3 AC |
4591 | static enum ofperr |
4592 | handle_nxt_get_async_request(struct ofconn *ofconn, const struct ofp_header *oh) | |
4593 | { | |
4594 | struct ofpbuf *buf; | |
4595 | uint32_t master[OAM_N_TYPES]; | |
4596 | uint32_t slave[OAM_N_TYPES]; | |
4597 | struct nx_async_config *msg; | |
4598 | ||
4599 | ofconn_get_async_config(ofconn, master, slave); | |
4600 | buf = ofpraw_alloc_reply(OFPRAW_OFPT13_GET_ASYNC_REPLY, oh, 0); | |
4601 | msg = ofpbuf_put_zeros(buf, sizeof *msg); | |
4602 | ||
4603 | msg->packet_in_mask[0] = htonl(master[OAM_PACKET_IN]); | |
4604 | msg->port_status_mask[0] = htonl(master[OAM_PORT_STATUS]); | |
4605 | msg->flow_removed_mask[0] = htonl(master[OAM_FLOW_REMOVED]); | |
4606 | ||
4607 | msg->packet_in_mask[1] = htonl(slave[OAM_PACKET_IN]); | |
4608 | msg->port_status_mask[1] = htonl(slave[OAM_PORT_STATUS]); | |
4609 | msg->flow_removed_mask[1] = htonl(slave[OAM_FLOW_REMOVED]); | |
4610 | ||
4611 | ofconn_send_reply(ofconn, buf); | |
4612 | ||
4613 | return 0; | |
4614 | } | |
4615 | ||
a7349929 BP |
4616 | static enum ofperr |
4617 | handle_nxt_set_controller_id(struct ofconn *ofconn, | |
4618 | const struct ofp_header *oh) | |
4619 | { | |
982697a4 | 4620 | const struct nx_controller_id *nci = ofpmsg_body(oh); |
a7349929 | 4621 | |
a7349929 BP |
4622 | if (!is_all_zeros(nci->zero, sizeof nci->zero)) { |
4623 | return OFPERR_NXBRC_MUST_BE_ZERO; | |
4624 | } | |
4625 | ||
4626 | ofconn_set_controller_id(ofconn, ntohs(nci->controller_id)); | |
4627 | return 0; | |
4628 | } | |
4629 | ||
90bf1e07 | 4630 | static enum ofperr |
d1e2cf21 | 4631 | handle_barrier_request(struct ofconn *ofconn, const struct ofp_header *oh) |
246e61ea | 4632 | { |
246e61ea JP |
4633 | struct ofpbuf *buf; |
4634 | ||
7ee20df1 BP |
4635 | if (ofconn_has_pending_opgroups(ofconn)) { |
4636 | return OFPROTO_POSTPONE; | |
4637 | } | |
4638 | ||
982697a4 BP |
4639 | buf = ofpraw_alloc_reply((oh->version == OFP10_VERSION |
4640 | ? OFPRAW_OFPT10_BARRIER_REPLY | |
4641 | : OFPRAW_OFPT11_BARRIER_REPLY), oh, 0); | |
b0421aa2 | 4642 | ofconn_send_reply(ofconn, buf); |
246e61ea JP |
4643 | return 0; |
4644 | } | |
4645 | ||
2b07c8b1 BP |
4646 | static void |
4647 | ofproto_compose_flow_refresh_update(const struct rule *rule, | |
4648 | enum nx_flow_monitor_flags flags, | |
4649 | struct list *msgs) | |
15aaf599 | 4650 | OVS_REQUIRES(ofproto_mutex) |
2b07c8b1 BP |
4651 | { |
4652 | struct ofoperation *op = rule->pending; | |
6f00e29b | 4653 | const struct rule_actions *actions; |
2b07c8b1 | 4654 | struct ofputil_flow_update fu; |
5cb7a798 | 4655 | struct match match; |
2b07c8b1 | 4656 | |
b277c7cc | 4657 | if (op && op->type == OFOPERATION_ADD) { |
2b07c8b1 BP |
4658 | /* We'll report the final flow when the operation completes. Reporting |
4659 | * it now would cause a duplicate report later. */ | |
4660 | return; | |
4661 | } | |
4662 | ||
4663 | fu.event = (flags & (NXFMF_INITIAL | NXFMF_ADD) | |
4664 | ? NXFME_ADDED : NXFME_MODIFIED); | |
4665 | fu.reason = 0; | |
d10976b7 | 4666 | ovs_mutex_lock(&rule->mutex); |
2b07c8b1 BP |
4667 | fu.idle_timeout = rule->idle_timeout; |
4668 | fu.hard_timeout = rule->hard_timeout; | |
d10976b7 | 4669 | ovs_mutex_unlock(&rule->mutex); |
2b07c8b1 BP |
4670 | fu.table_id = rule->table_id; |
4671 | fu.cookie = rule->flow_cookie; | |
5cb7a798 BP |
4672 | minimatch_expand(&rule->cr.match, &match); |
4673 | fu.match = &match; | |
6b140a4e | 4674 | fu.priority = rule->cr.priority; |
6f00e29b | 4675 | |
2b07c8b1 | 4676 | if (!(flags & NXFMF_ACTIONS)) { |
6f00e29b | 4677 | actions = NULL; |
2b07c8b1 | 4678 | } else if (!op) { |
6f00e29b | 4679 | actions = rule->actions; |
2b07c8b1 BP |
4680 | } else { |
4681 | /* An operation is in progress. Use the previous version of the flow's | |
4682 | * actions, so that when the operation commits we report the change. */ | |
4683 | switch (op->type) { | |
4684 | case OFOPERATION_ADD: | |
b277c7cc | 4685 | NOT_REACHED(); |
2b07c8b1 BP |
4686 | |
4687 | case OFOPERATION_MODIFY: | |
b277c7cc | 4688 | case OFOPERATION_REPLACE: |
6f00e29b | 4689 | actions = op->actions ? op->actions : rule->actions; |
2b07c8b1 BP |
4690 | break; |
4691 | ||
4692 | case OFOPERATION_DELETE: | |
6f00e29b | 4693 | actions = rule->actions; |
2b07c8b1 BP |
4694 | break; |
4695 | ||
4696 | default: | |
4697 | NOT_REACHED(); | |
4698 | } | |
4699 | } | |
6f00e29b BP |
4700 | fu.ofpacts = actions ? actions->ofpacts : NULL; |
4701 | fu.ofpacts_len = actions ? actions->ofpacts_len : 0; | |
2b07c8b1 BP |
4702 | |
4703 | if (list_is_empty(msgs)) { | |
4704 | ofputil_start_flow_update(msgs); | |
4705 | } | |
4706 | ofputil_append_flow_update(&fu, msgs); | |
4707 | } | |
4708 | ||
4709 | void | |
a8e547c1 BP |
4710 | ofmonitor_compose_refresh_updates(struct rule_collection *rules, |
4711 | struct list *msgs) | |
15aaf599 | 4712 | OVS_REQUIRES(ofproto_mutex) |
2b07c8b1 | 4713 | { |
a8e547c1 | 4714 | size_t i; |
2b07c8b1 | 4715 | |
a8e547c1 BP |
4716 | for (i = 0; i < rules->n; i++) { |
4717 | struct rule *rule = rules->rules[i]; | |
2b07c8b1 BP |
4718 | enum nx_flow_monitor_flags flags = rule->monitor_flags; |
4719 | rule->monitor_flags = 0; | |
4720 | ||
4721 | ofproto_compose_flow_refresh_update(rule, flags, msgs); | |
4722 | } | |
4723 | } | |
4724 | ||
4725 | static void | |
4726 | ofproto_collect_ofmonitor_refresh_rule(const struct ofmonitor *m, | |
4727 | struct rule *rule, uint64_t seqno, | |
a8e547c1 | 4728 | struct rule_collection *rules) |
15aaf599 | 4729 | OVS_REQUIRES(ofproto_mutex) |
2b07c8b1 BP |
4730 | { |
4731 | enum nx_flow_monitor_flags update; | |
4732 | ||
4733 | if (ofproto_rule_is_hidden(rule)) { | |
4734 | return; | |
4735 | } | |
4736 | ||
4737 | if (!(rule->pending | |
4738 | ? ofoperation_has_out_port(rule->pending, m->out_port) | |
4739 | : ofproto_rule_has_out_port(rule, m->out_port))) { | |
4740 | return; | |
4741 | } | |
4742 | ||
4743 | if (seqno) { | |
4744 | if (rule->add_seqno > seqno) { | |
4745 | update = NXFMF_ADD | NXFMF_MODIFY; | |
4746 | } else if (rule->modify_seqno > seqno) { | |
4747 | update = NXFMF_MODIFY; | |
4748 | } else { | |
4749 | return; | |
4750 | } | |
4751 | ||
4752 | if (!(m->flags & update)) { | |
4753 | return; | |
4754 | } | |
4755 | } else { | |
4756 | update = NXFMF_INITIAL; | |
4757 | } | |
4758 | ||
4759 | if (!rule->monitor_flags) { | |
a8e547c1 | 4760 | rule_collection_add(rules, rule); |
2b07c8b1 BP |
4761 | } |
4762 | rule->monitor_flags |= update | (m->flags & NXFMF_ACTIONS); | |
4763 | } | |
4764 | ||
4765 | static void | |
4766 | ofproto_collect_ofmonitor_refresh_rules(const struct ofmonitor *m, | |
4767 | uint64_t seqno, | |
a8e547c1 | 4768 | struct rule_collection *rules) |
15aaf599 | 4769 | OVS_REQUIRES(ofproto_mutex) |
2b07c8b1 BP |
4770 | { |
4771 | const struct ofproto *ofproto = ofconn_get_ofproto(m->ofconn); | |
4772 | const struct ofoperation *op; | |
4773 | const struct oftable *table; | |
81a76618 | 4774 | struct cls_rule target; |
2b07c8b1 | 4775 | |
5cb7a798 | 4776 | cls_rule_init_from_minimatch(&target, &m->match, 0); |
2b07c8b1 BP |
4777 | FOR_EACH_MATCHING_TABLE (table, m->table_id, ofproto) { |
4778 | struct cls_cursor cursor; | |
4779 | struct rule *rule; | |
4780 | ||
0b4f2078 | 4781 | ovs_rwlock_rdlock(&table->cls.rwlock); |
81a76618 | 4782 | cls_cursor_init(&cursor, &table->cls, &target); |
2b07c8b1 | 4783 | CLS_CURSOR_FOR_EACH (rule, cr, &cursor) { |
cb22974d | 4784 | ovs_assert(!rule->pending); /* XXX */ |
2b07c8b1 BP |
4785 | ofproto_collect_ofmonitor_refresh_rule(m, rule, seqno, rules); |
4786 | } | |
0b4f2078 | 4787 | ovs_rwlock_unlock(&table->cls.rwlock); |
2b07c8b1 BP |
4788 | } |
4789 | ||
4790 | HMAP_FOR_EACH (op, hmap_node, &ofproto->deletions) { | |
4791 | struct rule *rule = op->rule; | |
4792 | ||
4793 | if (((m->table_id == 0xff | |
4794 | ? !(ofproto->tables[rule->table_id].flags & OFTABLE_HIDDEN) | |
4795 | : m->table_id == rule->table_id)) | |
81a76618 | 4796 | && cls_rule_is_loose_match(&rule->cr, &target.match)) { |
2b07c8b1 BP |
4797 | ofproto_collect_ofmonitor_refresh_rule(m, rule, seqno, rules); |
4798 | } | |
4799 | } | |
48d28ac1 | 4800 | cls_rule_destroy(&target); |
2b07c8b1 BP |
4801 | } |
4802 | ||
4803 | static void | |
4804 | ofproto_collect_ofmonitor_initial_rules(struct ofmonitor *m, | |
a8e547c1 | 4805 | struct rule_collection *rules) |
15aaf599 | 4806 | OVS_REQUIRES(ofproto_mutex) |
2b07c8b1 BP |
4807 | { |
4808 | if (m->flags & NXFMF_INITIAL) { | |
4809 | ofproto_collect_ofmonitor_refresh_rules(m, 0, rules); | |
4810 | } | |
4811 | } | |
4812 | ||
4813 | void | |
4814 | ofmonitor_collect_resume_rules(struct ofmonitor *m, | |
a8e547c1 | 4815 | uint64_t seqno, struct rule_collection *rules) |
15aaf599 | 4816 | OVS_REQUIRES(ofproto_mutex) |
2b07c8b1 BP |
4817 | { |
4818 | ofproto_collect_ofmonitor_refresh_rules(m, seqno, rules); | |
4819 | } | |
4820 | ||
4821 | static enum ofperr | |
982697a4 | 4822 | handle_flow_monitor_request(struct ofconn *ofconn, const struct ofp_header *oh) |
15aaf599 | 4823 | OVS_EXCLUDED(ofproto_mutex) |
2b07c8b1 BP |
4824 | { |
4825 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
4826 | struct ofmonitor **monitors; | |
4827 | size_t n_monitors, allocated_monitors; | |
a8e547c1 | 4828 | struct rule_collection rules; |
2b07c8b1 BP |
4829 | struct list replies; |
4830 | enum ofperr error; | |
2b07c8b1 BP |
4831 | struct ofpbuf b; |
4832 | size_t i; | |
4833 | ||
4834 | error = 0; | |
982697a4 | 4835 | ofpbuf_use_const(&b, oh, ntohs(oh->length)); |
2b07c8b1 BP |
4836 | monitors = NULL; |
4837 | n_monitors = allocated_monitors = 0; | |
15aaf599 BP |
4838 | |
4839 | ovs_mutex_lock(&ofproto_mutex); | |
2b07c8b1 BP |
4840 | for (;;) { |
4841 | struct ofputil_flow_monitor_request request; | |
4842 | struct ofmonitor *m; | |
4843 | int retval; | |
4844 | ||
4845 | retval = ofputil_decode_flow_monitor_request(&request, &b); | |
4846 | if (retval == EOF) { | |
4847 | break; | |
4848 | } else if (retval) { | |
4849 | error = retval; | |
4850 | goto error; | |
4851 | } | |
4852 | ||
4853 | if (request.table_id != 0xff | |
4854 | && request.table_id >= ofproto->n_tables) { | |
4855 | error = OFPERR_OFPBRC_BAD_TABLE_ID; | |
4856 | goto error; | |
4857 | } | |
4858 | ||
4859 | error = ofmonitor_create(&request, ofconn, &m); | |
4860 | if (error) { | |
4861 | goto error; | |
4862 | } | |
4863 | ||
4864 | if (n_monitors >= allocated_monitors) { | |
4865 | monitors = x2nrealloc(monitors, &allocated_monitors, | |
4866 | sizeof *monitors); | |
4867 | } | |
4868 | monitors[n_monitors++] = m; | |
4869 | } | |
4870 | ||
a8e547c1 | 4871 | rule_collection_init(&rules); |
2b07c8b1 BP |
4872 | for (i = 0; i < n_monitors; i++) { |
4873 | ofproto_collect_ofmonitor_initial_rules(monitors[i], &rules); | |
4874 | } | |
4875 | ||
982697a4 | 4876 | ofpmp_init(&replies, oh); |
2b07c8b1 | 4877 | ofmonitor_compose_refresh_updates(&rules, &replies); |
15aaf599 BP |
4878 | ovs_mutex_unlock(&ofproto_mutex); |
4879 | ||
a8e547c1 BP |
4880 | rule_collection_destroy(&rules); |
4881 | ||
2b07c8b1 | 4882 | ofconn_send_replies(ofconn, &replies); |
2b07c8b1 BP |
4883 | free(monitors); |
4884 | ||
4885 | return 0; | |
4886 | ||
4887 | error: | |
4888 | for (i = 0; i < n_monitors; i++) { | |
4889 | ofmonitor_destroy(monitors[i]); | |
4890 | } | |
4891 | free(monitors); | |
4cd1bc9d BP |
4892 | ovs_mutex_unlock(&ofproto_mutex); |
4893 | ||
2b07c8b1 BP |
4894 | return error; |
4895 | } | |
4896 | ||
4897 | static enum ofperr | |
4898 | handle_flow_monitor_cancel(struct ofconn *ofconn, const struct ofp_header *oh) | |
15aaf599 | 4899 | OVS_EXCLUDED(ofproto_mutex) |
2b07c8b1 BP |
4900 | { |
4901 | struct ofmonitor *m; | |
15aaf599 | 4902 | enum ofperr error; |
2b07c8b1 BP |
4903 | uint32_t id; |
4904 | ||
4905 | id = ofputil_decode_flow_monitor_cancel(oh); | |
15aaf599 BP |
4906 | |
4907 | ovs_mutex_lock(&ofproto_mutex); | |
2b07c8b1 | 4908 | m = ofmonitor_lookup(ofconn, id); |
15aaf599 BP |
4909 | if (m) { |
4910 | ofmonitor_destroy(m); | |
4911 | error = 0; | |
4912 | } else { | |
4913 | error = OFPERR_NXBRC_FM_BAD_ID; | |
2b07c8b1 | 4914 | } |
15aaf599 | 4915 | ovs_mutex_unlock(&ofproto_mutex); |
2b07c8b1 | 4916 | |
15aaf599 | 4917 | return error; |
2b07c8b1 BP |
4918 | } |
4919 | ||
9cae45dc JR |
4920 | /* Meters implementation. |
4921 | * | |
4922 | * Meter table entry, indexed by the OpenFlow meter_id. | |
4923 | * These are always dynamically allocated to allocate enough space for | |
4924 | * the bands. | |
4925 | * 'created' is used to compute the duration for meter stats. | |
4926 | * 'list rules' is needed so that we can delete the dependent rules when the | |
4927 | * meter table entry is deleted. | |
4928 | * 'provider_meter_id' is for the provider's private use. | |
4929 | */ | |
4930 | struct meter { | |
4931 | long long int created; /* Time created. */ | |
4932 | struct list rules; /* List of "struct rule_dpif"s. */ | |
4933 | ofproto_meter_id provider_meter_id; | |
4934 | uint16_t flags; /* Meter flags. */ | |
4935 | uint16_t n_bands; /* Number of meter bands. */ | |
4936 | struct ofputil_meter_band *bands; | |
4937 | }; | |
4938 | ||
4939 | /* | |
4940 | * This is used in instruction validation at flow set-up time, | |
4941 | * as flows may not use non-existing meters. | |
9cae45dc JR |
4942 | * Return value of UINT32_MAX signifies an invalid meter. |
4943 | */ | |
65efd2ab JR |
4944 | static uint32_t |
4945 | get_provider_meter_id(const struct ofproto *ofproto, uint32_t of_meter_id) | |
9cae45dc JR |
4946 | { |
4947 | if (of_meter_id && of_meter_id <= ofproto->meter_features.max_meters) { | |
4948 | const struct meter *meter = ofproto->meters[of_meter_id]; | |
4949 | if (meter) { | |
4950 | return meter->provider_meter_id.uint32; | |
4951 | } | |
4952 | } | |
4953 | return UINT32_MAX; | |
4954 | } | |
4955 | ||
4956 | static void | |
4957 | meter_update(struct meter *meter, const struct ofputil_meter_config *config) | |
4958 | { | |
4959 | free(meter->bands); | |
4960 | ||
4961 | meter->flags = config->flags; | |
4962 | meter->n_bands = config->n_bands; | |
4963 | meter->bands = xmemdup(config->bands, | |
4964 | config->n_bands * sizeof *meter->bands); | |
4965 | } | |
4966 | ||
4967 | static struct meter * | |
4968 | meter_create(const struct ofputil_meter_config *config, | |
4969 | ofproto_meter_id provider_meter_id) | |
4970 | { | |
4971 | struct meter *meter; | |
4972 | ||
4973 | meter = xzalloc(sizeof *meter); | |
4974 | meter->provider_meter_id = provider_meter_id; | |
4975 | meter->created = time_msec(); | |
4976 | list_init(&meter->rules); | |
4977 | ||
4978 | meter_update(meter, config); | |
4979 | ||
4980 | return meter; | |
4981 | } | |
4982 | ||
6b3f7f02 JR |
4983 | static void |
4984 | meter_delete(struct ofproto *ofproto, uint32_t first, uint32_t last) | |
15aaf599 | 4985 | OVS_REQUIRES(ofproto_mutex) |
6b3f7f02 JR |
4986 | { |
4987 | uint32_t mid; | |
4988 | for (mid = first; mid <= last; ++mid) { | |
4989 | struct meter *meter = ofproto->meters[mid]; | |
4990 | if (meter) { | |
4991 | ofproto->meters[mid] = NULL; | |
4992 | ofproto->ofproto_class->meter_del(ofproto, | |
4993 | meter->provider_meter_id); | |
4994 | free(meter->bands); | |
4995 | free(meter); | |
4996 | } | |
4997 | } | |
4998 | } | |
4999 | ||
9cae45dc JR |
5000 | static enum ofperr |
5001 | handle_add_meter(struct ofproto *ofproto, struct ofputil_meter_mod *mm) | |
5002 | { | |
5003 | ofproto_meter_id provider_meter_id = { UINT32_MAX }; | |
5004 | struct meter **meterp = &ofproto->meters[mm->meter.meter_id]; | |
5005 | enum ofperr error; | |
5006 | ||
5007 | if (*meterp) { | |
5008 | return OFPERR_OFPMMFC_METER_EXISTS; | |
5009 | } | |
5010 | ||
5011 | error = ofproto->ofproto_class->meter_set(ofproto, &provider_meter_id, | |
5012 | &mm->meter); | |
5013 | if (!error) { | |
5014 | ovs_assert(provider_meter_id.uint32 != UINT32_MAX); | |
5015 | *meterp = meter_create(&mm->meter, provider_meter_id); | |
5016 | } | |
f0f8c6c2 | 5017 | return error; |
9cae45dc JR |
5018 | } |
5019 | ||
5020 | static enum ofperr | |
5021 | handle_modify_meter(struct ofproto *ofproto, struct ofputil_meter_mod *mm) | |
5022 | { | |
5023 | struct meter *meter = ofproto->meters[mm->meter.meter_id]; | |
5024 | enum ofperr error; | |
e555eb7c | 5025 | uint32_t provider_meter_id; |
9cae45dc JR |
5026 | |
5027 | if (!meter) { | |
5028 | return OFPERR_OFPMMFC_UNKNOWN_METER; | |
5029 | } | |
5030 | ||
e555eb7c | 5031 | provider_meter_id = meter->provider_meter_id.uint32; |
9cae45dc JR |
5032 | error = ofproto->ofproto_class->meter_set(ofproto, |
5033 | &meter->provider_meter_id, | |
5034 | &mm->meter); | |
e555eb7c | 5035 | ovs_assert(meter->provider_meter_id.uint32 == provider_meter_id); |
9cae45dc JR |
5036 | if (!error) { |
5037 | meter_update(meter, &mm->meter); | |
5038 | } | |
5039 | return error; | |
5040 | } | |
5041 | ||
5042 | static enum ofperr | |
5043 | handle_delete_meter(struct ofconn *ofconn, const struct ofp_header *oh, | |
5044 | struct ofputil_meter_mod *mm) | |
15aaf599 | 5045 | OVS_EXCLUDED(ofproto_mutex) |
9cae45dc JR |
5046 | { |
5047 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
5048 | uint32_t meter_id = mm->meter.meter_id; | |
a8e547c1 BP |
5049 | struct rule_collection rules; |
5050 | enum ofperr error = 0; | |
9cae45dc | 5051 | uint32_t first, last; |
9cae45dc JR |
5052 | |
5053 | if (meter_id == OFPM13_ALL) { | |
5054 | first = 1; | |
5055 | last = ofproto->meter_features.max_meters; | |
5056 | } else { | |
5057 | if (!meter_id || meter_id > ofproto->meter_features.max_meters) { | |
5058 | return 0; | |
5059 | } | |
5060 | first = last = meter_id; | |
5061 | } | |
5062 | ||
5063 | /* First delete the rules that use this meter. If any of those rules are | |
5064 | * currently being modified, postpone the whole operation until later. */ | |
a8e547c1 | 5065 | rule_collection_init(&rules); |
15aaf599 | 5066 | ovs_mutex_lock(&ofproto_mutex); |
9cae45dc JR |
5067 | for (meter_id = first; meter_id <= last; ++meter_id) { |
5068 | struct meter *meter = ofproto->meters[meter_id]; | |
5069 | if (meter && !list_is_empty(&meter->rules)) { | |
5070 | struct rule *rule; | |
5071 | ||
5072 | LIST_FOR_EACH (rule, meter_list_node, &meter->rules) { | |
5073 | if (rule->pending) { | |
a8e547c1 BP |
5074 | error = OFPROTO_POSTPONE; |
5075 | goto exit; | |
9cae45dc | 5076 | } |
a8e547c1 | 5077 | rule_collection_add(&rules, rule); |
9cae45dc JR |
5078 | } |
5079 | } | |
5080 | } | |
a8e547c1 | 5081 | if (rules.n > 0) { |
9cae45dc JR |
5082 | delete_flows__(ofproto, ofconn, oh, &rules, OFPRR_METER_DELETE); |
5083 | } | |
5084 | ||
5085 | /* Delete the meters. */ | |
6b3f7f02 | 5086 | meter_delete(ofproto, first, last); |
9cae45dc | 5087 | |
a8e547c1 | 5088 | exit: |
15aaf599 | 5089 | ovs_mutex_unlock(&ofproto_mutex); |
a8e547c1 BP |
5090 | rule_collection_destroy(&rules); |
5091 | ||
5092 | return error; | |
9cae45dc JR |
5093 | } |
5094 | ||
5095 | static enum ofperr | |
5096 | handle_meter_mod(struct ofconn *ofconn, const struct ofp_header *oh) | |
5097 | { | |
5098 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
5099 | struct ofputil_meter_mod mm; | |
5100 | uint64_t bands_stub[256 / 8]; | |
5101 | struct ofpbuf bands; | |
5102 | uint32_t meter_id; | |
5103 | enum ofperr error; | |
5104 | ||
5105 | error = reject_slave_controller(ofconn); | |
5106 | if (error) { | |
5107 | return error; | |
5108 | } | |
5109 | ||
5110 | ofpbuf_use_stub(&bands, bands_stub, sizeof bands_stub); | |
5111 | ||
5112 | error = ofputil_decode_meter_mod(oh, &mm, &bands); | |
5113 | if (error) { | |
5114 | goto exit_free_bands; | |
5115 | } | |
5116 | ||
5117 | meter_id = mm.meter.meter_id; | |
5118 | ||
5119 | if (mm.command != OFPMC13_DELETE) { | |
5120 | /* Fails also when meters are not implemented by the provider. */ | |
b31e8700 | 5121 | if (meter_id == 0 || meter_id > OFPM13_MAX) { |
9cae45dc JR |
5122 | error = OFPERR_OFPMMFC_INVALID_METER; |
5123 | goto exit_free_bands; | |
b31e8700 JR |
5124 | } else if (meter_id > ofproto->meter_features.max_meters) { |
5125 | error = OFPERR_OFPMMFC_OUT_OF_METERS; | |
5126 | goto exit_free_bands; | |
9cae45dc JR |
5127 | } |
5128 | if (mm.meter.n_bands > ofproto->meter_features.max_bands) { | |
5129 | error = OFPERR_OFPMMFC_OUT_OF_BANDS; | |
5130 | goto exit_free_bands; | |
5131 | } | |
5132 | } | |
5133 | ||
5134 | switch (mm.command) { | |
5135 | case OFPMC13_ADD: | |
5136 | error = handle_add_meter(ofproto, &mm); | |
5137 | break; | |
5138 | ||
5139 | case OFPMC13_MODIFY: | |
5140 | error = handle_modify_meter(ofproto, &mm); | |
5141 | break; | |
5142 | ||
5143 | case OFPMC13_DELETE: | |
5144 | error = handle_delete_meter(ofconn, oh, &mm); | |
5145 | break; | |
5146 | ||
5147 | default: | |
5148 | error = OFPERR_OFPMMFC_BAD_COMMAND; | |
5149 | break; | |
5150 | } | |
5151 | ||
5152 | exit_free_bands: | |
5153 | ofpbuf_uninit(&bands); | |
5154 | return error; | |
5155 | } | |
5156 | ||
5157 | static enum ofperr | |
5158 | handle_meter_features_request(struct ofconn *ofconn, | |
5159 | const struct ofp_header *request) | |
5160 | { | |
5161 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
5162 | struct ofputil_meter_features features; | |
5163 | struct ofpbuf *b; | |
5164 | ||
5165 | if (ofproto->ofproto_class->meter_get_features) { | |
5166 | ofproto->ofproto_class->meter_get_features(ofproto, &features); | |
5167 | } else { | |
5168 | memset(&features, 0, sizeof features); | |
5169 | } | |
5170 | b = ofputil_encode_meter_features_reply(&features, request); | |
5171 | ||
5172 | ofconn_send_reply(ofconn, b); | |
5173 | return 0; | |
5174 | } | |
5175 | ||
5176 | static enum ofperr | |
5177 | handle_meter_request(struct ofconn *ofconn, const struct ofp_header *request, | |
5178 | enum ofptype type) | |
5179 | { | |
5180 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
5181 | struct list replies; | |
5182 | uint64_t bands_stub[256 / 8]; | |
5183 | struct ofpbuf bands; | |
5184 | uint32_t meter_id, first, last; | |
5185 | ||
5186 | ofputil_decode_meter_request(request, &meter_id); | |
5187 | ||
5188 | if (meter_id == OFPM13_ALL) { | |
5189 | first = 1; | |
5190 | last = ofproto->meter_features.max_meters; | |
5191 | } else { | |
5192 | if (!meter_id || meter_id > ofproto->meter_features.max_meters || | |
5193 | !ofproto->meters[meter_id]) { | |
5194 | return OFPERR_OFPMMFC_UNKNOWN_METER; | |
5195 | } | |
5196 | first = last = meter_id; | |
5197 | } | |
5198 | ||
5199 | ofpbuf_use_stub(&bands, bands_stub, sizeof bands_stub); | |
5200 | ofpmp_init(&replies, request); | |
5201 | ||
5202 | for (meter_id = first; meter_id <= last; ++meter_id) { | |
5203 | struct meter *meter = ofproto->meters[meter_id]; | |
5204 | if (!meter) { | |
5205 | continue; /* Skip non-existing meters. */ | |
5206 | } | |
261bd854 | 5207 | if (type == OFPTYPE_METER_STATS_REQUEST) { |
9cae45dc JR |
5208 | struct ofputil_meter_stats stats; |
5209 | ||
5210 | stats.meter_id = meter_id; | |
5211 | ||
5212 | /* Provider sets the packet and byte counts, we do the rest. */ | |
5213 | stats.flow_count = list_size(&meter->rules); | |
5214 | calc_duration(meter->created, time_msec(), | |
5215 | &stats.duration_sec, &stats.duration_nsec); | |
5216 | stats.n_bands = meter->n_bands; | |
5217 | ofpbuf_clear(&bands); | |
5218 | stats.bands | |
5219 | = ofpbuf_put_uninit(&bands, | |
5220 | meter->n_bands * sizeof *stats.bands); | |
5221 | ||
5222 | if (!ofproto->ofproto_class->meter_get(ofproto, | |
5223 | meter->provider_meter_id, | |
5224 | &stats)) { | |
5225 | ofputil_append_meter_stats(&replies, &stats); | |
5226 | } | |
5227 | } else { /* type == OFPTYPE_METER_CONFIG_REQUEST */ | |
5228 | struct ofputil_meter_config config; | |
5229 | ||
5230 | config.meter_id = meter_id; | |
5231 | config.flags = meter->flags; | |
5232 | config.n_bands = meter->n_bands; | |
5233 | config.bands = meter->bands; | |
5234 | ofputil_append_meter_config(&replies, &config); | |
5235 | } | |
5236 | } | |
5237 | ||
5238 | ofconn_send_replies(ofconn, &replies); | |
5239 | ofpbuf_uninit(&bands); | |
5240 | return 0; | |
5241 | } | |
5242 | ||
7395c052 NZ |
5243 | bool |
5244 | ofproto_group_lookup(const struct ofproto *ofproto, uint32_t group_id, | |
5245 | struct ofgroup **group) | |
5246 | OVS_TRY_RDLOCK(true, (*group)->rwlock) | |
5247 | { | |
5248 | ovs_rwlock_rdlock(&ofproto->groups_rwlock); | |
5249 | HMAP_FOR_EACH_IN_BUCKET (*group, hmap_node, | |
5250 | hash_int(group_id, 0), &ofproto->groups) { | |
5251 | if ((*group)->group_id == group_id) { | |
5252 | ovs_rwlock_rdlock(&(*group)->rwlock); | |
5253 | ovs_rwlock_unlock(&ofproto->groups_rwlock); | |
5254 | return true; | |
5255 | } | |
5256 | } | |
5257 | ovs_rwlock_unlock(&ofproto->groups_rwlock); | |
5258 | return false; | |
5259 | } | |
5260 | ||
5261 | void | |
5262 | ofproto_group_release(struct ofgroup *group) | |
5263 | OVS_RELEASES(group->rwlock) | |
5264 | { | |
5265 | ovs_rwlock_unlock(&group->rwlock); | |
5266 | } | |
5267 | ||
5268 | static bool | |
5269 | ofproto_group_write_lookup(const struct ofproto *ofproto, uint32_t group_id, | |
5270 | struct ofgroup **group) | |
5271 | OVS_TRY_WRLOCK(true, ofproto->groups_rwlock) | |
5272 | OVS_TRY_WRLOCK(true, (*group)->rwlock) | |
5273 | { | |
5274 | ovs_rwlock_wrlock(&ofproto->groups_rwlock); | |
5275 | HMAP_FOR_EACH_IN_BUCKET (*group, hmap_node, | |
5276 | hash_int(group_id, 0), &ofproto->groups) { | |
5277 | if ((*group)->group_id == group_id) { | |
5278 | ovs_rwlock_wrlock(&(*group)->rwlock); | |
5279 | return true; | |
5280 | } | |
5281 | } | |
5282 | ovs_rwlock_unlock(&ofproto->groups_rwlock); | |
5283 | return false; | |
5284 | } | |
5285 | ||
5286 | static bool | |
5287 | ofproto_group_exists(const struct ofproto *ofproto, uint32_t group_id) | |
5288 | OVS_REQ_RDLOCK(ofproto->groups_rwlock) | |
5289 | { | |
5290 | struct ofgroup *grp; | |
5291 | ||
5292 | HMAP_FOR_EACH_IN_BUCKET (grp, hmap_node, | |
5293 | hash_int(group_id, 0), &ofproto->groups) { | |
5294 | if (grp->group_id == group_id) { | |
5295 | return true; | |
5296 | } | |
5297 | } | |
5298 | return false; | |
5299 | } | |
5300 | ||
5301 | static void | |
5302 | append_group_stats(struct ofgroup *group, struct list *replies) | |
5303 | OVS_REQ_RDLOCK(group->rwlock) | |
5304 | { | |
5305 | struct ofputil_group_stats ogs; | |
5306 | struct ofproto *ofproto = group->ofproto; | |
5307 | long long int now = time_msec(); | |
5308 | int error; | |
5309 | ||
646b2a9c SH |
5310 | ogs.bucket_stats = xmalloc(group->n_buckets * sizeof *ogs.bucket_stats); |
5311 | ||
7395c052 NZ |
5312 | error = (ofproto->ofproto_class->group_get_stats |
5313 | ? ofproto->ofproto_class->group_get_stats(group, &ogs) | |
5314 | : EOPNOTSUPP); | |
5315 | if (error) { | |
5316 | ogs.ref_count = UINT32_MAX; | |
5317 | ogs.packet_count = UINT64_MAX; | |
5318 | ogs.byte_count = UINT64_MAX; | |
5319 | ogs.n_buckets = group->n_buckets; | |
5320 | memset(ogs.bucket_stats, 0xff, | |
5321 | ogs.n_buckets * sizeof *ogs.bucket_stats); | |
5322 | } | |
5323 | ||
5324 | ogs.group_id = group->group_id; | |
5325 | calc_duration(group->created, now, &ogs.duration_sec, &ogs.duration_nsec); | |
5326 | ||
5327 | ofputil_append_group_stats(replies, &ogs); | |
646b2a9c SH |
5328 | |
5329 | free(ogs.bucket_stats); | |
7395c052 NZ |
5330 | } |
5331 | ||
5332 | static enum ofperr | |
5333 | handle_group_stats_request(struct ofconn *ofconn, | |
5334 | const struct ofp_header *request) | |
5335 | { | |
5336 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
5337 | struct list replies; | |
5338 | enum ofperr error; | |
5339 | struct ofgroup *group; | |
5340 | uint32_t group_id; | |
5341 | ||
5342 | error = ofputil_decode_group_stats_request(request, &group_id); | |
5343 | if (error) { | |
5344 | return error; | |
5345 | } | |
5346 | ||
5347 | ofpmp_init(&replies, request); | |
5348 | ||
5349 | if (group_id == OFPG_ALL) { | |
5350 | ovs_rwlock_rdlock(&ofproto->groups_rwlock); | |
5351 | HMAP_FOR_EACH (group, hmap_node, &ofproto->groups) { | |
5352 | ovs_rwlock_rdlock(&group->rwlock); | |
5353 | append_group_stats(group, &replies); | |
5354 | ovs_rwlock_unlock(&group->rwlock); | |
5355 | } | |
5356 | ovs_rwlock_unlock(&ofproto->groups_rwlock); | |
5357 | } else { | |
5358 | if (ofproto_group_lookup(ofproto, group_id, &group)) { | |
5359 | append_group_stats(group, &replies); | |
5360 | ofproto_group_release(group); | |
5361 | } | |
5362 | } | |
5363 | ||
5364 | ofconn_send_replies(ofconn, &replies); | |
5365 | ||
5366 | return 0; | |
5367 | } | |
5368 | ||
5369 | static enum ofperr | |
5370 | handle_group_desc_stats_request(struct ofconn *ofconn, | |
5371 | const struct ofp_header *request) | |
5372 | { | |
5373 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
5374 | struct list replies; | |
5375 | struct ofputil_group_desc gds; | |
5376 | struct ofgroup *group; | |
5377 | ||
5378 | ofpmp_init(&replies, request); | |
5379 | ||
5380 | ovs_rwlock_rdlock(&ofproto->groups_rwlock); | |
5381 | HMAP_FOR_EACH (group, hmap_node, &ofproto->groups) { | |
5382 | gds.group_id = group->group_id; | |
5383 | gds.type = group->type; | |
5384 | ofputil_append_group_desc_reply(&gds, &group->buckets, &replies); | |
5385 | } | |
5386 | ovs_rwlock_unlock(&ofproto->groups_rwlock); | |
5387 | ||
5388 | ofconn_send_replies(ofconn, &replies); | |
5389 | ||
5390 | return 0; | |
5391 | } | |
5392 | ||
5393 | static enum ofperr | |
5394 | handle_group_features_stats_request(struct ofconn *ofconn, | |
5395 | const struct ofp_header *request) | |
5396 | { | |
5397 | struct ofproto *p = ofconn_get_ofproto(ofconn); | |
5398 | struct ofpbuf *msg; | |
5399 | ||
5400 | msg = ofputil_encode_group_features_reply(&p->ogf, request); | |
5401 | if (msg) { | |
5402 | ofconn_send_reply(ofconn, msg); | |
5403 | } | |
5404 | ||
5405 | return 0; | |
5406 | } | |
5407 | ||
5408 | /* Implements OFPGC11_ADD | |
5409 | * in which no matching flow already exists in the flow table. | |
5410 | * | |
5411 | * Adds the flow specified by 'ofm', which is followed by 'n_actions' | |
5412 | * ofp_actions, to the ofproto's flow table. Returns 0 on success, an OpenFlow | |
5413 | * error code on failure, or OFPROTO_POSTPONE if the operation cannot be | |
5414 | * initiated now but may be retried later. | |
5415 | * | |
5416 | * Upon successful return, takes ownership of 'fm->ofpacts'. On failure, | |
5417 | * ownership remains with the caller. | |
5418 | * | |
5419 | * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id, | |
5420 | * if any. */ | |
5421 | static enum ofperr | |
5422 | add_group(struct ofproto *ofproto, struct ofputil_group_mod *gm) | |
5423 | { | |
5424 | struct ofgroup *ofgroup; | |
5425 | enum ofperr error; | |
5426 | ||
5427 | if (gm->group_id > OFPG_MAX) { | |
5428 | return OFPERR_OFPGMFC_INVALID_GROUP; | |
5429 | } | |
5430 | if (gm->type > OFPGT11_FF) { | |
5431 | return OFPERR_OFPGMFC_BAD_TYPE; | |
5432 | } | |
5433 | ||
5434 | /* Allocate new group and initialize it. */ | |
5435 | ofgroup = ofproto->ofproto_class->group_alloc(); | |
5436 | if (!ofgroup) { | |
5437 | VLOG_WARN_RL(&rl, "%s: failed to create group", ofproto->name); | |
5438 | return OFPERR_OFPGMFC_OUT_OF_GROUPS; | |
5439 | } | |
5440 | ||
5441 | ovs_rwlock_init(&ofgroup->rwlock); | |
5442 | ofgroup->ofproto = ofproto; | |
5443 | ofgroup->group_id = gm->group_id; | |
5444 | ofgroup->type = gm->type; | |
5445 | ofgroup->created = ofgroup->modified = time_msec(); | |
5446 | ||
5447 | list_move(&ofgroup->buckets, &gm->buckets); | |
5448 | ofgroup->n_buckets = list_size(&ofgroup->buckets); | |
5449 | ||
5450 | /* Construct called BEFORE any locks are held. */ | |
5451 | error = ofproto->ofproto_class->group_construct(ofgroup); | |
5452 | if (error) { | |
5453 | goto free_out; | |
5454 | } | |
5455 | ||
5456 | /* We wrlock as late as possible to minimize the time we jam any other | |
5457 | * threads: No visible state changes before acquiring the lock. */ | |
5458 | ovs_rwlock_wrlock(&ofproto->groups_rwlock); | |
5459 | ||
5460 | if (ofproto->n_groups[gm->type] >= ofproto->ogf.max_groups[gm->type]) { | |
5461 | error = OFPERR_OFPGMFC_OUT_OF_GROUPS; | |
5462 | goto unlock_out; | |
5463 | } | |
5464 | ||
5465 | if (ofproto_group_exists(ofproto, gm->group_id)) { | |
5466 | error = OFPERR_OFPGMFC_GROUP_EXISTS; | |
5467 | goto unlock_out; | |
5468 | } | |
5469 | ||
5470 | if (!error) { | |
5471 | /* Insert new group. */ | |
5472 | hmap_insert(&ofproto->groups, &ofgroup->hmap_node, | |
5473 | hash_int(ofgroup->group_id, 0)); | |
5474 | ofproto->n_groups[ofgroup->type]++; | |
5475 | ||
5476 | ovs_rwlock_unlock(&ofproto->groups_rwlock); | |
5477 | return error; | |
5478 | } | |
5479 | ||
5480 | unlock_out: | |
5481 | ovs_rwlock_unlock(&ofproto->groups_rwlock); | |
5482 | ofproto->ofproto_class->group_destruct(ofgroup); | |
5483 | free_out: | |
5484 | ofputil_bucket_list_destroy(&ofgroup->buckets); | |
5485 | ofproto->ofproto_class->group_dealloc(ofgroup); | |
5486 | ||
5487 | return error; | |
5488 | } | |
5489 | ||
5490 | /* Implements OFPFC_MODIFY. Returns 0 on success or an OpenFlow error code on | |
5491 | * failure. | |
5492 | * | |
5493 | * 'ofconn' is used to retrieve the packet buffer specified in fm->buffer_id, | |
5494 | * if any. */ | |
5495 | static enum ofperr | |
5496 | modify_group(struct ofproto *ofproto, struct ofputil_group_mod *gm) | |
5497 | { | |
5498 | struct ofgroup *ofgroup; | |
5499 | struct ofgroup *victim; | |
5500 | enum ofperr error; | |
5501 | ||
5502 | if (gm->group_id > OFPG_MAX) { | |
5503 | return OFPERR_OFPGMFC_INVALID_GROUP; | |
5504 | } | |
5505 | ||
5506 | if (gm->type > OFPGT11_FF) { | |
5507 | return OFPERR_OFPGMFC_BAD_TYPE; | |
5508 | } | |
5509 | ||
5510 | victim = ofproto->ofproto_class->group_alloc(); | |
5511 | if (!victim) { | |
5512 | VLOG_WARN_RL(&rl, "%s: failed to allocate group", ofproto->name); | |
5513 | return OFPERR_OFPGMFC_OUT_OF_GROUPS; | |
5514 | } | |
5515 | ||
5516 | if (!ofproto_group_write_lookup(ofproto, gm->group_id, &ofgroup)) { | |
5517 | error = OFPERR_OFPGMFC_UNKNOWN_GROUP; | |
5518 | goto free_out; | |
5519 | } | |
5520 | /* Both group's and its container's write locks held now. | |
5521 | * Also, n_groups[] is protected by ofproto->groups_rwlock. */ | |
5522 | if (ofgroup->type != gm->type | |
5523 | && ofproto->n_groups[gm->type] >= ofproto->ogf.max_groups[gm->type]) { | |
5524 | error = OFPERR_OFPGMFC_OUT_OF_GROUPS; | |
5525 | goto unlock_out; | |
5526 | } | |
5527 | ||
5528 | *victim = *ofgroup; | |
5529 | list_move(&victim->buckets, &ofgroup->buckets); | |
5530 | ||
5531 | ofgroup->type = gm->type; | |
5532 | list_move(&ofgroup->buckets, &gm->buckets); | |
5533 | ofgroup->n_buckets = list_size(&ofgroup->buckets); | |
5534 | ||
5535 | error = ofproto->ofproto_class->group_modify(ofgroup, victim); | |
5536 | if (!error) { | |
5537 | ofputil_bucket_list_destroy(&victim->buckets); | |
5538 | ofproto->n_groups[victim->type]--; | |
5539 | ofproto->n_groups[ofgroup->type]++; | |
5540 | ofgroup->modified = time_msec(); | |
5541 | } else { | |
5542 | ofputil_bucket_list_destroy(&ofgroup->buckets); | |
5543 | ||
5544 | *ofgroup = *victim; | |
5545 | list_move(&ofgroup->buckets, &victim->buckets); | |
5546 | } | |
5547 | ||
5548 | unlock_out: | |
5549 | ovs_rwlock_unlock(&ofgroup->rwlock); | |
5550 | ovs_rwlock_unlock(&ofproto->groups_rwlock); | |
5551 | free_out: | |
5552 | ofproto->ofproto_class->group_dealloc(victim); | |
5553 | return error; | |
5554 | } | |
5555 | ||
5556 | static void | |
5557 | delete_group__(struct ofproto *ofproto, struct ofgroup *ofgroup) | |
5558 | OVS_RELEASES(ofproto->groups_rwlock) | |
5559 | { | |
5560 | /* Must wait until existing readers are done, | |
5561 | * while holding the container's write lock at the same time. */ | |
5562 | ovs_rwlock_wrlock(&ofgroup->rwlock); | |
5563 | hmap_remove(&ofproto->groups, &ofgroup->hmap_node); | |
5564 | /* No-one can find this group any more. */ | |
5565 | ofproto->n_groups[ofgroup->type]--; | |
5566 | ovs_rwlock_unlock(&ofproto->groups_rwlock); | |
5567 | ||
5568 | ofproto->ofproto_class->group_destruct(ofgroup); | |
5569 | ofputil_bucket_list_destroy(&ofgroup->buckets); | |
5570 | ovs_rwlock_unlock(&ofgroup->rwlock); | |
5571 | ovs_rwlock_destroy(&ofgroup->rwlock); | |
5572 | ofproto->ofproto_class->group_dealloc(ofgroup); | |
5573 | } | |
5574 | ||
5575 | /* Implements OFPGC_DELETE. */ | |
5576 | static void | |
5577 | delete_group(struct ofproto *ofproto, uint32_t group_id) | |
5578 | { | |
5579 | struct ofgroup *ofgroup; | |
5580 | ||
5581 | ovs_rwlock_wrlock(&ofproto->groups_rwlock); | |
5582 | if (group_id == OFPG_ALL) { | |
5583 | for (;;) { | |
5584 | struct hmap_node *node = hmap_first(&ofproto->groups); | |
5585 | if (!node) { | |
5586 | break; | |
5587 | } | |
5588 | ofgroup = CONTAINER_OF(node, struct ofgroup, hmap_node); | |
5589 | delete_group__(ofproto, ofgroup); | |
5590 | /* Lock for each node separately, so that we will not jam the | |
5591 | * other threads for too long time. */ | |
5592 | ovs_rwlock_wrlock(&ofproto->groups_rwlock); | |
5593 | } | |
5594 | } else { | |
5595 | HMAP_FOR_EACH_IN_BUCKET (ofgroup, hmap_node, | |
5596 | hash_int(group_id, 0), &ofproto->groups) { | |
5597 | if (ofgroup->group_id == group_id) { | |
5598 | delete_group__(ofproto, ofgroup); | |
5599 | return; | |
5600 | } | |
5601 | } | |
5602 | } | |
5603 | ovs_rwlock_unlock(&ofproto->groups_rwlock); | |
5604 | } | |
5605 | ||
5606 | static enum ofperr | |
5607 | handle_group_mod(struct ofconn *ofconn, const struct ofp_header *oh) | |
5608 | { | |
5609 | struct ofproto *ofproto = ofconn_get_ofproto(ofconn); | |
5610 | struct ofputil_group_mod gm; | |
5611 | enum ofperr error; | |
5612 | ||
5613 | error = reject_slave_controller(ofconn); | |
5614 | if (error) { | |
5615 | return error; | |
5616 | } | |
5617 | ||
5618 | error = ofputil_decode_group_mod(oh, &gm); | |
5619 | if (error) { | |
5620 | return error; | |
5621 | } | |
5622 | ||
5623 | switch (gm.command) { | |
5624 | case OFPGC11_ADD: | |
5625 | return add_group(ofproto, &gm); | |
5626 | ||
5627 | case OFPGC11_MODIFY: | |
5628 | return modify_group(ofproto, &gm); | |
5629 | ||
5630 | case OFPGC11_DELETE: | |
5631 | delete_group(ofproto, gm.group_id); | |
5632 | return 0; | |
5633 | ||
5634 | default: | |
5635 | if (gm.command > OFPGC11_DELETE) { | |
5636 | VLOG_WARN_RL(&rl, "%s: Invalid group_mod command type %d", | |
5637 | ofproto->name, gm.command); | |
5638 | } | |
5639 | return OFPERR_OFPGMFC_BAD_COMMAND; | |
5640 | } | |
5641 | } | |
5642 | ||
918f2b82 AZ |
5643 | static enum ofperr |
5644 | handle_table_mod(struct ofconn *ofconn, const struct ofp_header *oh) | |
5645 | { | |
5646 | struct ofputil_table_mod tm; | |
5647 | enum ofperr error; | |
5648 | ||
5649 | error = reject_slave_controller(ofconn); | |
5650 | if (error) { | |
5651 | return error; | |
5652 | } | |
5653 | ||
5654 | error = ofputil_decode_table_mod(oh, &tm); | |
5655 | if (error) { | |
5656 | return error; | |
5657 | } | |
5658 | ||
5659 | /* XXX Actual table mod support is not implemented yet. */ | |
5660 | return 0; | |
5661 | } | |
5662 | ||
90bf1e07 | 5663 | static enum ofperr |
d1e2cf21 | 5664 | handle_openflow__(struct ofconn *ofconn, const struct ofpbuf *msg) |
15aaf599 | 5665 | OVS_EXCLUDED(ofproto_mutex) |
064af421 | 5666 | { |
d1e2cf21 | 5667 | const struct ofp_header *oh = msg->data; |
982697a4 | 5668 | enum ofptype type; |
90bf1e07 | 5669 | enum ofperr error; |
064af421 | 5670 | |
982697a4 | 5671 | error = ofptype_decode(&type, oh); |
d1e2cf21 BP |
5672 | if (error) { |
5673 | return error; | |
5674 | } | |
064af421 | 5675 | |
982697a4 | 5676 | switch (type) { |
d1e2cf21 | 5677 | /* OpenFlow requests. */ |
982697a4 | 5678 | case OFPTYPE_ECHO_REQUEST: |
d1e2cf21 | 5679 | return handle_echo_request(ofconn, oh); |
064af421 | 5680 | |
982697a4 | 5681 | case OFPTYPE_FEATURES_REQUEST: |
d1e2cf21 | 5682 | return handle_features_request(ofconn, oh); |
064af421 | 5683 | |
982697a4 | 5684 | case OFPTYPE_GET_CONFIG_REQUEST: |
d1e2cf21 | 5685 | return handle_get_config_request(ofconn, oh); |
064af421 | 5686 | |
982697a4 BP |
5687 | case OFPTYPE_SET_CONFIG: |
5688 | return handle_set_config(ofconn, oh); | |
064af421 | 5689 | |
982697a4 BP |
5690 | case OFPTYPE_PACKET_OUT: |
5691 | return handle_packet_out(ofconn, oh); | |
064af421 | 5692 | |
982697a4 | 5693 | case OFPTYPE_PORT_MOD: |
d1e2cf21 | 5694 | return handle_port_mod(ofconn, oh); |
064af421 | 5695 | |
982697a4 | 5696 | case OFPTYPE_FLOW_MOD: |
2e4f5fcf | 5697 | return handle_flow_mod(ofconn, oh); |
064af421 | 5698 | |
7395c052 NZ |
5699 | case OFPTYPE_GROUP_MOD: |
5700 | return handle_group_mod(ofconn, oh); | |
5701 | ||
918f2b82 AZ |
5702 | case OFPTYPE_TABLE_MOD: |
5703 | return handle_table_mod(ofconn, oh); | |
5704 | ||
9cae45dc JR |
5705 | case OFPTYPE_METER_MOD: |
5706 | return handle_meter_mod(ofconn, oh); | |
5707 | ||
982697a4 | 5708 | case OFPTYPE_BARRIER_REQUEST: |
d1e2cf21 | 5709 | return handle_barrier_request(ofconn, oh); |
064af421 | 5710 | |
6ea4776b JR |
5711 | case OFPTYPE_ROLE_REQUEST: |
5712 | return handle_role_request(ofconn, oh); | |
5713 | ||
d1e2cf21 | 5714 | /* OpenFlow replies. */ |
982697a4 | 5715 | case OFPTYPE_ECHO_REPLY: |
d1e2cf21 | 5716 | return 0; |
246e61ea | 5717 | |
d1e2cf21 | 5718 | /* Nicira extension requests. */ |
982697a4 | 5719 | case OFPTYPE_FLOW_MOD_TABLE_ID: |
6c1491fb | 5720 | return handle_nxt_flow_mod_table_id(ofconn, oh); |
d1e2cf21 | 5721 | |
982697a4 | 5722 | case OFPTYPE_SET_FLOW_FORMAT: |
d1e2cf21 BP |
5723 | return handle_nxt_set_flow_format(ofconn, oh); |
5724 | ||
982697a4 | 5725 | case OFPTYPE_SET_PACKET_IN_FORMAT: |
54834960 EJ |
5726 | return handle_nxt_set_packet_in_format(ofconn, oh); |
5727 | ||
982697a4 | 5728 | case OFPTYPE_SET_CONTROLLER_ID: |
a7349929 BP |
5729 | return handle_nxt_set_controller_id(ofconn, oh); |
5730 | ||
982697a4 | 5731 | case OFPTYPE_FLOW_AGE: |
f27f2134 BP |
5732 | /* Nothing to do. */ |
5733 | return 0; | |
5734 | ||
982697a4 | 5735 | case OFPTYPE_FLOW_MONITOR_CANCEL: |
2b07c8b1 BP |
5736 | return handle_flow_monitor_cancel(ofconn, oh); |
5737 | ||
982697a4 | 5738 | case OFPTYPE_SET_ASYNC_CONFIG: |
80d5aefd BP |
5739 | return handle_nxt_set_async_config(ofconn, oh); |
5740 | ||
c423b3b3 AC |
5741 | case OFPTYPE_GET_ASYNC_REQUEST: |
5742 | return handle_nxt_get_async_request(ofconn, oh); | |
5743 | ||
349adfb2 | 5744 | /* Statistics requests. */ |
982697a4 BP |
5745 | case OFPTYPE_DESC_STATS_REQUEST: |
5746 | return handle_desc_stats_request(ofconn, oh); | |
5747 | ||
5748 | case OFPTYPE_FLOW_STATS_REQUEST: | |
5749 | return handle_flow_stats_request(ofconn, oh); | |
5750 | ||
5751 | case OFPTYPE_AGGREGATE_STATS_REQUEST: | |
5752 | return handle_aggregate_stats_request(ofconn, oh); | |
5753 | ||
5754 | case OFPTYPE_TABLE_STATS_REQUEST: | |
5755 | return handle_table_stats_request(ofconn, oh); | |
5756 | ||
5757 | case OFPTYPE_PORT_STATS_REQUEST: | |
5758 | return handle_port_stats_request(ofconn, oh); | |
5759 | ||
5760 | case OFPTYPE_QUEUE_STATS_REQUEST: | |
5761 | return handle_queue_stats_request(ofconn, oh); | |
5762 | ||
5763 | case OFPTYPE_PORT_DESC_STATS_REQUEST: | |
5764 | return handle_port_desc_stats_request(ofconn, oh); | |
5765 | ||
5766 | case OFPTYPE_FLOW_MONITOR_STATS_REQUEST: | |
5767 | return handle_flow_monitor_request(ofconn, oh); | |
5768 | ||
261bd854 BP |
5769 | case OFPTYPE_METER_STATS_REQUEST: |
5770 | case OFPTYPE_METER_CONFIG_STATS_REQUEST: | |
9cae45dc JR |
5771 | return handle_meter_request(ofconn, oh, type); |
5772 | ||
261bd854 | 5773 | case OFPTYPE_METER_FEATURES_STATS_REQUEST: |
9cae45dc JR |
5774 | return handle_meter_features_request(ofconn, oh); |
5775 | ||
261bd854 | 5776 | case OFPTYPE_GROUP_STATS_REQUEST: |
7395c052 NZ |
5777 | return handle_group_stats_request(ofconn, oh); |
5778 | ||
261bd854 | 5779 | case OFPTYPE_GROUP_DESC_STATS_REQUEST: |
7395c052 NZ |
5780 | return handle_group_desc_stats_request(ofconn, oh); |
5781 | ||
261bd854 | 5782 | case OFPTYPE_GROUP_FEATURES_STATS_REQUEST: |
7395c052 NZ |
5783 | return handle_group_features_stats_request(ofconn, oh); |
5784 | ||
5785 | /* FIXME: Change the following once they are implemented: */ | |
5786 | case OFPTYPE_QUEUE_GET_CONFIG_REQUEST: | |
261bd854 | 5787 | case OFPTYPE_TABLE_FEATURES_STATS_REQUEST: |
2e1ae200 JR |
5788 | return OFPERR_OFPBRC_BAD_TYPE; |
5789 | ||
982697a4 BP |
5790 | case OFPTYPE_HELLO: |
5791 | case OFPTYPE_ERROR: | |
5792 | case OFPTYPE_FEATURES_REPLY: | |
5793 | case OFPTYPE_GET_CONFIG_REPLY: | |
5794 | case OFPTYPE_PACKET_IN: | |
5795 | case OFPTYPE_FLOW_REMOVED: | |
5796 | case OFPTYPE_PORT_STATUS: | |
5797 | case OFPTYPE_BARRIER_REPLY: | |
c545d38d | 5798 | case OFPTYPE_QUEUE_GET_CONFIG_REPLY: |
982697a4 BP |
5799 | case OFPTYPE_DESC_STATS_REPLY: |
5800 | case OFPTYPE_FLOW_STATS_REPLY: | |
5801 | case OFPTYPE_QUEUE_STATS_REPLY: | |
5802 | case OFPTYPE_PORT_STATS_REPLY: | |
5803 | case OFPTYPE_TABLE_STATS_REPLY: | |
5804 | case OFPTYPE_AGGREGATE_STATS_REPLY: | |
5805 | case OFPTYPE_PORT_DESC_STATS_REPLY: | |
5806 | case OFPTYPE_ROLE_REPLY: | |
5807 | case OFPTYPE_FLOW_MONITOR_PAUSED: | |
5808 | case OFPTYPE_FLOW_MONITOR_RESUMED: | |
5809 | case OFPTYPE_FLOW_MONITOR_STATS_REPLY: | |
2e1ae200 | 5810 | case OFPTYPE_GET_ASYNC_REPLY: |
261bd854 BP |
5811 | case OFPTYPE_GROUP_STATS_REPLY: |
5812 | case OFPTYPE_GROUP_DESC_STATS_REPLY: | |
5813 | case OFPTYPE_GROUP_FEATURES_STATS_REPLY: | |
5814 | case OFPTYPE_METER_STATS_REPLY: | |
5815 | case OFPTYPE_METER_CONFIG_STATS_REPLY: | |
5816 | case OFPTYPE_METER_FEATURES_STATS_REPLY: | |
5817 | case OFPTYPE_TABLE_FEATURES_STATS_REPLY: | |
064af421 | 5818 | default: |
982697a4 | 5819 | return OFPERR_OFPBRC_BAD_TYPE; |
064af421 | 5820 | } |
d1e2cf21 | 5821 | } |
064af421 | 5822 | |
7ee20df1 | 5823 | static bool |
e03248b7 | 5824 | handle_openflow(struct ofconn *ofconn, const struct ofpbuf *ofp_msg) |
15aaf599 | 5825 | OVS_EXCLUDED(ofproto_mutex) |
d1e2cf21 BP |
5826 | { |
5827 | int error = handle_openflow__(ofconn, ofp_msg); | |
7ee20df1 | 5828 | if (error && error != OFPROTO_POSTPONE) { |
1be5ff75 | 5829 | ofconn_send_error(ofconn, ofp_msg->data, error); |
064af421 | 5830 | } |
d1e2cf21 | 5831 | COVERAGE_INC(ofproto_recv_openflow); |
7ee20df1 BP |
5832 | return error != OFPROTO_POSTPONE; |
5833 | } | |
5834 | \f | |
5835 | /* Asynchronous operations. */ | |
5836 | ||
5837 | /* Creates and returns a new ofopgroup that is not associated with any | |
5838 | * OpenFlow connection. | |
5839 | * | |
5840 | * The caller should add operations to the returned group with | |
5841 | * ofoperation_create() and then submit it with ofopgroup_submit(). */ | |
5842 | static struct ofopgroup * | |
7024dffe | 5843 | ofopgroup_create_unattached(struct ofproto *ofproto) |
15aaf599 | 5844 | OVS_REQUIRES(ofproto_mutex) |
7ee20df1 BP |
5845 | { |
5846 | struct ofopgroup *group = xzalloc(sizeof *group); | |
5847 | group->ofproto = ofproto; | |
5848 | list_init(&group->ofproto_node); | |
5849 | list_init(&group->ops); | |
5850 | list_init(&group->ofconn_node); | |
5851 | return group; | |
5852 | } | |
5853 | ||
7024dffe BP |
5854 | /* Creates and returns a new ofopgroup for 'ofproto'. |
5855 | * | |
5856 | * If 'ofconn' is NULL, the new ofopgroup is not associated with any OpenFlow | |
5857 | * connection. The 'request' and 'buffer_id' arguments are ignored. | |
5858 | * | |
5859 | * If 'ofconn' is nonnull, then the new ofopgroup is associated with 'ofconn'. | |
5860 | * If the ofopgroup eventually fails, then the error reply will include | |
5861 | * 'request'. If the ofopgroup eventually succeeds, then the packet with | |
5862 | * buffer id 'buffer_id' on 'ofconn' will be sent by 'ofconn''s ofproto. | |
7ee20df1 BP |
5863 | * |
5864 | * The caller should add operations to the returned group with | |
5865 | * ofoperation_create() and then submit it with ofopgroup_submit(). */ | |
5866 | static struct ofopgroup * | |
7024dffe BP |
5867 | ofopgroup_create(struct ofproto *ofproto, struct ofconn *ofconn, |
5868 | const struct ofp_header *request, uint32_t buffer_id) | |
15aaf599 | 5869 | OVS_REQUIRES(ofproto_mutex) |
7ee20df1 | 5870 | { |
7024dffe BP |
5871 | struct ofopgroup *group = ofopgroup_create_unattached(ofproto); |
5872 | if (ofconn) { | |
5873 | size_t request_len = ntohs(request->length); | |
7ee20df1 | 5874 | |
cb22974d | 5875 | ovs_assert(ofconn_get_ofproto(ofconn) == ofproto); |
7ee20df1 | 5876 | |
7024dffe BP |
5877 | ofconn_add_opgroup(ofconn, &group->ofconn_node); |
5878 | group->ofconn = ofconn; | |
5879 | group->request = xmemdup(request, MIN(request_len, 64)); | |
5880 | group->buffer_id = buffer_id; | |
5881 | } | |
7ee20df1 BP |
5882 | return group; |
5883 | } | |
5884 | ||
5885 | /* Submits 'group' for processing. | |
5886 | * | |
5887 | * If 'group' contains no operations (e.g. none were ever added, or all of the | |
5888 | * ones that were added completed synchronously), then it is destroyed | |
5889 | * immediately. Otherwise it is added to the ofproto's list of pending | |
5890 | * groups. */ | |
5891 | static void | |
5892 | ofopgroup_submit(struct ofopgroup *group) | |
15aaf599 | 5893 | OVS_REQUIRES(ofproto_mutex) |
7ee20df1 | 5894 | { |
e615b0a3 BP |
5895 | if (!group->n_running) { |
5896 | ofopgroup_complete(group); | |
7ee20df1 BP |
5897 | } else { |
5898 | list_push_back(&group->ofproto->pending, &group->ofproto_node); | |
dd5616b3 | 5899 | group->ofproto->n_pending++; |
7ee20df1 BP |
5900 | } |
5901 | } | |
5902 | ||
5903 | static void | |
e615b0a3 | 5904 | ofopgroup_complete(struct ofopgroup *group) |
15aaf599 | 5905 | OVS_REQUIRES(ofproto_mutex) |
7ee20df1 | 5906 | { |
e615b0a3 | 5907 | struct ofproto *ofproto = group->ofproto; |
2b07c8b1 BP |
5908 | |
5909 | struct ofconn *abbrev_ofconn; | |
5910 | ovs_be32 abbrev_xid; | |
5911 | ||
e615b0a3 BP |
5912 | struct ofoperation *op, *next_op; |
5913 | int error; | |
5914 | ||
cb22974d | 5915 | ovs_assert(!group->n_running); |
e615b0a3 BP |
5916 | |
5917 | error = 0; | |
5918 | LIST_FOR_EACH (op, group_node, &group->ops) { | |
5919 | if (op->error) { | |
5920 | error = op->error; | |
5921 | break; | |
5922 | } | |
5923 | } | |
5924 | ||
5925 | if (!error && group->ofconn && group->buffer_id != UINT32_MAX) { | |
5926 | LIST_FOR_EACH (op, group_node, &group->ops) { | |
5927 | if (op->type != OFOPERATION_DELETE) { | |
5928 | struct ofpbuf *packet; | |
4e022ec0 | 5929 | ofp_port_t in_port; |
e615b0a3 BP |
5930 | |
5931 | error = ofconn_pktbuf_retrieve(group->ofconn, group->buffer_id, | |
5932 | &packet, &in_port); | |
5933 | if (packet) { | |
35412852 BP |
5934 | struct rule_execute *re; |
5935 | ||
cb22974d | 5936 | ovs_assert(!error); |
35412852 BP |
5937 | |
5938 | ofproto_rule_ref(op->rule); | |
5939 | ||
5940 | re = xmalloc(sizeof *re); | |
5941 | re->rule = op->rule; | |
5942 | re->in_port = in_port; | |
5943 | re->packet = packet; | |
5944 | ||
5945 | if (!guarded_list_push_back(&ofproto->rule_executes, | |
5946 | &re->list_node, 1024)) { | |
5947 | ofproto_rule_unref(op->rule); | |
5948 | ofpbuf_delete(re->packet); | |
5949 | free(re); | |
5950 | } | |
e615b0a3 BP |
5951 | } |
5952 | break; | |
5953 | } | |
5954 | } | |
5955 | } | |
5956 | ||
2b07c8b1 BP |
5957 | if (!error && !list_is_empty(&group->ofconn_node)) { |
5958 | abbrev_ofconn = group->ofconn; | |
5959 | abbrev_xid = group->request->xid; | |
5960 | } else { | |
5961 | abbrev_ofconn = NULL; | |
5962 | abbrev_xid = htonl(0); | |
5963 | } | |
e615b0a3 BP |
5964 | LIST_FOR_EACH_SAFE (op, next_op, group_node, &group->ops) { |
5965 | struct rule *rule = op->rule; | |
2b07c8b1 | 5966 | |
644e2a7c BP |
5967 | /* We generally want to report the change to active OpenFlow flow |
5968 | monitors (e.g. NXST_FLOW_MONITOR). There are three exceptions: | |
5969 | ||
5970 | - The operation failed. | |
5971 | ||
5972 | - The affected rule is not visible to controllers. | |
5973 | ||
5974 | - The operation's only effect was to update rule->modified. */ | |
5975 | if (!(op->error | |
5976 | || ofproto_rule_is_hidden(rule) | |
5977 | || (op->type == OFOPERATION_MODIFY | |
6f00e29b | 5978 | && op->actions |
644e2a7c | 5979 | && rule->flow_cookie == op->flow_cookie))) { |
2b07c8b1 BP |
5980 | /* Check that we can just cast from ofoperation_type to |
5981 | * nx_flow_update_event. */ | |
b277c7cc BP |
5982 | enum nx_flow_update_event event_type; |
5983 | ||
5984 | switch (op->type) { | |
5985 | case OFOPERATION_ADD: | |
5986 | case OFOPERATION_REPLACE: | |
5987 | event_type = NXFME_ADDED; | |
5988 | break; | |
5989 | ||
5990 | case OFOPERATION_DELETE: | |
5991 | event_type = NXFME_DELETED; | |
5992 | break; | |
5993 | ||
5994 | case OFOPERATION_MODIFY: | |
5995 | event_type = NXFME_MODIFIED; | |
5996 | break; | |
5997 | ||
5998 | default: | |
5999 | NOT_REACHED(); | |
6000 | } | |
6001 | ||
6002 | ofmonitor_report(ofproto->connmgr, rule, event_type, | |
2b07c8b1 BP |
6003 | op->reason, abbrev_ofconn, abbrev_xid); |
6004 | } | |
6005 | ||
e615b0a3 BP |
6006 | rule->pending = NULL; |
6007 | ||
6008 | switch (op->type) { | |
6009 | case OFOPERATION_ADD: | |
6010 | if (!op->error) { | |
5cb7a798 BP |
6011 | uint16_t vid_mask; |
6012 | ||
5cb7a798 BP |
6013 | vid_mask = minimask_get_vid_mask(&rule->cr.match.mask); |
6014 | if (vid_mask == VLAN_VID_MASK) { | |
e615b0a3 | 6015 | if (ofproto->vlan_bitmap) { |
5cb7a798 | 6016 | uint16_t vid = miniflow_get_vid(&rule->cr.match.flow); |
e615b0a3 BP |
6017 | if (!bitmap_is_set(ofproto->vlan_bitmap, vid)) { |
6018 | bitmap_set1(ofproto->vlan_bitmap, vid); | |
6019 | ofproto->vlans_changed = true; | |
6020 | } | |
6021 | } else { | |
6022 | ofproto->vlans_changed = true; | |
6023 | } | |
6024 | } | |
6025 | } else { | |
b277c7cc | 6026 | oftable_remove_rule(rule); |
a2143702 | 6027 | ofproto_rule_unref(rule); |
e615b0a3 BP |
6028 | } |
6029 | break; | |
6030 | ||
6031 | case OFOPERATION_DELETE: | |
cb22974d | 6032 | ovs_assert(!op->error); |
a2143702 | 6033 | ofproto_rule_unref(rule); |
e615b0a3 BP |
6034 | op->rule = NULL; |
6035 | break; | |
6036 | ||
6037 | case OFOPERATION_MODIFY: | |
b277c7cc | 6038 | case OFOPERATION_REPLACE: |
e615b0a3 | 6039 | if (!op->error) { |
b277c7cc BP |
6040 | long long int now = time_msec(); |
6041 | ||
6042 | rule->modified = now; | |
6043 | if (op->type == OFOPERATION_REPLACE) { | |
6044 | rule->created = rule->used = now; | |
6045 | } | |
e615b0a3 | 6046 | } else { |
98eaac36 | 6047 | ofproto_rule_change_cookie(ofproto, rule, op->flow_cookie); |
d10976b7 | 6048 | ovs_mutex_lock(&rule->mutex); |
b277c7cc BP |
6049 | rule->idle_timeout = op->idle_timeout; |
6050 | rule->hard_timeout = op->hard_timeout; | |
d10976b7 | 6051 | ovs_mutex_unlock(&rule->mutex); |
6f00e29b BP |
6052 | if (op->actions) { |
6053 | struct rule_actions *old_actions; | |
884e1dc4 | 6054 | |
c7c9a7c8 | 6055 | ovs_mutex_lock(&rule->mutex); |
6f00e29b BP |
6056 | old_actions = rule->actions; |
6057 | rule->actions = op->actions; | |
c7c9a7c8 | 6058 | ovs_mutex_unlock(&rule->mutex); |
884e1dc4 | 6059 | |
6f00e29b BP |
6060 | op->actions = NULL; |
6061 | rule_actions_unref(old_actions); | |
08043761 | 6062 | } |
3f517bcd | 6063 | rule->flags = op->flags; |
e615b0a3 BP |
6064 | } |
6065 | break; | |
6066 | ||
6067 | default: | |
6068 | NOT_REACHED(); | |
6069 | } | |
6070 | ||
6071 | ofoperation_destroy(op); | |
6072 | } | |
6073 | ||
2b07c8b1 BP |
6074 | ofmonitor_flush(ofproto->connmgr); |
6075 | ||
7ee20df1 | 6076 | if (!list_is_empty(&group->ofproto_node)) { |
cb22974d | 6077 | ovs_assert(ofproto->n_pending > 0); |
e615b0a3 | 6078 | ofproto->n_pending--; |
7ee20df1 BP |
6079 | list_remove(&group->ofproto_node); |
6080 | } | |
6081 | if (!list_is_empty(&group->ofconn_node)) { | |
6082 | list_remove(&group->ofconn_node); | |
e615b0a3 BP |
6083 | if (error) { |
6084 | ofconn_send_error(group->ofconn, group->request, error); | |
7ee20df1 | 6085 | } |
e615b0a3 | 6086 | connmgr_retry(ofproto->connmgr); |
7ee20df1 BP |
6087 | } |
6088 | free(group->request); | |
6089 | free(group); | |
6090 | } | |
6091 | ||
6092 | /* Initiates a new operation on 'rule', of the specified 'type', within | |
a7d94793 BP |
6093 | * 'group'. Prior to calling, 'rule' must not have any pending operation. |
6094 | * | |
2b07c8b1 BP |
6095 | * For a 'type' of OFOPERATION_DELETE, 'reason' should specify the reason that |
6096 | * the flow is being deleted. For other 'type's, 'reason' is ignored (use 0). | |
6097 | * | |
a7d94793 BP |
6098 | * Returns the newly created ofoperation (which is also available as |
6099 | * rule->pending). */ | |
6100 | static struct ofoperation * | |
7ee20df1 | 6101 | ofoperation_create(struct ofopgroup *group, struct rule *rule, |
2b07c8b1 BP |
6102 | enum ofoperation_type type, |
6103 | enum ofp_flow_removed_reason reason) | |
15aaf599 | 6104 | OVS_REQUIRES(ofproto_mutex) |
7ee20df1 | 6105 | { |
a5b8d268 | 6106 | struct ofproto *ofproto = group->ofproto; |
7ee20df1 BP |
6107 | struct ofoperation *op; |
6108 | ||
cb22974d | 6109 | ovs_assert(!rule->pending); |
7ee20df1 BP |
6110 | |
6111 | op = rule->pending = xzalloc(sizeof *op); | |
6112 | op->group = group; | |
6113 | list_push_back(&group->ops, &op->group_node); | |
6114 | op->rule = rule; | |
6115 | op->type = type; | |
2b07c8b1 | 6116 | op->reason = reason; |
7ee20df1 | 6117 | op->flow_cookie = rule->flow_cookie; |
d10976b7 | 6118 | ovs_mutex_lock(&rule->mutex); |
b277c7cc BP |
6119 | op->idle_timeout = rule->idle_timeout; |
6120 | op->hard_timeout = rule->hard_timeout; | |
d10976b7 | 6121 | ovs_mutex_unlock(&rule->mutex); |
3f517bcd | 6122 | op->flags = rule->flags; |
7ee20df1 | 6123 | |
e615b0a3 BP |
6124 | group->n_running++; |
6125 | ||
7ee20df1 | 6126 | if (type == OFOPERATION_DELETE) { |
a5b8d268 | 6127 | hmap_insert(&ofproto->deletions, &op->hmap_node, |
7ee20df1 BP |
6128 | cls_rule_hash(&rule->cr, rule->table_id)); |
6129 | } | |
a7d94793 BP |
6130 | |
6131 | return op; | |
7ee20df1 BP |
6132 | } |
6133 | ||
6134 | static void | |
6135 | ofoperation_destroy(struct ofoperation *op) | |
15aaf599 | 6136 | OVS_REQUIRES(ofproto_mutex) |
7ee20df1 BP |
6137 | { |
6138 | struct ofopgroup *group = op->group; | |
6139 | ||
6140 | if (op->rule) { | |
6141 | op->rule->pending = NULL; | |
6142 | } | |
6143 | if (op->type == OFOPERATION_DELETE) { | |
6144 | hmap_remove(&group->ofproto->deletions, &op->hmap_node); | |
6145 | } | |
6146 | list_remove(&op->group_node); | |
6f00e29b | 6147 | rule_actions_unref(op->actions); |
7ee20df1 | 6148 | free(op); |
7ee20df1 BP |
6149 | } |
6150 | ||
6151 | /* Indicates that 'op' completed with status 'error', which is either 0 to | |
90bf1e07 | 6152 | * indicate success or an OpenFlow error code on failure. |
7ee20df1 | 6153 | * |
a6a62132 | 6154 | * If 'error' is 0, indicating success, the operation will be committed |
b277c7cc | 6155 | * permanently to the flow table. |
a6a62132 BP |
6156 | * |
6157 | * If 'error' is nonzero, then generally the operation will be rolled back: | |
6158 | * | |
6159 | * - If 'op' is an "add flow" operation, ofproto removes the new rule or | |
6160 | * restores the original rule. The caller must have uninitialized any | |
6161 | * derived state in the new rule, as in step 5 of in the "Life Cycle" in | |
6162 | * ofproto/ofproto-provider.h. ofoperation_complete() performs steps 6 and | |
6163 | * and 7 for the new rule, calling its ->rule_dealloc() function. | |
6164 | * | |
6165 | * - If 'op' is a "modify flow" operation, ofproto restores the original | |
6166 | * actions. | |
6167 | * | |
6168 | * - 'op' must not be a "delete flow" operation. Removing a rule is not | |
6169 | * allowed to fail. It must always succeed. | |
7ee20df1 | 6170 | * |
5bee6e26 JP |
6171 | * Please see the large comment in ofproto/ofproto-provider.h titled |
6172 | * "Asynchronous Operation Support" for more information. */ | |
7ee20df1 | 6173 | void |
90bf1e07 | 6174 | ofoperation_complete(struct ofoperation *op, enum ofperr error) |
7ee20df1 BP |
6175 | { |
6176 | struct ofopgroup *group = op->group; | |
7ee20df1 | 6177 | |
cb22974d BP |
6178 | ovs_assert(group->n_running > 0); |
6179 | ovs_assert(!error || op->type != OFOPERATION_DELETE); | |
9075907c | 6180 | |
e615b0a3 BP |
6181 | op->error = error; |
6182 | if (!--group->n_running && !list_is_empty(&group->ofproto_node)) { | |
15aaf599 BP |
6183 | /* This function can be called from ->rule_construct(), in which case |
6184 | * ofproto_mutex is held, or it can be called from ->run(), in which | |
6185 | * case ofproto_mutex is not held. But only in the latter case can we | |
6186 | * arrive here, so we can safely take ofproto_mutex now. */ | |
6187 | ovs_mutex_lock(&ofproto_mutex); | |
6188 | ovs_assert(op->rule->pending == op); | |
e615b0a3 | 6189 | ofopgroup_complete(group); |
15aaf599 | 6190 | ovs_mutex_unlock(&ofproto_mutex); |
7ee20df1 | 6191 | } |
7ee20df1 | 6192 | } |
064af421 | 6193 | \f |
064af421 | 6194 | static uint64_t |
fa60c019 | 6195 | pick_datapath_id(const struct ofproto *ofproto) |
064af421 | 6196 | { |
fa60c019 | 6197 | const struct ofport *port; |
064af421 | 6198 | |
abe529af | 6199 | port = ofproto_get_port(ofproto, OFPP_LOCAL); |
fa60c019 BP |
6200 | if (port) { |
6201 | uint8_t ea[ETH_ADDR_LEN]; | |
6202 | int error; | |
6203 | ||
6204 | error = netdev_get_etheraddr(port->netdev, ea); | |
064af421 BP |
6205 | if (!error) { |
6206 | return eth_addr_to_uint64(ea); | |
6207 | } | |
fbfa2911 BP |
6208 | VLOG_WARN("%s: could not get MAC address for %s (%s)", |
6209 | ofproto->name, netdev_get_name(port->netdev), | |
10a89ef0 | 6210 | ovs_strerror(error)); |
064af421 | 6211 | } |
fa60c019 | 6212 | return ofproto->fallback_dpid; |
064af421 BP |
6213 | } |
6214 | ||
6215 | static uint64_t | |
6216 | pick_fallback_dpid(void) | |
6217 | { | |
6218 | uint8_t ea[ETH_ADDR_LEN]; | |
70150daf | 6219 | eth_addr_nicira_random(ea); |
064af421 BP |
6220 | return eth_addr_to_uint64(ea); |
6221 | } | |
6222 | \f | |
254750ce BP |
6223 | /* Table overflow policy. */ |
6224 | ||
ad3efdcb EJ |
6225 | /* Chooses and updates 'rulep' with a rule to evict from 'table'. Sets 'rulep' |
6226 | * to NULL if the table is not configured to evict rules or if the table | |
6227 | * contains no evictable rules. (Rules with a readlock on their evict rwlock, | |
6228 | * or with no timeouts are not evictable.) */ | |
6229 | static bool | |
6230 | choose_rule_to_evict(struct oftable *table, struct rule **rulep) | |
15aaf599 | 6231 | OVS_REQUIRES(ofproto_mutex) |
254750ce BP |
6232 | { |
6233 | struct eviction_group *evg; | |
6234 | ||
ad3efdcb | 6235 | *rulep = NULL; |
254750ce | 6236 | if (!table->eviction_fields) { |
ad3efdcb | 6237 | return false; |
254750ce BP |
6238 | } |
6239 | ||
6240 | /* In the common case, the outer and inner loops here will each be entered | |
6241 | * exactly once: | |
6242 | * | |
6243 | * - The inner loop normally "return"s in its first iteration. If the | |
6244 | * eviction group has any evictable rules, then it always returns in | |
6245 | * some iteration. | |
6246 | * | |
6247 | * - The outer loop only iterates more than once if the largest eviction | |
6248 | * group has no evictable rules. | |
6249 | * | |
6250 | * - The outer loop can exit only if table's 'max_flows' is all filled up | |
afe2143d | 6251 | * by unevictable rules. */ |
254750ce BP |
6252 | HEAP_FOR_EACH (evg, size_node, &table->eviction_groups_by_size) { |
6253 | struct rule *rule; | |
6254 | ||
6255 | HEAP_FOR_EACH (rule, evg_node, &evg->rules) { | |
15aaf599 BP |
6256 | *rulep = rule; |
6257 | return true; | |
254750ce BP |
6258 | } |
6259 | } | |
6260 | ||
ad3efdcb | 6261 | return false; |
254750ce BP |
6262 | } |
6263 | ||
6264 | /* Searches 'ofproto' for tables that have more flows than their configured | |
6265 | * maximum and that have flow eviction enabled, and evicts as many flows as | |
6266 | * necessary and currently feasible from them. | |
6267 | * | |
6268 | * This triggers only when an OpenFlow table has N flows in it and then the | |
6269 | * client configures a maximum number of flows less than N. */ | |
6270 | static void | |
6271 | ofproto_evict(struct ofproto *ofproto) | |
6272 | { | |
254750ce BP |
6273 | struct oftable *table; |
6274 | ||
15aaf599 | 6275 | ovs_mutex_lock(&ofproto_mutex); |
254750ce | 6276 | OFPROTO_FOR_EACH_TABLE (table, ofproto) { |
2e31a9b8 | 6277 | evict_rules_from_table(ofproto, table, 0); |
254750ce | 6278 | } |
15aaf599 | 6279 | ovs_mutex_unlock(&ofproto_mutex); |
254750ce BP |
6280 | } |
6281 | \f | |
6282 | /* Eviction groups. */ | |
6283 | ||
6284 | /* Returns the priority to use for an eviction_group that contains 'n_rules' | |
6285 | * rules. The priority contains low-order random bits to ensure that eviction | |
6286 | * groups with the same number of rules are prioritized randomly. */ | |
6287 | static uint32_t | |
6288 | eviction_group_priority(size_t n_rules) | |
6289 | { | |
6290 | uint16_t size = MIN(UINT16_MAX, n_rules); | |
6291 | return (size << 16) | random_uint16(); | |
6292 | } | |
6293 | ||
6294 | /* Updates 'evg', an eviction_group within 'table', following a change that | |
6295 | * adds or removes rules in 'evg'. */ | |
6296 | static void | |
6297 | eviction_group_resized(struct oftable *table, struct eviction_group *evg) | |
15aaf599 | 6298 | OVS_REQUIRES(ofproto_mutex) |
254750ce BP |
6299 | { |
6300 | heap_change(&table->eviction_groups_by_size, &evg->size_node, | |
6301 | eviction_group_priority(heap_count(&evg->rules))); | |
6302 | } | |
6303 | ||
6304 | /* Destroys 'evg', an eviction_group within 'table': | |
6305 | * | |
6306 | * - Removes all the rules, if any, from 'evg'. (It doesn't destroy the | |
6307 | * rules themselves, just removes them from the eviction group.) | |
6308 | * | |
6309 | * - Removes 'evg' from 'table'. | |
6310 | * | |
6311 | * - Frees 'evg'. */ | |
6312 | static void | |
6313 | eviction_group_destroy(struct oftable *table, struct eviction_group *evg) | |
15aaf599 | 6314 | OVS_REQUIRES(ofproto_mutex) |
254750ce BP |
6315 | { |
6316 | while (!heap_is_empty(&evg->rules)) { | |
6317 | struct rule *rule; | |
6318 | ||
6319 | rule = CONTAINER_OF(heap_pop(&evg->rules), struct rule, evg_node); | |
6320 | rule->eviction_group = NULL; | |
6321 | } | |
6322 | hmap_remove(&table->eviction_groups_by_id, &evg->id_node); | |
6323 | heap_remove(&table->eviction_groups_by_size, &evg->size_node); | |
6324 | heap_destroy(&evg->rules); | |
6325 | free(evg); | |
6326 | } | |
6327 | ||
6328 | /* Removes 'rule' from its eviction group, if any. */ | |
6329 | static void | |
6330 | eviction_group_remove_rule(struct rule *rule) | |
15aaf599 | 6331 | OVS_REQUIRES(ofproto_mutex) |
254750ce BP |
6332 | { |
6333 | if (rule->eviction_group) { | |
6334 | struct oftable *table = &rule->ofproto->tables[rule->table_id]; | |
6335 | struct eviction_group *evg = rule->eviction_group; | |
6336 | ||
6337 | rule->eviction_group = NULL; | |
6338 | heap_remove(&evg->rules, &rule->evg_node); | |
6339 | if (heap_is_empty(&evg->rules)) { | |
6340 | eviction_group_destroy(table, evg); | |
6341 | } else { | |
6342 | eviction_group_resized(table, evg); | |
6343 | } | |
6344 | } | |
6345 | } | |
6346 | ||
6347 | /* Hashes the 'rule''s values for the eviction_fields of 'rule''s table, and | |
6348 | * returns the hash value. */ | |
6349 | static uint32_t | |
6350 | eviction_group_hash_rule(struct rule *rule) | |
15aaf599 | 6351 | OVS_REQUIRES(ofproto_mutex) |
254750ce BP |
6352 | { |
6353 | struct oftable *table = &rule->ofproto->tables[rule->table_id]; | |
6354 | const struct mf_subfield *sf; | |
5cb7a798 | 6355 | struct flow flow; |
254750ce BP |
6356 | uint32_t hash; |
6357 | ||
6358 | hash = table->eviction_group_id_basis; | |
5cb7a798 | 6359 | miniflow_expand(&rule->cr.match.flow, &flow); |
254750ce BP |
6360 | for (sf = table->eviction_fields; |
6361 | sf < &table->eviction_fields[table->n_eviction_fields]; | |
6362 | sf++) | |
6363 | { | |
5cb7a798 | 6364 | if (mf_are_prereqs_ok(sf->field, &flow)) { |
254750ce BP |
6365 | union mf_value value; |
6366 | ||
5cb7a798 | 6367 | mf_get_value(sf->field, &flow, &value); |
254750ce BP |
6368 | if (sf->ofs) { |
6369 | bitwise_zero(&value, sf->field->n_bytes, 0, sf->ofs); | |
6370 | } | |
6371 | if (sf->ofs + sf->n_bits < sf->field->n_bytes * 8) { | |
6372 | unsigned int start = sf->ofs + sf->n_bits; | |
6373 | bitwise_zero(&value, sf->field->n_bytes, start, | |
6374 | sf->field->n_bytes * 8 - start); | |
6375 | } | |
6376 | hash = hash_bytes(&value, sf->field->n_bytes, hash); | |
6377 | } else { | |
6378 | hash = hash_int(hash, 0); | |
6379 | } | |
6380 | } | |
6381 | ||
6382 | return hash; | |
6383 | } | |
6384 | ||
6385 | /* Returns an eviction group within 'table' with the given 'id', creating one | |
6386 | * if necessary. */ | |
6387 | static struct eviction_group * | |
6388 | eviction_group_find(struct oftable *table, uint32_t id) | |
15aaf599 | 6389 | OVS_REQUIRES(ofproto_mutex) |
254750ce BP |
6390 | { |
6391 | struct eviction_group *evg; | |
6392 | ||
6393 | HMAP_FOR_EACH_WITH_HASH (evg, id_node, id, &table->eviction_groups_by_id) { | |
6394 | return evg; | |
6395 | } | |
6396 | ||
6397 | evg = xmalloc(sizeof *evg); | |
6398 | hmap_insert(&table->eviction_groups_by_id, &evg->id_node, id); | |
6399 | heap_insert(&table->eviction_groups_by_size, &evg->size_node, | |
6400 | eviction_group_priority(0)); | |
6401 | heap_init(&evg->rules); | |
6402 | ||
6403 | return evg; | |
6404 | } | |
6405 | ||
6406 | /* Returns an eviction priority for 'rule'. The return value should be | |
6407 | * interpreted so that higher priorities make a rule more attractive candidates | |
6408 | * for eviction. */ | |
6409 | static uint32_t | |
6410 | rule_eviction_priority(struct rule *rule) | |
15aaf599 | 6411 | OVS_REQUIRES(ofproto_mutex) |
254750ce BP |
6412 | { |
6413 | long long int hard_expiration; | |
6414 | long long int idle_expiration; | |
6415 | long long int expiration; | |
6416 | uint32_t expiration_offset; | |
6417 | ||
6418 | /* Calculate time of expiration. */ | |
d10976b7 | 6419 | ovs_mutex_lock(&rule->mutex); |
254750ce BP |
6420 | hard_expiration = (rule->hard_timeout |
6421 | ? rule->modified + rule->hard_timeout * 1000 | |
6422 | : LLONG_MAX); | |
6423 | idle_expiration = (rule->idle_timeout | |
6424 | ? rule->used + rule->idle_timeout * 1000 | |
6425 | : LLONG_MAX); | |
6426 | expiration = MIN(hard_expiration, idle_expiration); | |
d10976b7 | 6427 | ovs_mutex_unlock(&rule->mutex); |
254750ce BP |
6428 | if (expiration == LLONG_MAX) { |
6429 | return 0; | |
6430 | } | |
6431 | ||
6432 | /* Calculate the time of expiration as a number of (approximate) seconds | |
6433 | * after program startup. | |
6434 | * | |
6435 | * This should work OK for program runs that last UINT32_MAX seconds or | |
6436 | * less. Therefore, please restart OVS at least once every 136 years. */ | |
6437 | expiration_offset = (expiration >> 10) - (time_boot_msec() >> 10); | |
6438 | ||
6439 | /* Invert the expiration offset because we're using a max-heap. */ | |
6440 | return UINT32_MAX - expiration_offset; | |
6441 | } | |
6442 | ||
6443 | /* Adds 'rule' to an appropriate eviction group for its oftable's | |
6444 | * configuration. Does nothing if 'rule''s oftable doesn't have eviction | |
6445 | * enabled, or if 'rule' is a permanent rule (one that will never expire on its | |
6446 | * own). | |
6447 | * | |
6448 | * The caller must ensure that 'rule' is not already in an eviction group. */ | |
6449 | static void | |
6450 | eviction_group_add_rule(struct rule *rule) | |
15aaf599 | 6451 | OVS_REQUIRES(ofproto_mutex) |
254750ce BP |
6452 | { |
6453 | struct ofproto *ofproto = rule->ofproto; | |
6454 | struct oftable *table = &ofproto->tables[rule->table_id]; | |
a3779dbc | 6455 | bool has_timeout; |
254750ce | 6456 | |
d10976b7 | 6457 | ovs_mutex_lock(&rule->mutex); |
a3779dbc | 6458 | has_timeout = rule->hard_timeout || rule->idle_timeout; |
d10976b7 | 6459 | ovs_mutex_unlock(&rule->mutex); |
a3779dbc EJ |
6460 | |
6461 | if (table->eviction_fields && has_timeout) { | |
254750ce BP |
6462 | struct eviction_group *evg; |
6463 | ||
6464 | evg = eviction_group_find(table, eviction_group_hash_rule(rule)); | |
6465 | ||
6466 | rule->eviction_group = evg; | |
6467 | heap_insert(&evg->rules, &rule->evg_node, | |
6468 | rule_eviction_priority(rule)); | |
6469 | eviction_group_resized(table, evg); | |
6470 | } | |
6471 | } | |
6472 | \f | |
d0918789 BP |
6473 | /* oftables. */ |
6474 | ||
6475 | /* Initializes 'table'. */ | |
6476 | static void | |
6477 | oftable_init(struct oftable *table) | |
6478 | { | |
5c67e4af | 6479 | memset(table, 0, sizeof *table); |
d0918789 | 6480 | classifier_init(&table->cls); |
ec1c5c7e | 6481 | table->max_flows = UINT_MAX; |
d0918789 BP |
6482 | } |
6483 | ||
254750ce | 6484 | /* Destroys 'table', including its classifier and eviction groups. |
d0918789 BP |
6485 | * |
6486 | * The caller is responsible for freeing 'table' itself. */ | |
6487 | static void | |
6488 | oftable_destroy(struct oftable *table) | |
6489 | { | |
0b4f2078 | 6490 | ovs_rwlock_rdlock(&table->cls.rwlock); |
cb22974d | 6491 | ovs_assert(classifier_is_empty(&table->cls)); |
0b4f2078 | 6492 | ovs_rwlock_unlock(&table->cls.rwlock); |
254750ce | 6493 | oftable_disable_eviction(table); |
d0918789 | 6494 | classifier_destroy(&table->cls); |
254750ce BP |
6495 | free(table->name); |
6496 | } | |
6497 | ||
6498 | /* Changes the name of 'table' to 'name'. If 'name' is NULL or the empty | |
6499 | * string, then 'table' will use its default name. | |
6500 | * | |
6501 | * This only affects the name exposed for a table exposed through the OpenFlow | |
6502 | * OFPST_TABLE (as printed by "ovs-ofctl dump-tables"). */ | |
6503 | static void | |
6504 | oftable_set_name(struct oftable *table, const char *name) | |
6505 | { | |
6506 | if (name && name[0]) { | |
6507 | int len = strnlen(name, OFP_MAX_TABLE_NAME_LEN); | |
6508 | if (!table->name || strncmp(name, table->name, len)) { | |
6509 | free(table->name); | |
6510 | table->name = xmemdup0(name, len); | |
6511 | } | |
6512 | } else { | |
6513 | free(table->name); | |
6514 | table->name = NULL; | |
6515 | } | |
6516 | } | |
6517 | ||
6518 | /* oftables support a choice of two policies when adding a rule would cause the | |
6519 | * number of flows in the table to exceed the configured maximum number: either | |
6520 | * they can refuse to add the new flow or they can evict some existing flow. | |
6521 | * This function configures the former policy on 'table'. */ | |
6522 | static void | |
6523 | oftable_disable_eviction(struct oftable *table) | |
15aaf599 | 6524 | OVS_REQUIRES(ofproto_mutex) |
254750ce BP |
6525 | { |
6526 | if (table->eviction_fields) { | |
6527 | struct eviction_group *evg, *next; | |
6528 | ||
6529 | HMAP_FOR_EACH_SAFE (evg, next, id_node, | |
6530 | &table->eviction_groups_by_id) { | |
6531 | eviction_group_destroy(table, evg); | |
6532 | } | |
6533 | hmap_destroy(&table->eviction_groups_by_id); | |
6534 | heap_destroy(&table->eviction_groups_by_size); | |
6535 | ||
6536 | free(table->eviction_fields); | |
6537 | table->eviction_fields = NULL; | |
6538 | table->n_eviction_fields = 0; | |
6539 | } | |
6540 | } | |
6541 | ||
6542 | /* oftables support a choice of two policies when adding a rule would cause the | |
6543 | * number of flows in the table to exceed the configured maximum number: either | |
6544 | * they can refuse to add the new flow or they can evict some existing flow. | |
6545 | * This function configures the latter policy on 'table', with fairness based | |
6546 | * on the values of the 'n_fields' fields specified in 'fields'. (Specifying | |
6547 | * 'n_fields' as 0 disables fairness.) */ | |
6548 | static void | |
6549 | oftable_enable_eviction(struct oftable *table, | |
6550 | const struct mf_subfield *fields, size_t n_fields) | |
15aaf599 | 6551 | OVS_REQUIRES(ofproto_mutex) |
254750ce BP |
6552 | { |
6553 | struct cls_cursor cursor; | |
6554 | struct rule *rule; | |
6555 | ||
6556 | if (table->eviction_fields | |
6557 | && n_fields == table->n_eviction_fields | |
6558 | && (!n_fields | |
6559 | || !memcmp(fields, table->eviction_fields, | |
6560 | n_fields * sizeof *fields))) { | |
6561 | /* No change. */ | |
6562 | return; | |
6563 | } | |
6564 | ||
6565 | oftable_disable_eviction(table); | |
6566 | ||
6567 | table->n_eviction_fields = n_fields; | |
6568 | table->eviction_fields = xmemdup(fields, n_fields * sizeof *fields); | |
6569 | ||
6570 | table->eviction_group_id_basis = random_uint32(); | |
6571 | hmap_init(&table->eviction_groups_by_id); | |
6572 | heap_init(&table->eviction_groups_by_size); | |
6573 | ||
0b4f2078 | 6574 | ovs_rwlock_rdlock(&table->cls.rwlock); |
254750ce BP |
6575 | cls_cursor_init(&cursor, &table->cls, NULL); |
6576 | CLS_CURSOR_FOR_EACH (rule, cr, &cursor) { | |
6577 | eviction_group_add_rule(rule); | |
6578 | } | |
0b4f2078 | 6579 | ovs_rwlock_unlock(&table->cls.rwlock); |
d0918789 BP |
6580 | } |
6581 | ||
6582 | /* Removes 'rule' from the oftable that contains it. */ | |
6583 | static void | |
8b81d1ef | 6584 | oftable_remove_rule__(struct ofproto *ofproto, struct rule *rule) |
15aaf599 | 6585 | OVS_REQUIRES(ofproto_mutex) |
d0918789 | 6586 | { |
8b81d1ef BP |
6587 | struct classifier *cls = &ofproto->tables[rule->table_id].cls; |
6588 | ||
15aaf599 | 6589 | ovs_rwlock_wrlock(&cls->rwlock); |
49a0e0eb | 6590 | classifier_remove(cls, CONST_CAST(struct cls_rule *, &rule->cr)); |
15aaf599 | 6591 | ovs_rwlock_unlock(&cls->rwlock); |
2c916028 | 6592 | |
98eaac36 | 6593 | cookies_remove(ofproto, rule); |
2c916028 | 6594 | |
254750ce | 6595 | eviction_group_remove_rule(rule); |
e503cc19 SH |
6596 | if (!list_is_empty(&rule->expirable)) { |
6597 | list_remove(&rule->expirable); | |
6598 | } | |
9cae45dc JR |
6599 | if (!list_is_empty(&rule->meter_list_node)) { |
6600 | list_remove(&rule->meter_list_node); | |
51b50dfd | 6601 | list_init(&rule->meter_list_node); |
9cae45dc | 6602 | } |
d0918789 BP |
6603 | } |
6604 | ||
0b4f2078 EJ |
6605 | static void |
6606 | oftable_remove_rule(struct rule *rule) | |
15aaf599 | 6607 | OVS_REQUIRES(ofproto_mutex) |
0b4f2078 | 6608 | { |
8b81d1ef | 6609 | oftable_remove_rule__(rule->ofproto, rule); |
0b4f2078 EJ |
6610 | } |
6611 | ||
b277c7cc BP |
6612 | /* Inserts 'rule' into its oftable, which must not already contain any rule for |
6613 | * the same cls_rule. */ | |
6614 | static void | |
6615 | oftable_insert_rule(struct rule *rule) | |
15aaf599 | 6616 | OVS_REQUIRES(ofproto_mutex) |
d0918789 BP |
6617 | { |
6618 | struct ofproto *ofproto = rule->ofproto; | |
6619 | struct oftable *table = &ofproto->tables[rule->table_id]; | |
a3779dbc EJ |
6620 | bool may_expire; |
6621 | ||
d10976b7 | 6622 | ovs_mutex_lock(&rule->mutex); |
a3779dbc | 6623 | may_expire = rule->hard_timeout || rule->idle_timeout; |
d10976b7 | 6624 | ovs_mutex_unlock(&rule->mutex); |
e503cc19 SH |
6625 | |
6626 | if (may_expire) { | |
6627 | list_insert(&ofproto->expirable, &rule->expirable); | |
6628 | } | |
2c916028 | 6629 | |
98eaac36 | 6630 | cookies_insert(ofproto, rule); |
6f00e29b | 6631 | |
65efd2ab JR |
6632 | if (rule->actions->provider_meter_id != UINT32_MAX) { |
6633 | uint32_t meter_id = ofpacts_get_meter(rule->actions->ofpacts, | |
6634 | rule->actions->ofpacts_len); | |
6635 | struct meter *meter = ofproto->meters[meter_id]; | |
9cae45dc JR |
6636 | list_insert(&meter->rules, &rule->meter_list_node); |
6637 | } | |
0b4f2078 | 6638 | ovs_rwlock_wrlock(&table->cls.rwlock); |
49a0e0eb | 6639 | classifier_insert(&table->cls, CONST_CAST(struct cls_rule *, &rule->cr)); |
0b4f2078 | 6640 | ovs_rwlock_unlock(&table->cls.rwlock); |
254750ce | 6641 | eviction_group_add_rule(rule); |
d0918789 BP |
6642 | } |
6643 | \f | |
abe529af | 6644 | /* unixctl commands. */ |
7aa697dd | 6645 | |
abe529af | 6646 | struct ofproto * |
2ac6bedd | 6647 | ofproto_lookup(const char *name) |
7aa697dd | 6648 | { |
2ac6bedd | 6649 | struct ofproto *ofproto; |
7aa697dd | 6650 | |
2ac6bedd BP |
6651 | HMAP_FOR_EACH_WITH_HASH (ofproto, hmap_node, hash_string(name, 0), |
6652 | &all_ofprotos) { | |
6653 | if (!strcmp(ofproto->name, name)) { | |
6654 | return ofproto; | |
6655 | } | |
7aa697dd | 6656 | } |
2ac6bedd | 6657 | return NULL; |
7aa697dd BP |
6658 | } |
6659 | ||
6660 | static void | |
0e15264f BP |
6661 | ofproto_unixctl_list(struct unixctl_conn *conn, int argc OVS_UNUSED, |
6662 | const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED) | |
7aa697dd | 6663 | { |
7aa697dd | 6664 | struct ofproto *ofproto; |
7aa697dd | 6665 | struct ds results; |
7aa697dd | 6666 | |
7aa697dd | 6667 | ds_init(&results); |
2ac6bedd BP |
6668 | HMAP_FOR_EACH (ofproto, hmap_node, &all_ofprotos) { |
6669 | ds_put_format(&results, "%s\n", ofproto->name); | |
7aa697dd | 6670 | } |
bde9f75d | 6671 | unixctl_command_reply(conn, ds_cstr(&results)); |
7aa697dd | 6672 | ds_destroy(&results); |
7aa697dd BP |
6673 | } |
6674 | ||
6675 | static void | |
6676 | ofproto_unixctl_init(void) | |
6677 | { | |
6678 | static bool registered; | |
6679 | if (registered) { | |
6680 | return; | |
6681 | } | |
6682 | registered = true; | |
6683 | ||
0e15264f BP |
6684 | unixctl_command_register("ofproto/list", "", 0, 0, |
6685 | ofproto_unixctl_list, NULL); | |
064af421 | 6686 | } |
52a90c29 BP |
6687 | \f |
6688 | /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.) | |
6689 | * | |
6690 | * This is deprecated. It is only for compatibility with broken device drivers | |
6691 | * in old versions of Linux that do not properly support VLANs when VLAN | |
6692 | * devices are not used. When broken device drivers are no longer in | |
6693 | * widespread use, we will delete these interfaces. */ | |
6694 | ||
6695 | /* Sets a 1-bit in the 4096-bit 'vlan_bitmap' for each VLAN ID that is matched | |
6696 | * (exactly) by an OpenFlow rule in 'ofproto'. */ | |
6697 | void | |
6698 | ofproto_get_vlan_usage(struct ofproto *ofproto, unsigned long int *vlan_bitmap) | |
6699 | { | |
d0918789 | 6700 | const struct oftable *oftable; |
52a90c29 BP |
6701 | |
6702 | free(ofproto->vlan_bitmap); | |
6703 | ofproto->vlan_bitmap = bitmap_allocate(4096); | |
6704 | ofproto->vlans_changed = false; | |
6705 | ||
d0918789 | 6706 | OFPROTO_FOR_EACH_TABLE (oftable, ofproto) { |
52a90c29 BP |
6707 | const struct cls_table *table; |
6708 | ||
15aaf599 | 6709 | ovs_rwlock_rdlock(&oftable->cls.rwlock); |
d0918789 | 6710 | HMAP_FOR_EACH (table, hmap_node, &oftable->cls.tables) { |
5cb7a798 | 6711 | if (minimask_get_vid_mask(&table->mask) == VLAN_VID_MASK) { |
52a90c29 BP |
6712 | const struct cls_rule *rule; |
6713 | ||
6714 | HMAP_FOR_EACH (rule, hmap_node, &table->rules) { | |
5cb7a798 | 6715 | uint16_t vid = miniflow_get_vid(&rule->match.flow); |
52a90c29 BP |
6716 | bitmap_set1(vlan_bitmap, vid); |
6717 | bitmap_set1(ofproto->vlan_bitmap, vid); | |
6718 | } | |
6719 | } | |
6720 | } | |
15aaf599 | 6721 | ovs_rwlock_unlock(&oftable->cls.rwlock); |
52a90c29 BP |
6722 | } |
6723 | } | |
6724 | ||
6725 | /* Returns true if new VLANs have come into use by the flow table since the | |
6726 | * last call to ofproto_get_vlan_usage(). | |
6727 | * | |
6728 | * We don't track when old VLANs stop being used. */ | |
6729 | bool | |
6730 | ofproto_has_vlan_usage_changed(const struct ofproto *ofproto) | |
6731 | { | |
6732 | return ofproto->vlans_changed; | |
6733 | } | |
6734 | ||
6735 | /* Configures a VLAN splinter binding between the ports identified by OpenFlow | |
6736 | * port numbers 'vlandev_ofp_port' and 'realdev_ofp_port'. If | |
6737 | * 'realdev_ofp_port' is nonzero, then the VLAN device is enslaved to the real | |
6738 | * device as a VLAN splinter for VLAN ID 'vid'. If 'realdev_ofp_port' is zero, | |
6739 | * then the VLAN device is un-enslaved. */ | |
6740 | int | |
4e022ec0 AW |
6741 | ofproto_port_set_realdev(struct ofproto *ofproto, ofp_port_t vlandev_ofp_port, |
6742 | ofp_port_t realdev_ofp_port, int vid) | |
52a90c29 BP |
6743 | { |
6744 | struct ofport *ofport; | |
6745 | int error; | |
6746 | ||
cb22974d | 6747 | ovs_assert(vlandev_ofp_port != realdev_ofp_port); |
52a90c29 BP |
6748 | |
6749 | ofport = ofproto_get_port(ofproto, vlandev_ofp_port); | |
6750 | if (!ofport) { | |
6751 | VLOG_WARN("%s: cannot set realdev on nonexistent port %"PRIu16, | |
6752 | ofproto->name, vlandev_ofp_port); | |
6753 | return EINVAL; | |
6754 | } | |
6755 | ||
6756 | if (!ofproto->ofproto_class->set_realdev) { | |
6757 | if (!vlandev_ofp_port) { | |
6758 | return 0; | |
6759 | } | |
6760 | VLOG_WARN("%s: vlan splinters not supported", ofproto->name); | |
6761 | return EOPNOTSUPP; | |
6762 | } | |
6763 | ||
6764 | error = ofproto->ofproto_class->set_realdev(ofport, realdev_ofp_port, vid); | |
6765 | if (error) { | |
6766 | VLOG_WARN("%s: setting realdev on port %"PRIu16" (%s) failed (%s)", | |
6767 | ofproto->name, vlandev_ofp_port, | |
10a89ef0 | 6768 | netdev_get_name(ofport->netdev), ovs_strerror(error)); |
52a90c29 BP |
6769 | } |
6770 | return error; | |
6771 | } |