]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
c475ae67 | 2 | * Copyright (c) 2009, 2010 Nicira Networks. |
064af421 | 3 | * |
a14bc59f BP |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at: | |
064af421 | 7 | * |
a14bc59f BP |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
064af421 BP |
15 | */ |
16 | ||
17 | #include <config.h> | |
18 | #include "ofproto.h" | |
19 | #include <errno.h> | |
20 | #include <inttypes.h> | |
21 | #include <net/if.h> | |
22 | #include <netinet/in.h> | |
23 | #include <stdbool.h> | |
24 | #include <stdlib.h> | |
25 | #include "classifier.h" | |
26 | #include "coverage.h" | |
27 | #include "discovery.h" | |
28 | #include "dpif.h" | |
4f2cad2c | 29 | #include "dynamic-string.h" |
064af421 BP |
30 | #include "fail-open.h" |
31 | #include "in-band.h" | |
32 | #include "mac-learning.h" | |
33 | #include "netdev.h" | |
34 | #include "netflow.h" | |
35 | #include "odp-util.h" | |
36 | #include "ofp-print.h" | |
37 | #include "ofpbuf.h" | |
38 | #include "openflow/nicira-ext.h" | |
39 | #include "openflow/openflow.h" | |
40 | #include "openflow/openflow-mgmt.h" | |
41 | #include "openvswitch/datapath-protocol.h" | |
42 | #include "packets.h" | |
43 | #include "pinsched.h" | |
44 | #include "pktbuf.h" | |
45 | #include "poll-loop.h" | |
46 | #include "port-array.h" | |
47 | #include "rconn.h" | |
48 | #include "shash.h" | |
49 | #include "status.h" | |
50 | #include "stp.h" | |
fe55ad15 | 51 | #include "stream-ssl.h" |
064af421 BP |
52 | #include "svec.h" |
53 | #include "tag.h" | |
54 | #include "timeval.h" | |
4f2cad2c | 55 | #include "unixctl.h" |
064af421 | 56 | #include "vconn.h" |
064af421 BP |
57 | #include "xtoxll.h" |
58 | ||
59 | #define THIS_MODULE VLM_ofproto | |
60 | #include "vlog.h" | |
61 | ||
62 | enum { | |
63 | DP_GROUP_FLOOD = 0, | |
64 | DP_GROUP_ALL = 1 | |
65 | }; | |
66 | ||
67 | enum { | |
68 | TABLEID_HASH = 0, | |
69 | TABLEID_CLASSIFIER = 1 | |
70 | }; | |
71 | ||
72 | struct ofport { | |
73 | struct netdev *netdev; | |
74 | struct ofp_phy_port opp; /* In host byte order. */ | |
75 | }; | |
76 | ||
77 | static void ofport_free(struct ofport *); | |
78 | static void hton_ofp_phy_port(struct ofp_phy_port *); | |
79 | ||
80 | static int xlate_actions(const union ofp_action *in, size_t n_in, | |
81 | const flow_t *flow, struct ofproto *ofproto, | |
82 | const struct ofpbuf *packet, | |
83 | struct odp_actions *out, tag_type *tags, | |
6a07af36 | 84 | bool *may_set_up_flow, uint16_t *nf_output_iface); |
064af421 BP |
85 | |
86 | struct rule { | |
87 | struct cls_rule cr; | |
88 | ||
89 | uint16_t idle_timeout; /* In seconds from time of last use. */ | |
90 | uint16_t hard_timeout; /* In seconds from time of creation. */ | |
91 | long long int used; /* Last-used time (0 if never used). */ | |
92 | long long int created; /* Creation time. */ | |
93 | uint64_t packet_count; /* Number of packets received. */ | |
94 | uint64_t byte_count; /* Number of bytes received. */ | |
95 | uint64_t accounted_bytes; /* Number of bytes passed to account_cb. */ | |
064af421 | 96 | tag_type tags; /* Tags (set only by hooks). */ |
0193b2af | 97 | struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */ |
064af421 BP |
98 | |
99 | /* If 'super' is non-NULL, this rule is a subrule, that is, it is an | |
100 | * exact-match rule (having cr.wc.wildcards of 0) generated from the | |
101 | * wildcard rule 'super'. In this case, 'list' is an element of the | |
102 | * super-rule's list. | |
103 | * | |
104 | * If 'super' is NULL, this rule is a super-rule, and 'list' is the head of | |
105 | * a list of subrules. A super-rule with no wildcards (where | |
106 | * cr.wc.wildcards is 0) will never have any subrules. */ | |
107 | struct rule *super; | |
108 | struct list list; | |
109 | ||
110 | /* OpenFlow actions. | |
111 | * | |
112 | * A subrule has no actions (it uses the super-rule's actions). */ | |
113 | int n_actions; | |
114 | union ofp_action *actions; | |
115 | ||
116 | /* Datapath actions. | |
117 | * | |
118 | * A super-rule with wildcard fields never has ODP actions (since the | |
119 | * datapath only supports exact-match flows). */ | |
120 | bool installed; /* Installed in datapath? */ | |
121 | bool may_install; /* True ordinarily; false if actions must | |
122 | * be reassessed for every packet. */ | |
123 | int n_odp_actions; | |
124 | union odp_action *odp_actions; | |
125 | }; | |
126 | ||
127 | static inline bool | |
128 | rule_is_hidden(const struct rule *rule) | |
129 | { | |
130 | /* Subrules are merely an implementation detail, so hide them from the | |
131 | * controller. */ | |
132 | if (rule->super != NULL) { | |
133 | return true; | |
134 | } | |
135 | ||
8cd4882f | 136 | /* Rules with priority higher than UINT16_MAX are set up by ofproto itself |
064af421 BP |
137 | * (e.g. by in-band control) and are intentionally hidden from the |
138 | * controller. */ | |
139 | if (rule->cr.priority > UINT16_MAX) { | |
140 | return true; | |
141 | } | |
142 | ||
143 | return false; | |
144 | } | |
145 | ||
0193b2af JG |
146 | static struct rule *rule_create(struct ofproto *, struct rule *super, |
147 | const union ofp_action *, size_t n_actions, | |
148 | uint16_t idle_timeout, uint16_t hard_timeout); | |
064af421 BP |
149 | static void rule_free(struct rule *); |
150 | static void rule_destroy(struct ofproto *, struct rule *); | |
151 | static struct rule *rule_from_cls_rule(const struct cls_rule *); | |
152 | static void rule_insert(struct ofproto *, struct rule *, | |
153 | struct ofpbuf *packet, uint16_t in_port); | |
154 | static void rule_remove(struct ofproto *, struct rule *); | |
155 | static bool rule_make_actions(struct ofproto *, struct rule *, | |
156 | const struct ofpbuf *packet); | |
157 | static void rule_install(struct ofproto *, struct rule *, | |
158 | struct rule *displaced_rule); | |
159 | static void rule_uninstall(struct ofproto *, struct rule *); | |
160 | static void rule_post_uninstall(struct ofproto *, struct rule *); | |
161 | ||
162 | struct ofconn { | |
163 | struct list node; | |
164 | struct rconn *rconn; | |
165 | struct pktbuf *pktbuf; | |
166 | bool send_flow_exp; | |
167 | int miss_send_len; | |
168 | ||
169 | struct rconn_packet_counter *packet_in_counter; | |
170 | ||
171 | /* Number of OpenFlow messages queued as replies to OpenFlow requests, and | |
172 | * the maximum number before we stop reading OpenFlow requests. */ | |
173 | #define OFCONN_REPLY_MAX 100 | |
174 | struct rconn_packet_counter *reply_counter; | |
175 | }; | |
176 | ||
177 | static struct ofconn *ofconn_create(struct ofproto *, struct rconn *); | |
c475ae67 | 178 | static void ofconn_destroy(struct ofconn *); |
064af421 BP |
179 | static void ofconn_run(struct ofconn *, struct ofproto *); |
180 | static void ofconn_wait(struct ofconn *); | |
181 | static void queue_tx(struct ofpbuf *msg, const struct ofconn *ofconn, | |
182 | struct rconn_packet_counter *counter); | |
183 | ||
184 | struct ofproto { | |
185 | /* Settings. */ | |
186 | uint64_t datapath_id; /* Datapath ID. */ | |
187 | uint64_t fallback_dpid; /* Datapath ID if no better choice found. */ | |
188 | uint64_t mgmt_id; /* Management channel identifier. */ | |
189 | char *manufacturer; /* Manufacturer. */ | |
190 | char *hardware; /* Hardware. */ | |
191 | char *software; /* Software version. */ | |
192 | char *serial; /* Serial number. */ | |
193 | ||
194 | /* Datapath. */ | |
c228a364 | 195 | struct dpif *dpif; |
e9e28be3 | 196 | struct netdev_monitor *netdev_monitor; |
064af421 BP |
197 | struct port_array ports; /* Index is ODP port nr; ofport->opp.port_no is |
198 | * OFP port nr. */ | |
199 | struct shash port_by_name; | |
200 | uint32_t max_ports; | |
201 | ||
202 | /* Configuration. */ | |
203 | struct switch_status *switch_status; | |
204 | struct status_category *ss_cat; | |
205 | struct in_band *in_band; | |
206 | struct discovery *discovery; | |
207 | struct fail_open *fail_open; | |
208 | struct pinsched *miss_sched, *action_sched; | |
064af421 BP |
209 | struct netflow *netflow; |
210 | ||
211 | /* Flow table. */ | |
212 | struct classifier cls; | |
213 | bool need_revalidate; | |
214 | long long int next_expiration; | |
215 | struct tag_set revalidate_set; | |
216 | ||
217 | /* OpenFlow connections. */ | |
218 | struct list all_conns; | |
219 | struct ofconn *controller; | |
220 | struct pvconn **listeners; | |
221 | size_t n_listeners; | |
222 | struct pvconn **snoops; | |
223 | size_t n_snoops; | |
224 | ||
225 | /* Hooks for ovs-vswitchd. */ | |
226 | const struct ofhooks *ofhooks; | |
227 | void *aux; | |
228 | ||
229 | /* Used by default ofhooks. */ | |
230 | struct mac_learning *ml; | |
231 | }; | |
232 | ||
233 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
234 | ||
235 | static const struct ofhooks default_ofhooks; | |
236 | ||
fa60c019 | 237 | static uint64_t pick_datapath_id(const struct ofproto *); |
064af421 BP |
238 | static uint64_t pick_fallback_dpid(void); |
239 | static void send_packet_in_miss(struct ofpbuf *, void *ofproto); | |
240 | static void send_packet_in_action(struct ofpbuf *, void *ofproto); | |
241 | static void update_used(struct ofproto *); | |
0193b2af JG |
242 | static void update_stats(struct ofproto *, struct rule *, |
243 | const struct odp_flow_stats *); | |
064af421 | 244 | static void expire_rule(struct cls_rule *, void *ofproto); |
0193b2af | 245 | static void active_timeout(struct ofproto *ofproto, struct rule *rule); |
064af421 BP |
246 | static bool revalidate_rule(struct ofproto *p, struct rule *rule); |
247 | static void revalidate_cb(struct cls_rule *rule_, void *p_); | |
248 | ||
249 | static void handle_odp_msg(struct ofproto *, struct ofpbuf *); | |
250 | ||
251 | static void handle_openflow(struct ofconn *, struct ofproto *, | |
252 | struct ofpbuf *); | |
253 | ||
254 | static void refresh_port_group(struct ofproto *, unsigned int group); | |
255 | static void update_port(struct ofproto *, const char *devname); | |
256 | static int init_ports(struct ofproto *); | |
257 | static void reinit_ports(struct ofproto *); | |
258 | ||
259 | int | |
260 | ofproto_create(const char *datapath, const struct ofhooks *ofhooks, void *aux, | |
261 | struct ofproto **ofprotop) | |
262 | { | |
064af421 BP |
263 | struct odp_stats stats; |
264 | struct ofproto *p; | |
c228a364 | 265 | struct dpif *dpif; |
064af421 BP |
266 | int error; |
267 | ||
268 | *ofprotop = NULL; | |
269 | ||
270 | /* Connect to datapath and start listening for messages. */ | |
271 | error = dpif_open(datapath, &dpif); | |
272 | if (error) { | |
273 | VLOG_ERR("failed to open datapath %s: %s", datapath, strerror(error)); | |
274 | return error; | |
275 | } | |
c228a364 | 276 | error = dpif_get_dp_stats(dpif, &stats); |
064af421 BP |
277 | if (error) { |
278 | VLOG_ERR("failed to obtain stats for datapath %s: %s", | |
279 | datapath, strerror(error)); | |
c228a364 | 280 | dpif_close(dpif); |
064af421 BP |
281 | return error; |
282 | } | |
8f24562a | 283 | error = dpif_recv_set_mask(dpif, ODPL_MISS | ODPL_ACTION); |
064af421 BP |
284 | if (error) { |
285 | VLOG_ERR("failed to listen on datapath %s: %s", | |
286 | datapath, strerror(error)); | |
c228a364 | 287 | dpif_close(dpif); |
064af421 BP |
288 | return error; |
289 | } | |
c228a364 | 290 | dpif_flow_flush(dpif); |
8f24562a | 291 | dpif_recv_purge(dpif); |
064af421 BP |
292 | |
293 | /* Initialize settings. */ | |
ec6fde61 | 294 | p = xzalloc(sizeof *p); |
064af421 | 295 | p->fallback_dpid = pick_fallback_dpid(); |
fa60c019 | 296 | p->datapath_id = p->fallback_dpid; |
064af421 BP |
297 | p->manufacturer = xstrdup("Nicira Networks, Inc."); |
298 | p->hardware = xstrdup("Reference Implementation"); | |
299 | p->software = xstrdup(VERSION BUILDNR); | |
300 | p->serial = xstrdup("None"); | |
301 | ||
302 | /* Initialize datapath. */ | |
303 | p->dpif = dpif; | |
8b61709d | 304 | p->netdev_monitor = netdev_monitor_create(); |
064af421 BP |
305 | port_array_init(&p->ports); |
306 | shash_init(&p->port_by_name); | |
307 | p->max_ports = stats.max_ports; | |
308 | ||
309 | /* Initialize submodules. */ | |
310 | p->switch_status = switch_status_create(p); | |
311 | p->in_band = NULL; | |
312 | p->discovery = NULL; | |
313 | p->fail_open = NULL; | |
314 | p->miss_sched = p->action_sched = NULL; | |
064af421 BP |
315 | p->netflow = NULL; |
316 | ||
317 | /* Initialize flow table. */ | |
318 | classifier_init(&p->cls); | |
319 | p->need_revalidate = false; | |
320 | p->next_expiration = time_msec() + 1000; | |
321 | tag_set_init(&p->revalidate_set); | |
322 | ||
323 | /* Initialize OpenFlow connections. */ | |
324 | list_init(&p->all_conns); | |
f9fb1858 | 325 | p->controller = ofconn_create(p, rconn_create(5, 8)); |
064af421 BP |
326 | p->controller->pktbuf = pktbuf_create(); |
327 | p->controller->miss_send_len = OFP_DEFAULT_MISS_SEND_LEN; | |
328 | p->listeners = NULL; | |
329 | p->n_listeners = 0; | |
330 | p->snoops = NULL; | |
331 | p->n_snoops = 0; | |
332 | ||
333 | /* Initialize hooks. */ | |
334 | if (ofhooks) { | |
335 | p->ofhooks = ofhooks; | |
336 | p->aux = aux; | |
337 | p->ml = NULL; | |
338 | } else { | |
339 | p->ofhooks = &default_ofhooks; | |
340 | p->aux = p; | |
341 | p->ml = mac_learning_create(); | |
342 | } | |
343 | ||
344 | /* Register switch status category. */ | |
345 | p->ss_cat = switch_status_register(p->switch_status, "remote", | |
346 | rconn_status_cb, p->controller->rconn); | |
347 | ||
fa60c019 BP |
348 | /* Pick final datapath ID. */ |
349 | p->datapath_id = pick_datapath_id(p); | |
350 | VLOG_INFO("using datapath ID %012"PRIx64, p->datapath_id); | |
351 | ||
064af421 BP |
352 | *ofprotop = p; |
353 | return 0; | |
354 | } | |
355 | ||
356 | void | |
357 | ofproto_set_datapath_id(struct ofproto *p, uint64_t datapath_id) | |
358 | { | |
359 | uint64_t old_dpid = p->datapath_id; | |
fa60c019 | 360 | p->datapath_id = datapath_id ? datapath_id : pick_datapath_id(p); |
064af421 BP |
361 | if (p->datapath_id != old_dpid) { |
362 | VLOG_INFO("datapath ID changed to %012"PRIx64, p->datapath_id); | |
363 | rconn_reconnect(p->controller->rconn); | |
364 | } | |
365 | } | |
366 | ||
367 | void | |
368 | ofproto_set_mgmt_id(struct ofproto *p, uint64_t mgmt_id) | |
369 | { | |
370 | p->mgmt_id = mgmt_id; | |
371 | } | |
372 | ||
373 | void | |
374 | ofproto_set_probe_interval(struct ofproto *p, int probe_interval) | |
375 | { | |
376 | probe_interval = probe_interval ? MAX(probe_interval, 5) : 0; | |
377 | rconn_set_probe_interval(p->controller->rconn, probe_interval); | |
378 | if (p->fail_open) { | |
379 | int trigger_duration = probe_interval ? probe_interval * 3 : 15; | |
380 | fail_open_set_trigger_duration(p->fail_open, trigger_duration); | |
381 | } | |
382 | } | |
383 | ||
384 | void | |
385 | ofproto_set_max_backoff(struct ofproto *p, int max_backoff) | |
386 | { | |
387 | rconn_set_max_backoff(p->controller->rconn, max_backoff); | |
388 | } | |
389 | ||
390 | void | |
391 | ofproto_set_desc(struct ofproto *p, | |
392 | const char *manufacturer, const char *hardware, | |
393 | const char *software, const char *serial) | |
394 | { | |
395 | if (manufacturer) { | |
396 | free(p->manufacturer); | |
397 | p->manufacturer = xstrdup(manufacturer); | |
398 | } | |
399 | if (hardware) { | |
400 | free(p->hardware); | |
401 | p->hardware = xstrdup(hardware); | |
402 | } | |
403 | if (software) { | |
404 | free(p->software); | |
405 | p->software = xstrdup(software); | |
406 | } | |
407 | if (serial) { | |
408 | free(p->serial); | |
409 | p->serial = xstrdup(serial); | |
410 | } | |
411 | } | |
412 | ||
413 | int | |
414 | ofproto_set_in_band(struct ofproto *p, bool in_band) | |
415 | { | |
416 | if (in_band != (p->in_band != NULL)) { | |
417 | if (in_band) { | |
f1acd62b BP |
418 | return in_band_create(p, p->dpif, p->switch_status, |
419 | p->controller->rconn, &p->in_band); | |
064af421 BP |
420 | } else { |
421 | ofproto_set_discovery(p, false, NULL, true); | |
422 | in_band_destroy(p->in_band); | |
423 | p->in_band = NULL; | |
424 | } | |
425 | rconn_reconnect(p->controller->rconn); | |
426 | } | |
427 | return 0; | |
428 | } | |
429 | ||
430 | int | |
431 | ofproto_set_discovery(struct ofproto *p, bool discovery, | |
432 | const char *re, bool update_resolv_conf) | |
433 | { | |
434 | if (discovery != (p->discovery != NULL)) { | |
435 | if (discovery) { | |
436 | int error = ofproto_set_in_band(p, true); | |
437 | if (error) { | |
438 | return error; | |
439 | } | |
440 | error = discovery_create(re, update_resolv_conf, | |
c228a364 | 441 | p->dpif, p->switch_status, |
064af421 BP |
442 | &p->discovery); |
443 | if (error) { | |
444 | return error; | |
445 | } | |
446 | } else { | |
447 | discovery_destroy(p->discovery); | |
448 | p->discovery = NULL; | |
449 | } | |
450 | rconn_disconnect(p->controller->rconn); | |
451 | } else if (discovery) { | |
452 | discovery_set_update_resolv_conf(p->discovery, update_resolv_conf); | |
453 | return discovery_set_accept_controller_re(p->discovery, re); | |
454 | } | |
455 | return 0; | |
456 | } | |
457 | ||
458 | int | |
459 | ofproto_set_controller(struct ofproto *ofproto, const char *controller) | |
460 | { | |
461 | if (ofproto->discovery) { | |
462 | return EINVAL; | |
463 | } else if (controller) { | |
464 | if (strcmp(rconn_get_name(ofproto->controller->rconn), controller)) { | |
465 | return rconn_connect(ofproto->controller->rconn, controller); | |
466 | } else { | |
467 | return 0; | |
468 | } | |
469 | } else { | |
470 | rconn_disconnect(ofproto->controller->rconn); | |
471 | return 0; | |
472 | } | |
473 | } | |
474 | ||
475 | static int | |
476 | set_pvconns(struct pvconn ***pvconnsp, size_t *n_pvconnsp, | |
477 | const struct svec *svec) | |
478 | { | |
479 | struct pvconn **pvconns = *pvconnsp; | |
480 | size_t n_pvconns = *n_pvconnsp; | |
481 | int retval = 0; | |
482 | size_t i; | |
483 | ||
484 | for (i = 0; i < n_pvconns; i++) { | |
485 | pvconn_close(pvconns[i]); | |
486 | } | |
487 | free(pvconns); | |
488 | ||
489 | pvconns = xmalloc(svec->n * sizeof *pvconns); | |
490 | n_pvconns = 0; | |
491 | for (i = 0; i < svec->n; i++) { | |
492 | const char *name = svec->names[i]; | |
493 | struct pvconn *pvconn; | |
494 | int error; | |
495 | ||
496 | error = pvconn_open(name, &pvconn); | |
497 | if (!error) { | |
498 | pvconns[n_pvconns++] = pvconn; | |
499 | } else { | |
500 | VLOG_ERR("failed to listen on %s: %s", name, strerror(error)); | |
501 | if (!retval) { | |
502 | retval = error; | |
503 | } | |
504 | } | |
505 | } | |
506 | ||
507 | *pvconnsp = pvconns; | |
508 | *n_pvconnsp = n_pvconns; | |
509 | ||
510 | return retval; | |
511 | } | |
512 | ||
513 | int | |
514 | ofproto_set_listeners(struct ofproto *ofproto, const struct svec *listeners) | |
515 | { | |
516 | return set_pvconns(&ofproto->listeners, &ofproto->n_listeners, listeners); | |
517 | } | |
518 | ||
519 | int | |
520 | ofproto_set_snoops(struct ofproto *ofproto, const struct svec *snoops) | |
521 | { | |
522 | return set_pvconns(&ofproto->snoops, &ofproto->n_snoops, snoops); | |
523 | } | |
524 | ||
525 | int | |
0193b2af JG |
526 | ofproto_set_netflow(struct ofproto *ofproto, |
527 | const struct netflow_options *nf_options) | |
064af421 | 528 | { |
76343538 | 529 | if (nf_options && nf_options->collectors.n) { |
064af421 BP |
530 | if (!ofproto->netflow) { |
531 | ofproto->netflow = netflow_create(); | |
532 | } | |
0193b2af | 533 | return netflow_set_options(ofproto->netflow, nf_options); |
064af421 BP |
534 | } else { |
535 | netflow_destroy(ofproto->netflow); | |
536 | ofproto->netflow = NULL; | |
537 | return 0; | |
538 | } | |
539 | } | |
540 | ||
541 | void | |
542 | ofproto_set_failure(struct ofproto *ofproto, bool fail_open) | |
543 | { | |
544 | if (fail_open) { | |
545 | struct rconn *rconn = ofproto->controller->rconn; | |
546 | int trigger_duration = rconn_get_probe_interval(rconn) * 3; | |
547 | if (!ofproto->fail_open) { | |
548 | ofproto->fail_open = fail_open_create(ofproto, trigger_duration, | |
549 | ofproto->switch_status, | |
550 | rconn); | |
551 | } else { | |
552 | fail_open_set_trigger_duration(ofproto->fail_open, | |
553 | trigger_duration); | |
554 | } | |
555 | } else { | |
556 | fail_open_destroy(ofproto->fail_open); | |
557 | ofproto->fail_open = NULL; | |
558 | } | |
559 | } | |
560 | ||
561 | void | |
562 | ofproto_set_rate_limit(struct ofproto *ofproto, | |
563 | int rate_limit, int burst_limit) | |
564 | { | |
565 | if (rate_limit > 0) { | |
566 | if (!ofproto->miss_sched) { | |
567 | ofproto->miss_sched = pinsched_create(rate_limit, burst_limit, | |
568 | ofproto->switch_status); | |
569 | ofproto->action_sched = pinsched_create(rate_limit, burst_limit, | |
570 | NULL); | |
571 | } else { | |
572 | pinsched_set_limits(ofproto->miss_sched, rate_limit, burst_limit); | |
573 | pinsched_set_limits(ofproto->action_sched, | |
574 | rate_limit, burst_limit); | |
575 | } | |
576 | } else { | |
577 | pinsched_destroy(ofproto->miss_sched); | |
578 | ofproto->miss_sched = NULL; | |
579 | pinsched_destroy(ofproto->action_sched); | |
580 | ofproto->action_sched = NULL; | |
581 | } | |
582 | } | |
583 | ||
584 | int | |
585 | ofproto_set_stp(struct ofproto *ofproto UNUSED, bool enable_stp) | |
586 | { | |
587 | /* XXX */ | |
588 | if (enable_stp) { | |
589 | VLOG_WARN("STP is not yet implemented"); | |
590 | return EINVAL; | |
591 | } else { | |
592 | return 0; | |
593 | } | |
594 | } | |
595 | ||
064af421 BP |
596 | uint64_t |
597 | ofproto_get_datapath_id(const struct ofproto *ofproto) | |
598 | { | |
599 | return ofproto->datapath_id; | |
600 | } | |
601 | ||
3b250e29 JP |
602 | uint64_t |
603 | ofproto_get_mgmt_id(const struct ofproto *ofproto) | |
604 | { | |
605 | return ofproto->mgmt_id; | |
606 | } | |
607 | ||
064af421 BP |
608 | int |
609 | ofproto_get_probe_interval(const struct ofproto *ofproto) | |
610 | { | |
611 | return rconn_get_probe_interval(ofproto->controller->rconn); | |
612 | } | |
613 | ||
614 | int | |
615 | ofproto_get_max_backoff(const struct ofproto *ofproto) | |
616 | { | |
617 | return rconn_get_max_backoff(ofproto->controller->rconn); | |
618 | } | |
619 | ||
620 | bool | |
621 | ofproto_get_in_band(const struct ofproto *ofproto) | |
622 | { | |
623 | return ofproto->in_band != NULL; | |
624 | } | |
625 | ||
626 | bool | |
627 | ofproto_get_discovery(const struct ofproto *ofproto) | |
628 | { | |
629 | return ofproto->discovery != NULL; | |
630 | } | |
631 | ||
632 | const char * | |
633 | ofproto_get_controller(const struct ofproto *ofproto) | |
634 | { | |
635 | return rconn_get_name(ofproto->controller->rconn); | |
636 | } | |
637 | ||
638 | void | |
639 | ofproto_get_listeners(const struct ofproto *ofproto, struct svec *listeners) | |
640 | { | |
641 | size_t i; | |
642 | ||
643 | for (i = 0; i < ofproto->n_listeners; i++) { | |
644 | svec_add(listeners, pvconn_get_name(ofproto->listeners[i])); | |
645 | } | |
646 | } | |
647 | ||
648 | void | |
649 | ofproto_get_snoops(const struct ofproto *ofproto, struct svec *snoops) | |
650 | { | |
651 | size_t i; | |
652 | ||
653 | for (i = 0; i < ofproto->n_snoops; i++) { | |
654 | svec_add(snoops, pvconn_get_name(ofproto->snoops[i])); | |
655 | } | |
656 | } | |
657 | ||
658 | void | |
659 | ofproto_destroy(struct ofproto *p) | |
660 | { | |
661 | struct ofconn *ofconn, *next_ofconn; | |
662 | struct ofport *ofport; | |
663 | unsigned int port_no; | |
664 | size_t i; | |
665 | ||
666 | if (!p) { | |
667 | return; | |
668 | } | |
669 | ||
670 | ofproto_flush_flows(p); | |
671 | classifier_destroy(&p->cls); | |
672 | ||
673 | LIST_FOR_EACH_SAFE (ofconn, next_ofconn, struct ofconn, node, | |
674 | &p->all_conns) { | |
c475ae67 | 675 | ofconn_destroy(ofconn); |
064af421 BP |
676 | } |
677 | ||
c228a364 | 678 | dpif_close(p->dpif); |
e9e28be3 | 679 | netdev_monitor_destroy(p->netdev_monitor); |
064af421 BP |
680 | PORT_ARRAY_FOR_EACH (ofport, &p->ports, port_no) { |
681 | ofport_free(ofport); | |
682 | } | |
683 | shash_destroy(&p->port_by_name); | |
684 | ||
685 | switch_status_destroy(p->switch_status); | |
686 | in_band_destroy(p->in_band); | |
687 | discovery_destroy(p->discovery); | |
688 | fail_open_destroy(p->fail_open); | |
689 | pinsched_destroy(p->miss_sched); | |
690 | pinsched_destroy(p->action_sched); | |
064af421 BP |
691 | netflow_destroy(p->netflow); |
692 | ||
693 | switch_status_unregister(p->ss_cat); | |
694 | ||
695 | for (i = 0; i < p->n_listeners; i++) { | |
696 | pvconn_close(p->listeners[i]); | |
697 | } | |
698 | free(p->listeners); | |
699 | ||
700 | for (i = 0; i < p->n_snoops; i++) { | |
701 | pvconn_close(p->snoops[i]); | |
702 | } | |
703 | free(p->snoops); | |
704 | ||
705 | mac_learning_destroy(p->ml); | |
706 | ||
707 | free(p); | |
708 | } | |
709 | ||
710 | int | |
711 | ofproto_run(struct ofproto *p) | |
712 | { | |
713 | int error = ofproto_run1(p); | |
714 | if (!error) { | |
715 | error = ofproto_run2(p, false); | |
716 | } | |
717 | return error; | |
718 | } | |
719 | ||
e9e28be3 BP |
720 | static void |
721 | process_port_change(struct ofproto *ofproto, int error, char *devname) | |
722 | { | |
723 | if (error == ENOBUFS) { | |
724 | reinit_ports(ofproto); | |
725 | } else if (!error) { | |
726 | update_port(ofproto, devname); | |
727 | free(devname); | |
728 | } | |
729 | } | |
730 | ||
064af421 BP |
731 | int |
732 | ofproto_run1(struct ofproto *p) | |
733 | { | |
734 | struct ofconn *ofconn, *next_ofconn; | |
735 | char *devname; | |
736 | int error; | |
737 | int i; | |
738 | ||
149f577a JG |
739 | if (shash_is_empty(&p->port_by_name)) { |
740 | init_ports(p); | |
741 | } | |
742 | ||
064af421 BP |
743 | for (i = 0; i < 50; i++) { |
744 | struct ofpbuf *buf; | |
745 | int error; | |
746 | ||
c228a364 | 747 | error = dpif_recv(p->dpif, &buf); |
064af421 BP |
748 | if (error) { |
749 | if (error == ENODEV) { | |
750 | /* Someone destroyed the datapath behind our back. The caller | |
751 | * better destroy us and give up, because we're just going to | |
752 | * spin from here on out. */ | |
753 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
b29ba128 | 754 | VLOG_ERR_RL(&rl, "%s: datapath was destroyed externally", |
c228a364 | 755 | dpif_name(p->dpif)); |
064af421 BP |
756 | return ENODEV; |
757 | } | |
758 | break; | |
759 | } | |
760 | ||
761 | handle_odp_msg(p, buf); | |
762 | } | |
763 | ||
e9e28be3 BP |
764 | while ((error = dpif_port_poll(p->dpif, &devname)) != EAGAIN) { |
765 | process_port_change(p, error, devname); | |
766 | } | |
767 | while ((error = netdev_monitor_poll(p->netdev_monitor, | |
768 | &devname)) != EAGAIN) { | |
769 | process_port_change(p, error, devname); | |
064af421 BP |
770 | } |
771 | ||
772 | if (p->in_band) { | |
773 | in_band_run(p->in_band); | |
774 | } | |
775 | if (p->discovery) { | |
776 | char *controller_name; | |
777 | if (rconn_is_connectivity_questionable(p->controller->rconn)) { | |
778 | discovery_question_connectivity(p->discovery); | |
779 | } | |
780 | if (discovery_run(p->discovery, &controller_name)) { | |
781 | if (controller_name) { | |
782 | rconn_connect(p->controller->rconn, controller_name); | |
783 | } else { | |
784 | rconn_disconnect(p->controller->rconn); | |
785 | } | |
786 | } | |
787 | } | |
064af421 BP |
788 | pinsched_run(p->miss_sched, send_packet_in_miss, p); |
789 | pinsched_run(p->action_sched, send_packet_in_action, p); | |
064af421 BP |
790 | |
791 | LIST_FOR_EACH_SAFE (ofconn, next_ofconn, struct ofconn, node, | |
792 | &p->all_conns) { | |
793 | ofconn_run(ofconn, p); | |
794 | } | |
795 | ||
7778bd15 BP |
796 | /* Fail-open maintenance. Do this after processing the ofconns since |
797 | * fail-open checks the status of the controller rconn. */ | |
798 | if (p->fail_open) { | |
799 | fail_open_run(p->fail_open); | |
800 | } | |
801 | ||
064af421 BP |
802 | for (i = 0; i < p->n_listeners; i++) { |
803 | struct vconn *vconn; | |
804 | int retval; | |
805 | ||
806 | retval = pvconn_accept(p->listeners[i], OFP_VERSION, &vconn); | |
807 | if (!retval) { | |
808 | ofconn_create(p, rconn_new_from_vconn("passive", vconn)); | |
809 | } else if (retval != EAGAIN) { | |
810 | VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval)); | |
811 | } | |
812 | } | |
813 | ||
814 | for (i = 0; i < p->n_snoops; i++) { | |
815 | struct vconn *vconn; | |
816 | int retval; | |
817 | ||
818 | retval = pvconn_accept(p->snoops[i], OFP_VERSION, &vconn); | |
819 | if (!retval) { | |
820 | rconn_add_monitor(p->controller->rconn, vconn); | |
821 | } else if (retval != EAGAIN) { | |
822 | VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval)); | |
823 | } | |
824 | } | |
825 | ||
826 | if (time_msec() >= p->next_expiration) { | |
827 | COVERAGE_INC(ofproto_expiration); | |
828 | p->next_expiration = time_msec() + 1000; | |
829 | update_used(p); | |
830 | ||
831 | classifier_for_each(&p->cls, CLS_INC_ALL, expire_rule, p); | |
832 | ||
833 | /* Let the hook know that we're at a stable point: all outstanding data | |
834 | * in existing flows has been accounted to the account_cb. Thus, the | |
835 | * hook can now reasonably do operations that depend on having accurate | |
836 | * flow volume accounting (currently, that's just bond rebalancing). */ | |
837 | if (p->ofhooks->account_checkpoint_cb) { | |
838 | p->ofhooks->account_checkpoint_cb(p->aux); | |
839 | } | |
840 | } | |
841 | ||
842 | if (p->netflow) { | |
843 | netflow_run(p->netflow); | |
844 | } | |
845 | ||
846 | return 0; | |
847 | } | |
848 | ||
849 | struct revalidate_cbdata { | |
850 | struct ofproto *ofproto; | |
851 | bool revalidate_all; /* Revalidate all exact-match rules? */ | |
852 | bool revalidate_subrules; /* Revalidate all exact-match subrules? */ | |
853 | struct tag_set revalidate_set; /* Set of tags to revalidate. */ | |
854 | }; | |
855 | ||
856 | int | |
857 | ofproto_run2(struct ofproto *p, bool revalidate_all) | |
858 | { | |
859 | if (p->need_revalidate || revalidate_all | |
860 | || !tag_set_is_empty(&p->revalidate_set)) { | |
861 | struct revalidate_cbdata cbdata; | |
862 | cbdata.ofproto = p; | |
863 | cbdata.revalidate_all = revalidate_all; | |
864 | cbdata.revalidate_subrules = p->need_revalidate; | |
865 | cbdata.revalidate_set = p->revalidate_set; | |
866 | tag_set_init(&p->revalidate_set); | |
867 | COVERAGE_INC(ofproto_revalidate); | |
868 | classifier_for_each(&p->cls, CLS_INC_EXACT, revalidate_cb, &cbdata); | |
869 | p->need_revalidate = false; | |
870 | } | |
871 | ||
872 | return 0; | |
873 | } | |
874 | ||
875 | void | |
876 | ofproto_wait(struct ofproto *p) | |
877 | { | |
878 | struct ofconn *ofconn; | |
879 | size_t i; | |
880 | ||
c228a364 | 881 | dpif_recv_wait(p->dpif); |
e9e28be3 BP |
882 | dpif_port_poll_wait(p->dpif); |
883 | netdev_monitor_poll_wait(p->netdev_monitor); | |
064af421 BP |
884 | LIST_FOR_EACH (ofconn, struct ofconn, node, &p->all_conns) { |
885 | ofconn_wait(ofconn); | |
886 | } | |
887 | if (p->in_band) { | |
888 | in_band_wait(p->in_band); | |
889 | } | |
890 | if (p->discovery) { | |
891 | discovery_wait(p->discovery); | |
892 | } | |
893 | if (p->fail_open) { | |
894 | fail_open_wait(p->fail_open); | |
895 | } | |
896 | pinsched_wait(p->miss_sched); | |
897 | pinsched_wait(p->action_sched); | |
064af421 BP |
898 | if (!tag_set_is_empty(&p->revalidate_set)) { |
899 | poll_immediate_wake(); | |
900 | } | |
901 | if (p->need_revalidate) { | |
902 | /* Shouldn't happen, but if it does just go around again. */ | |
903 | VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()"); | |
904 | poll_immediate_wake(); | |
905 | } else if (p->next_expiration != LLONG_MAX) { | |
906 | poll_timer_wait(p->next_expiration - time_msec()); | |
907 | } | |
908 | for (i = 0; i < p->n_listeners; i++) { | |
909 | pvconn_wait(p->listeners[i]); | |
910 | } | |
911 | for (i = 0; i < p->n_snoops; i++) { | |
912 | pvconn_wait(p->snoops[i]); | |
913 | } | |
914 | } | |
915 | ||
916 | void | |
917 | ofproto_revalidate(struct ofproto *ofproto, tag_type tag) | |
918 | { | |
919 | tag_set_add(&ofproto->revalidate_set, tag); | |
920 | } | |
921 | ||
922 | struct tag_set * | |
923 | ofproto_get_revalidate_set(struct ofproto *ofproto) | |
924 | { | |
925 | return &ofproto->revalidate_set; | |
926 | } | |
927 | ||
928 | bool | |
929 | ofproto_is_alive(const struct ofproto *p) | |
930 | { | |
931 | return p->discovery || rconn_is_alive(p->controller->rconn); | |
932 | } | |
933 | ||
934 | int | |
935 | ofproto_send_packet(struct ofproto *p, const flow_t *flow, | |
936 | const union ofp_action *actions, size_t n_actions, | |
937 | const struct ofpbuf *packet) | |
938 | { | |
939 | struct odp_actions odp_actions; | |
940 | int error; | |
941 | ||
942 | error = xlate_actions(actions, n_actions, flow, p, packet, &odp_actions, | |
6a07af36 | 943 | NULL, NULL, NULL); |
064af421 BP |
944 | if (error) { |
945 | return error; | |
946 | } | |
947 | ||
948 | /* XXX Should we translate the dpif_execute() errno value into an OpenFlow | |
949 | * error code? */ | |
c228a364 | 950 | dpif_execute(p->dpif, flow->in_port, odp_actions.actions, |
064af421 BP |
951 | odp_actions.n_actions, packet); |
952 | return 0; | |
953 | } | |
954 | ||
955 | void | |
956 | ofproto_add_flow(struct ofproto *p, | |
957 | const flow_t *flow, uint32_t wildcards, unsigned int priority, | |
958 | const union ofp_action *actions, size_t n_actions, | |
959 | int idle_timeout) | |
960 | { | |
961 | struct rule *rule; | |
0193b2af | 962 | rule = rule_create(p, NULL, actions, n_actions, |
064af421 BP |
963 | idle_timeout >= 0 ? idle_timeout : 5 /* XXX */, 0); |
964 | cls_rule_from_flow(&rule->cr, flow, wildcards, priority); | |
965 | rule_insert(p, rule, NULL, 0); | |
966 | } | |
967 | ||
968 | void | |
969 | ofproto_delete_flow(struct ofproto *ofproto, const flow_t *flow, | |
970 | uint32_t wildcards, unsigned int priority) | |
971 | { | |
972 | struct rule *rule; | |
973 | ||
974 | rule = rule_from_cls_rule(classifier_find_rule_exactly(&ofproto->cls, | |
975 | flow, wildcards, | |
976 | priority)); | |
977 | if (rule) { | |
978 | rule_remove(ofproto, rule); | |
979 | } | |
980 | } | |
981 | ||
982 | static void | |
983 | destroy_rule(struct cls_rule *rule_, void *ofproto_) | |
984 | { | |
985 | struct rule *rule = rule_from_cls_rule(rule_); | |
986 | struct ofproto *ofproto = ofproto_; | |
987 | ||
988 | /* Mark the flow as not installed, even though it might really be | |
989 | * installed, so that rule_remove() doesn't bother trying to uninstall it. | |
990 | * There is no point in uninstalling it individually since we are about to | |
991 | * blow away all the flows with dpif_flow_flush(). */ | |
992 | rule->installed = false; | |
993 | ||
994 | rule_remove(ofproto, rule); | |
995 | } | |
996 | ||
997 | void | |
998 | ofproto_flush_flows(struct ofproto *ofproto) | |
999 | { | |
1000 | COVERAGE_INC(ofproto_flush); | |
1001 | classifier_for_each(&ofproto->cls, CLS_INC_ALL, destroy_rule, ofproto); | |
c228a364 | 1002 | dpif_flow_flush(ofproto->dpif); |
064af421 BP |
1003 | if (ofproto->in_band) { |
1004 | in_band_flushed(ofproto->in_band); | |
1005 | } | |
1006 | if (ofproto->fail_open) { | |
1007 | fail_open_flushed(ofproto->fail_open); | |
1008 | } | |
1009 | } | |
1010 | \f | |
1011 | static void | |
1012 | reinit_ports(struct ofproto *p) | |
1013 | { | |
1014 | struct svec devnames; | |
1015 | struct ofport *ofport; | |
1016 | unsigned int port_no; | |
1017 | struct odp_port *odp_ports; | |
1018 | size_t n_odp_ports; | |
1019 | size_t i; | |
1020 | ||
1021 | svec_init(&devnames); | |
1022 | PORT_ARRAY_FOR_EACH (ofport, &p->ports, port_no) { | |
1023 | svec_add (&devnames, (char *) ofport->opp.name); | |
1024 | } | |
c228a364 | 1025 | dpif_port_list(p->dpif, &odp_ports, &n_odp_ports); |
064af421 BP |
1026 | for (i = 0; i < n_odp_ports; i++) { |
1027 | svec_add (&devnames, odp_ports[i].devname); | |
1028 | } | |
1029 | free(odp_ports); | |
1030 | ||
1031 | svec_sort_unique(&devnames); | |
1032 | for (i = 0; i < devnames.n; i++) { | |
1033 | update_port(p, devnames.names[i]); | |
1034 | } | |
1035 | svec_destroy(&devnames); | |
1036 | } | |
1037 | ||
1038 | static void | |
1039 | refresh_port_group(struct ofproto *p, unsigned int group) | |
1040 | { | |
1041 | uint16_t *ports; | |
1042 | size_t n_ports; | |
1043 | struct ofport *port; | |
1044 | unsigned int port_no; | |
1045 | ||
1046 | assert(group == DP_GROUP_ALL || group == DP_GROUP_FLOOD); | |
1047 | ||
1048 | ports = xmalloc(port_array_count(&p->ports) * sizeof *ports); | |
1049 | n_ports = 0; | |
1050 | PORT_ARRAY_FOR_EACH (port, &p->ports, port_no) { | |
1051 | if (group == DP_GROUP_ALL || !(port->opp.config & OFPPC_NO_FLOOD)) { | |
1052 | ports[n_ports++] = port_no; | |
1053 | } | |
1054 | } | |
c228a364 | 1055 | dpif_port_group_set(p->dpif, group, ports, n_ports); |
064af421 BP |
1056 | free(ports); |
1057 | } | |
1058 | ||
1059 | static void | |
1060 | refresh_port_groups(struct ofproto *p) | |
1061 | { | |
1062 | refresh_port_group(p, DP_GROUP_FLOOD); | |
1063 | refresh_port_group(p, DP_GROUP_ALL); | |
1064 | } | |
1065 | ||
1066 | static struct ofport * | |
1067 | make_ofport(const struct odp_port *odp_port) | |
1068 | { | |
149f577a | 1069 | struct netdev_options netdev_options; |
064af421 BP |
1070 | enum netdev_flags flags; |
1071 | struct ofport *ofport; | |
1072 | struct netdev *netdev; | |
1073 | bool carrier; | |
1074 | int error; | |
1075 | ||
149f577a JG |
1076 | memset(&netdev_options, 0, sizeof netdev_options); |
1077 | netdev_options.name = odp_port->devname; | |
1078 | netdev_options.ethertype = NETDEV_ETH_TYPE_NONE; | |
1079 | netdev_options.may_open = true; | |
1080 | ||
1081 | error = netdev_open(&netdev_options, &netdev); | |
064af421 BP |
1082 | if (error) { |
1083 | VLOG_WARN_RL(&rl, "ignoring port %s (%"PRIu16") because netdev %s " | |
1084 | "cannot be opened (%s)", | |
1085 | odp_port->devname, odp_port->port, | |
1086 | odp_port->devname, strerror(error)); | |
1087 | return NULL; | |
1088 | } | |
1089 | ||
1090 | ofport = xmalloc(sizeof *ofport); | |
1091 | ofport->netdev = netdev; | |
1092 | ofport->opp.port_no = odp_port_to_ofp_port(odp_port->port); | |
80992a35 | 1093 | netdev_get_etheraddr(netdev, ofport->opp.hw_addr); |
064af421 BP |
1094 | memcpy(ofport->opp.name, odp_port->devname, |
1095 | MIN(sizeof ofport->opp.name, sizeof odp_port->devname)); | |
1096 | ofport->opp.name[sizeof ofport->opp.name - 1] = '\0'; | |
1097 | ||
1098 | netdev_get_flags(netdev, &flags); | |
1099 | ofport->opp.config = flags & NETDEV_UP ? 0 : OFPPC_PORT_DOWN; | |
1100 | ||
1101 | netdev_get_carrier(netdev, &carrier); | |
1102 | ofport->opp.state = carrier ? 0 : OFPPS_LINK_DOWN; | |
1103 | ||
1104 | netdev_get_features(netdev, | |
1105 | &ofport->opp.curr, &ofport->opp.advertised, | |
1106 | &ofport->opp.supported, &ofport->opp.peer); | |
1107 | return ofport; | |
1108 | } | |
1109 | ||
1110 | static bool | |
1111 | ofport_conflicts(const struct ofproto *p, const struct odp_port *odp_port) | |
1112 | { | |
1113 | if (port_array_get(&p->ports, odp_port->port)) { | |
1114 | VLOG_WARN_RL(&rl, "ignoring duplicate port %"PRIu16" in datapath", | |
1115 | odp_port->port); | |
1116 | return true; | |
1117 | } else if (shash_find(&p->port_by_name, odp_port->devname)) { | |
1118 | VLOG_WARN_RL(&rl, "ignoring duplicate device %s in datapath", | |
1119 | odp_port->devname); | |
1120 | return true; | |
1121 | } else { | |
1122 | return false; | |
1123 | } | |
1124 | } | |
1125 | ||
1126 | static int | |
1127 | ofport_equal(const struct ofport *a_, const struct ofport *b_) | |
1128 | { | |
1129 | const struct ofp_phy_port *a = &a_->opp; | |
1130 | const struct ofp_phy_port *b = &b_->opp; | |
1131 | ||
1132 | BUILD_ASSERT_DECL(sizeof *a == 48); /* Detect ofp_phy_port changes. */ | |
1133 | return (a->port_no == b->port_no | |
1134 | && !memcmp(a->hw_addr, b->hw_addr, sizeof a->hw_addr) | |
1135 | && !strcmp((char *) a->name, (char *) b->name) | |
1136 | && a->state == b->state | |
1137 | && a->config == b->config | |
1138 | && a->curr == b->curr | |
1139 | && a->advertised == b->advertised | |
1140 | && a->supported == b->supported | |
1141 | && a->peer == b->peer); | |
1142 | } | |
1143 | ||
1144 | static void | |
1145 | send_port_status(struct ofproto *p, const struct ofport *ofport, | |
1146 | uint8_t reason) | |
1147 | { | |
1148 | /* XXX Should limit the number of queued port status change messages. */ | |
1149 | struct ofconn *ofconn; | |
1150 | LIST_FOR_EACH (ofconn, struct ofconn, node, &p->all_conns) { | |
1151 | struct ofp_port_status *ops; | |
1152 | struct ofpbuf *b; | |
1153 | ||
1154 | ops = make_openflow_xid(sizeof *ops, OFPT_PORT_STATUS, 0, &b); | |
1155 | ops->reason = reason; | |
1156 | ops->desc = ofport->opp; | |
1157 | hton_ofp_phy_port(&ops->desc); | |
1158 | queue_tx(b, ofconn, NULL); | |
1159 | } | |
1160 | if (p->ofhooks->port_changed_cb) { | |
1161 | p->ofhooks->port_changed_cb(reason, &ofport->opp, p->aux); | |
1162 | } | |
1163 | } | |
1164 | ||
1165 | static void | |
1166 | ofport_install(struct ofproto *p, struct ofport *ofport) | |
1167 | { | |
e9e28be3 | 1168 | netdev_monitor_add(p->netdev_monitor, ofport->netdev); |
064af421 BP |
1169 | port_array_set(&p->ports, ofp_port_to_odp_port(ofport->opp.port_no), |
1170 | ofport); | |
1171 | shash_add(&p->port_by_name, (char *) ofport->opp.name, ofport); | |
1172 | } | |
1173 | ||
1174 | static void | |
1175 | ofport_remove(struct ofproto *p, struct ofport *ofport) | |
1176 | { | |
e9e28be3 | 1177 | netdev_monitor_remove(p->netdev_monitor, ofport->netdev); |
064af421 BP |
1178 | port_array_set(&p->ports, ofp_port_to_odp_port(ofport->opp.port_no), NULL); |
1179 | shash_delete(&p->port_by_name, | |
1180 | shash_find(&p->port_by_name, (char *) ofport->opp.name)); | |
1181 | } | |
1182 | ||
1183 | static void | |
1184 | ofport_free(struct ofport *ofport) | |
1185 | { | |
1186 | if (ofport) { | |
1187 | netdev_close(ofport->netdev); | |
1188 | free(ofport); | |
1189 | } | |
1190 | } | |
1191 | ||
1192 | static void | |
1193 | update_port(struct ofproto *p, const char *devname) | |
1194 | { | |
1195 | struct odp_port odp_port; | |
c874dc6d BP |
1196 | struct ofport *old_ofport; |
1197 | struct ofport *new_ofport; | |
064af421 BP |
1198 | int error; |
1199 | ||
1200 | COVERAGE_INC(ofproto_update_port); | |
c874dc6d BP |
1201 | |
1202 | /* Query the datapath for port information. */ | |
c228a364 | 1203 | error = dpif_port_query_by_name(p->dpif, devname, &odp_port); |
064af421 | 1204 | |
c874dc6d BP |
1205 | /* Find the old ofport. */ |
1206 | old_ofport = shash_find_data(&p->port_by_name, devname); | |
1207 | if (!error) { | |
1208 | if (!old_ofport) { | |
1209 | /* There's no port named 'devname' but there might be a port with | |
1210 | * the same port number. This could happen if a port is deleted | |
1211 | * and then a new one added in its place very quickly, or if a port | |
1212 | * is renamed. In the former case we want to send an OFPPR_DELETE | |
1213 | * and an OFPPR_ADD, and in the latter case we want to send a | |
1214 | * single OFPPR_MODIFY. We can distinguish the cases by comparing | |
1215 | * the old port's ifindex against the new port, or perhaps less | |
1216 | * reliably but more portably by comparing the old port's MAC | |
1217 | * against the new port's MAC. However, this code isn't that smart | |
1218 | * and always sends an OFPPR_MODIFY (XXX). */ | |
1219 | old_ofport = port_array_get(&p->ports, odp_port.port); | |
064af421 | 1220 | } |
c874dc6d | 1221 | } else if (error != ENOENT && error != ENODEV) { |
064af421 BP |
1222 | VLOG_WARN_RL(&rl, "dpif_port_query_by_name returned unexpected error " |
1223 | "%s", strerror(error)); | |
1224 | return; | |
1225 | } | |
c874dc6d BP |
1226 | |
1227 | /* Create a new ofport. */ | |
1228 | new_ofport = !error ? make_ofport(&odp_port) : NULL; | |
1229 | ||
1230 | /* Eliminate a few pathological cases. */ | |
1231 | if (!old_ofport && !new_ofport) { | |
1232 | return; | |
1233 | } else if (old_ofport && new_ofport) { | |
1234 | /* Most of the 'config' bits are OpenFlow soft state, but | |
1235 | * OFPPC_PORT_DOWN is maintained the kernel. So transfer the OpenFlow | |
1236 | * bits from old_ofport. (make_ofport() only sets OFPPC_PORT_DOWN and | |
1237 | * leaves the other bits 0.) */ | |
1238 | new_ofport->opp.config |= old_ofport->opp.config & ~OFPPC_PORT_DOWN; | |
1239 | ||
1240 | if (ofport_equal(old_ofport, new_ofport)) { | |
1241 | /* False alarm--no change. */ | |
1242 | ofport_free(new_ofport); | |
1243 | return; | |
1244 | } | |
1245 | } | |
1246 | ||
1247 | /* Now deal with the normal cases. */ | |
1248 | if (old_ofport) { | |
1249 | ofport_remove(p, old_ofport); | |
1250 | } | |
1251 | if (new_ofport) { | |
1252 | ofport_install(p, new_ofport); | |
1253 | } | |
1254 | send_port_status(p, new_ofport ? new_ofport : old_ofport, | |
1255 | (!old_ofport ? OFPPR_ADD | |
1256 | : !new_ofport ? OFPPR_DELETE | |
1257 | : OFPPR_MODIFY)); | |
1258 | ofport_free(old_ofport); | |
1259 | ||
1260 | /* Update port groups. */ | |
064af421 BP |
1261 | refresh_port_groups(p); |
1262 | } | |
1263 | ||
1264 | static int | |
1265 | init_ports(struct ofproto *p) | |
1266 | { | |
1267 | struct odp_port *ports; | |
1268 | size_t n_ports; | |
1269 | size_t i; | |
1270 | int error; | |
1271 | ||
c228a364 | 1272 | error = dpif_port_list(p->dpif, &ports, &n_ports); |
064af421 BP |
1273 | if (error) { |
1274 | return error; | |
1275 | } | |
1276 | ||
1277 | for (i = 0; i < n_ports; i++) { | |
1278 | const struct odp_port *odp_port = &ports[i]; | |
1279 | if (!ofport_conflicts(p, odp_port)) { | |
1280 | struct ofport *ofport = make_ofport(odp_port); | |
1281 | if (ofport) { | |
1282 | ofport_install(p, ofport); | |
1283 | } | |
1284 | } | |
1285 | } | |
1286 | free(ports); | |
1287 | refresh_port_groups(p); | |
1288 | return 0; | |
1289 | } | |
1290 | \f | |
1291 | static struct ofconn * | |
1292 | ofconn_create(struct ofproto *p, struct rconn *rconn) | |
1293 | { | |
1294 | struct ofconn *ofconn = xmalloc(sizeof *ofconn); | |
1295 | list_push_back(&p->all_conns, &ofconn->node); | |
1296 | ofconn->rconn = rconn; | |
1297 | ofconn->pktbuf = NULL; | |
1298 | ofconn->send_flow_exp = false; | |
1299 | ofconn->miss_send_len = 0; | |
1300 | ofconn->packet_in_counter = rconn_packet_counter_create (); | |
1301 | ofconn->reply_counter = rconn_packet_counter_create (); | |
1302 | return ofconn; | |
1303 | } | |
1304 | ||
1305 | static void | |
c475ae67 | 1306 | ofconn_destroy(struct ofconn *ofconn) |
064af421 | 1307 | { |
064af421 BP |
1308 | list_remove(&ofconn->node); |
1309 | rconn_destroy(ofconn->rconn); | |
1310 | rconn_packet_counter_destroy(ofconn->packet_in_counter); | |
1311 | rconn_packet_counter_destroy(ofconn->reply_counter); | |
1312 | pktbuf_destroy(ofconn->pktbuf); | |
1313 | free(ofconn); | |
1314 | } | |
1315 | ||
1316 | static void | |
1317 | ofconn_run(struct ofconn *ofconn, struct ofproto *p) | |
1318 | { | |
1319 | int iteration; | |
1320 | ||
1321 | rconn_run(ofconn->rconn); | |
1322 | ||
1323 | if (rconn_packet_counter_read (ofconn->reply_counter) < OFCONN_REPLY_MAX) { | |
1324 | /* Limit the number of iterations to prevent other tasks from | |
1325 | * starving. */ | |
1326 | for (iteration = 0; iteration < 50; iteration++) { | |
1327 | struct ofpbuf *of_msg = rconn_recv(ofconn->rconn); | |
1328 | if (!of_msg) { | |
1329 | break; | |
1330 | } | |
7778bd15 BP |
1331 | if (p->fail_open) { |
1332 | fail_open_maybe_recover(p->fail_open); | |
1333 | } | |
064af421 BP |
1334 | handle_openflow(ofconn, p, of_msg); |
1335 | ofpbuf_delete(of_msg); | |
1336 | } | |
1337 | } | |
1338 | ||
1339 | if (ofconn != p->controller && !rconn_is_alive(ofconn->rconn)) { | |
c475ae67 | 1340 | ofconn_destroy(ofconn); |
064af421 BP |
1341 | } |
1342 | } | |
1343 | ||
1344 | static void | |
1345 | ofconn_wait(struct ofconn *ofconn) | |
1346 | { | |
1347 | rconn_run_wait(ofconn->rconn); | |
1348 | if (rconn_packet_counter_read (ofconn->reply_counter) < OFCONN_REPLY_MAX) { | |
1349 | rconn_recv_wait(ofconn->rconn); | |
1350 | } else { | |
1351 | COVERAGE_INC(ofproto_ofconn_stuck); | |
1352 | } | |
1353 | } | |
1354 | \f | |
1355 | /* Caller is responsible for initializing the 'cr' member of the returned | |
1356 | * rule. */ | |
1357 | static struct rule * | |
0193b2af | 1358 | rule_create(struct ofproto *ofproto, struct rule *super, |
064af421 BP |
1359 | const union ofp_action *actions, size_t n_actions, |
1360 | uint16_t idle_timeout, uint16_t hard_timeout) | |
1361 | { | |
ec6fde61 | 1362 | struct rule *rule = xzalloc(sizeof *rule); |
064af421 BP |
1363 | rule->idle_timeout = idle_timeout; |
1364 | rule->hard_timeout = hard_timeout; | |
1365 | rule->used = rule->created = time_msec(); | |
1366 | rule->super = super; | |
1367 | if (super) { | |
1368 | list_push_back(&super->list, &rule->list); | |
1369 | } else { | |
1370 | list_init(&rule->list); | |
1371 | } | |
1372 | rule->n_actions = n_actions; | |
1373 | rule->actions = xmemdup(actions, n_actions * sizeof *actions); | |
0193b2af JG |
1374 | netflow_flow_clear(&rule->nf_flow); |
1375 | netflow_flow_update_time(ofproto->netflow, &rule->nf_flow, rule->created); | |
1376 | ||
064af421 BP |
1377 | return rule; |
1378 | } | |
1379 | ||
1380 | static struct rule * | |
1381 | rule_from_cls_rule(const struct cls_rule *cls_rule) | |
1382 | { | |
1383 | return cls_rule ? CONTAINER_OF(cls_rule, struct rule, cr) : NULL; | |
1384 | } | |
1385 | ||
1386 | static void | |
1387 | rule_free(struct rule *rule) | |
1388 | { | |
1389 | free(rule->actions); | |
1390 | free(rule->odp_actions); | |
1391 | free(rule); | |
1392 | } | |
1393 | ||
1394 | /* Destroys 'rule'. If 'rule' is a subrule, also removes it from its | |
1395 | * super-rule's list of subrules. If 'rule' is a super-rule, also iterates | |
1396 | * through all of its subrules and revalidates them, destroying any that no | |
1397 | * longer has a super-rule (which is probably all of them). | |
1398 | * | |
1399 | * Before calling this function, the caller must make have removed 'rule' from | |
1400 | * the classifier. If 'rule' is an exact-match rule, the caller is also | |
1401 | * responsible for ensuring that it has been uninstalled from the datapath. */ | |
1402 | static void | |
1403 | rule_destroy(struct ofproto *ofproto, struct rule *rule) | |
1404 | { | |
1405 | if (!rule->super) { | |
1406 | struct rule *subrule, *next; | |
1407 | LIST_FOR_EACH_SAFE (subrule, next, struct rule, list, &rule->list) { | |
1408 | revalidate_rule(ofproto, subrule); | |
1409 | } | |
1410 | } else { | |
1411 | list_remove(&rule->list); | |
1412 | } | |
1413 | rule_free(rule); | |
1414 | } | |
1415 | ||
1416 | static bool | |
1417 | rule_has_out_port(const struct rule *rule, uint16_t out_port) | |
1418 | { | |
1419 | const union ofp_action *oa; | |
1420 | struct actions_iterator i; | |
1421 | ||
1422 | if (out_port == htons(OFPP_NONE)) { | |
1423 | return true; | |
1424 | } | |
1425 | for (oa = actions_first(&i, rule->actions, rule->n_actions); oa; | |
1426 | oa = actions_next(&i)) { | |
1427 | if (oa->type == htons(OFPAT_OUTPUT) && oa->output.port == out_port) { | |
1428 | return true; | |
1429 | } | |
1430 | } | |
1431 | return false; | |
1432 | } | |
1433 | ||
1434 | /* Executes the actions indicated by 'rule' on 'packet', which is in flow | |
1435 | * 'flow' and is considered to have arrived on ODP port 'in_port'. | |
1436 | * | |
1437 | * The flow that 'packet' actually contains does not need to actually match | |
1438 | * 'rule'; the actions in 'rule' will be applied to it either way. Likewise, | |
1439 | * the packet and byte counters for 'rule' will be credited for the packet sent | |
1440 | * out whether or not the packet actually matches 'rule'. | |
1441 | * | |
1442 | * If 'rule' is an exact-match rule and 'flow' actually equals the rule's flow, | |
1443 | * the caller must already have accurately composed ODP actions for it given | |
1444 | * 'packet' using rule_make_actions(). If 'rule' is a wildcard rule, or if | |
1445 | * 'rule' is an exact-match rule but 'flow' is not the rule's flow, then this | |
1446 | * function will compose a set of ODP actions based on 'rule''s OpenFlow | |
1447 | * actions and apply them to 'packet'. */ | |
1448 | static void | |
1449 | rule_execute(struct ofproto *ofproto, struct rule *rule, | |
1450 | struct ofpbuf *packet, const flow_t *flow) | |
1451 | { | |
1452 | const union odp_action *actions; | |
1453 | size_t n_actions; | |
1454 | struct odp_actions a; | |
1455 | ||
1456 | /* Grab or compose the ODP actions. | |
1457 | * | |
1458 | * The special case for an exact-match 'rule' where 'flow' is not the | |
1459 | * rule's flow is important to avoid, e.g., sending a packet out its input | |
1460 | * port simply because the ODP actions were composed for the wrong | |
1461 | * scenario. */ | |
1462 | if (rule->cr.wc.wildcards || !flow_equal(flow, &rule->cr.flow)) { | |
1463 | struct rule *super = rule->super ? rule->super : rule; | |
1464 | if (xlate_actions(super->actions, super->n_actions, flow, ofproto, | |
6a07af36 | 1465 | packet, &a, NULL, 0, NULL)) { |
064af421 BP |
1466 | return; |
1467 | } | |
1468 | actions = a.actions; | |
1469 | n_actions = a.n_actions; | |
1470 | } else { | |
1471 | actions = rule->odp_actions; | |
1472 | n_actions = rule->n_odp_actions; | |
1473 | } | |
1474 | ||
1475 | /* Execute the ODP actions. */ | |
c228a364 | 1476 | if (!dpif_execute(ofproto->dpif, flow->in_port, |
064af421 BP |
1477 | actions, n_actions, packet)) { |
1478 | struct odp_flow_stats stats; | |
1479 | flow_extract_stats(flow, packet, &stats); | |
0193b2af | 1480 | update_stats(ofproto, rule, &stats); |
064af421 | 1481 | rule->used = time_msec(); |
0193b2af | 1482 | netflow_flow_update_time(ofproto->netflow, &rule->nf_flow, rule->used); |
064af421 BP |
1483 | } |
1484 | } | |
1485 | ||
1486 | static void | |
1487 | rule_insert(struct ofproto *p, struct rule *rule, struct ofpbuf *packet, | |
1488 | uint16_t in_port) | |
1489 | { | |
1490 | struct rule *displaced_rule; | |
1491 | ||
1492 | /* Insert the rule in the classifier. */ | |
1493 | displaced_rule = rule_from_cls_rule(classifier_insert(&p->cls, &rule->cr)); | |
1494 | if (!rule->cr.wc.wildcards) { | |
1495 | rule_make_actions(p, rule, packet); | |
1496 | } | |
1497 | ||
1498 | /* Send the packet and credit it to the rule. */ | |
1499 | if (packet) { | |
1500 | flow_t flow; | |
1501 | flow_extract(packet, in_port, &flow); | |
1502 | rule_execute(p, rule, packet, &flow); | |
1503 | } | |
1504 | ||
1505 | /* Install the rule in the datapath only after sending the packet, to | |
1506 | * avoid packet reordering. */ | |
1507 | if (rule->cr.wc.wildcards) { | |
1508 | COVERAGE_INC(ofproto_add_wc_flow); | |
1509 | p->need_revalidate = true; | |
1510 | } else { | |
1511 | rule_install(p, rule, displaced_rule); | |
1512 | } | |
1513 | ||
1514 | /* Free the rule that was displaced, if any. */ | |
1515 | if (displaced_rule) { | |
1516 | rule_destroy(p, displaced_rule); | |
1517 | } | |
1518 | } | |
1519 | ||
1520 | static struct rule * | |
1521 | rule_create_subrule(struct ofproto *ofproto, struct rule *rule, | |
1522 | const flow_t *flow) | |
1523 | { | |
0193b2af | 1524 | struct rule *subrule = rule_create(ofproto, rule, NULL, 0, |
064af421 BP |
1525 | rule->idle_timeout, rule->hard_timeout); |
1526 | COVERAGE_INC(ofproto_subrule_create); | |
1527 | cls_rule_from_flow(&subrule->cr, flow, 0, | |
1528 | (rule->cr.priority <= UINT16_MAX ? UINT16_MAX | |
1529 | : rule->cr.priority)); | |
1530 | classifier_insert_exact(&ofproto->cls, &subrule->cr); | |
1531 | ||
1532 | return subrule; | |
1533 | } | |
1534 | ||
1535 | static void | |
1536 | rule_remove(struct ofproto *ofproto, struct rule *rule) | |
1537 | { | |
1538 | if (rule->cr.wc.wildcards) { | |
1539 | COVERAGE_INC(ofproto_del_wc_flow); | |
1540 | ofproto->need_revalidate = true; | |
1541 | } else { | |
1542 | rule_uninstall(ofproto, rule); | |
1543 | } | |
1544 | classifier_remove(&ofproto->cls, &rule->cr); | |
1545 | rule_destroy(ofproto, rule); | |
1546 | } | |
1547 | ||
1548 | /* Returns true if the actions changed, false otherwise. */ | |
1549 | static bool | |
1550 | rule_make_actions(struct ofproto *p, struct rule *rule, | |
1551 | const struct ofpbuf *packet) | |
1552 | { | |
1553 | const struct rule *super; | |
1554 | struct odp_actions a; | |
1555 | size_t actions_len; | |
1556 | ||
1557 | assert(!rule->cr.wc.wildcards); | |
1558 | ||
1559 | super = rule->super ? rule->super : rule; | |
1560 | rule->tags = 0; | |
1561 | xlate_actions(super->actions, super->n_actions, &rule->cr.flow, p, | |
6a07af36 | 1562 | packet, &a, &rule->tags, &rule->may_install, |
0193b2af | 1563 | &rule->nf_flow.output_iface); |
064af421 BP |
1564 | |
1565 | actions_len = a.n_actions * sizeof *a.actions; | |
1566 | if (rule->n_odp_actions != a.n_actions | |
1567 | || memcmp(rule->odp_actions, a.actions, actions_len)) { | |
1568 | COVERAGE_INC(ofproto_odp_unchanged); | |
1569 | free(rule->odp_actions); | |
1570 | rule->n_odp_actions = a.n_actions; | |
1571 | rule->odp_actions = xmemdup(a.actions, actions_len); | |
1572 | return true; | |
1573 | } else { | |
1574 | return false; | |
1575 | } | |
1576 | } | |
1577 | ||
1578 | static int | |
1579 | do_put_flow(struct ofproto *ofproto, struct rule *rule, int flags, | |
1580 | struct odp_flow_put *put) | |
1581 | { | |
1582 | memset(&put->flow.stats, 0, sizeof put->flow.stats); | |
1583 | put->flow.key = rule->cr.flow; | |
1584 | put->flow.actions = rule->odp_actions; | |
1585 | put->flow.n_actions = rule->n_odp_actions; | |
1586 | put->flags = flags; | |
c228a364 | 1587 | return dpif_flow_put(ofproto->dpif, put); |
064af421 BP |
1588 | } |
1589 | ||
1590 | static void | |
1591 | rule_install(struct ofproto *p, struct rule *rule, struct rule *displaced_rule) | |
1592 | { | |
1593 | assert(!rule->cr.wc.wildcards); | |
1594 | ||
1595 | if (rule->may_install) { | |
1596 | struct odp_flow_put put; | |
1597 | if (!do_put_flow(p, rule, | |
1598 | ODPPF_CREATE | ODPPF_MODIFY | ODPPF_ZERO_STATS, | |
1599 | &put)) { | |
1600 | rule->installed = true; | |
1601 | if (displaced_rule) { | |
14986b31 | 1602 | update_stats(p, displaced_rule, &put.flow.stats); |
064af421 BP |
1603 | rule_post_uninstall(p, displaced_rule); |
1604 | } | |
1605 | } | |
1606 | } else if (displaced_rule) { | |
1607 | rule_uninstall(p, displaced_rule); | |
1608 | } | |
1609 | } | |
1610 | ||
1611 | static void | |
1612 | rule_reinstall(struct ofproto *ofproto, struct rule *rule) | |
1613 | { | |
1614 | if (rule->installed) { | |
1615 | struct odp_flow_put put; | |
1616 | COVERAGE_INC(ofproto_dp_missed); | |
1617 | do_put_flow(ofproto, rule, ODPPF_CREATE | ODPPF_MODIFY, &put); | |
1618 | } else { | |
1619 | rule_install(ofproto, rule, NULL); | |
1620 | } | |
1621 | } | |
1622 | ||
1623 | static void | |
1624 | rule_update_actions(struct ofproto *ofproto, struct rule *rule) | |
1625 | { | |
42c3641c JG |
1626 | bool actions_changed; |
1627 | uint16_t new_out_iface, old_out_iface; | |
1628 | ||
1629 | old_out_iface = rule->nf_flow.output_iface; | |
1630 | actions_changed = rule_make_actions(ofproto, rule, NULL); | |
1631 | ||
064af421 BP |
1632 | if (rule->may_install) { |
1633 | if (rule->installed) { | |
1634 | if (actions_changed) { | |
064af421 | 1635 | struct odp_flow_put put; |
42c3641c JG |
1636 | do_put_flow(ofproto, rule, ODPPF_CREATE | ODPPF_MODIFY |
1637 | | ODPPF_ZERO_STATS, &put); | |
1638 | update_stats(ofproto, rule, &put.flow.stats); | |
1639 | ||
1640 | /* Temporarily set the old output iface so that NetFlow | |
1641 | * messages have the correct output interface for the old | |
1642 | * stats. */ | |
1643 | new_out_iface = rule->nf_flow.output_iface; | |
1644 | rule->nf_flow.output_iface = old_out_iface; | |
1645 | rule_post_uninstall(ofproto, rule); | |
1646 | rule->nf_flow.output_iface = new_out_iface; | |
064af421 BP |
1647 | } |
1648 | } else { | |
1649 | rule_install(ofproto, rule, NULL); | |
1650 | } | |
1651 | } else { | |
1652 | rule_uninstall(ofproto, rule); | |
1653 | } | |
1654 | } | |
1655 | ||
1656 | static void | |
1657 | rule_account(struct ofproto *ofproto, struct rule *rule, uint64_t extra_bytes) | |
1658 | { | |
1659 | uint64_t total_bytes = rule->byte_count + extra_bytes; | |
1660 | ||
1661 | if (ofproto->ofhooks->account_flow_cb | |
1662 | && total_bytes > rule->accounted_bytes) | |
1663 | { | |
1664 | ofproto->ofhooks->account_flow_cb( | |
1665 | &rule->cr.flow, rule->odp_actions, rule->n_odp_actions, | |
1666 | total_bytes - rule->accounted_bytes, ofproto->aux); | |
1667 | rule->accounted_bytes = total_bytes; | |
1668 | } | |
1669 | } | |
1670 | ||
1671 | static void | |
1672 | rule_uninstall(struct ofproto *p, struct rule *rule) | |
1673 | { | |
1674 | assert(!rule->cr.wc.wildcards); | |
1675 | if (rule->installed) { | |
1676 | struct odp_flow odp_flow; | |
1677 | ||
1678 | odp_flow.key = rule->cr.flow; | |
1679 | odp_flow.actions = NULL; | |
1680 | odp_flow.n_actions = 0; | |
c228a364 | 1681 | if (!dpif_flow_del(p->dpif, &odp_flow)) { |
0193b2af | 1682 | update_stats(p, rule, &odp_flow.stats); |
064af421 BP |
1683 | } |
1684 | rule->installed = false; | |
1685 | ||
1686 | rule_post_uninstall(p, rule); | |
1687 | } | |
1688 | } | |
1689 | ||
0193b2af JG |
1690 | static bool |
1691 | is_controller_rule(struct rule *rule) | |
1692 | { | |
1693 | /* If the only action is send to the controller then don't report | |
1694 | * NetFlow expiration messages since it is just part of the control | |
1695 | * logic for the network and not real traffic. */ | |
1696 | ||
1697 | if (rule && rule->super) { | |
1698 | struct rule *super = rule->super; | |
1699 | ||
1700 | return super->n_actions == 1 && | |
1701 | super->actions[0].type == htons(OFPAT_OUTPUT) && | |
1702 | super->actions[0].output.port == htons(OFPP_CONTROLLER); | |
1703 | } | |
1704 | ||
1705 | return false; | |
1706 | } | |
1707 | ||
064af421 BP |
1708 | static void |
1709 | rule_post_uninstall(struct ofproto *ofproto, struct rule *rule) | |
1710 | { | |
1711 | struct rule *super = rule->super; | |
1712 | ||
1713 | rule_account(ofproto, rule, 0); | |
6a07af36 | 1714 | |
0193b2af | 1715 | if (ofproto->netflow && !is_controller_rule(rule)) { |
064af421 BP |
1716 | struct ofexpired expired; |
1717 | expired.flow = rule->cr.flow; | |
1718 | expired.packet_count = rule->packet_count; | |
1719 | expired.byte_count = rule->byte_count; | |
1720 | expired.used = rule->used; | |
0193b2af | 1721 | netflow_expire(ofproto->netflow, &rule->nf_flow, &expired); |
064af421 BP |
1722 | } |
1723 | if (super) { | |
1724 | super->packet_count += rule->packet_count; | |
1725 | super->byte_count += rule->byte_count; | |
064af421 | 1726 | |
0c0afbec JG |
1727 | /* Reset counters to prevent double counting if the rule ever gets |
1728 | * reinstalled. */ | |
1729 | rule->packet_count = 0; | |
1730 | rule->byte_count = 0; | |
1731 | rule->accounted_bytes = 0; | |
0193b2af JG |
1732 | |
1733 | netflow_flow_clear(&rule->nf_flow); | |
0c0afbec | 1734 | } |
064af421 BP |
1735 | } |
1736 | \f | |
1737 | static void | |
1738 | queue_tx(struct ofpbuf *msg, const struct ofconn *ofconn, | |
1739 | struct rconn_packet_counter *counter) | |
1740 | { | |
1741 | update_openflow_length(msg); | |
1742 | if (rconn_send(ofconn->rconn, msg, counter)) { | |
1743 | ofpbuf_delete(msg); | |
1744 | } | |
1745 | } | |
1746 | ||
1747 | static void | |
1748 | send_error(const struct ofconn *ofconn, const struct ofp_header *oh, | |
1749 | int error, const void *data, size_t len) | |
1750 | { | |
1751 | struct ofpbuf *buf; | |
1752 | struct ofp_error_msg *oem; | |
1753 | ||
1754 | if (!(error >> 16)) { | |
1755 | VLOG_WARN_RL(&rl, "not sending bad error code %d to controller", | |
1756 | error); | |
1757 | return; | |
1758 | } | |
1759 | ||
1760 | COVERAGE_INC(ofproto_error); | |
1761 | oem = make_openflow_xid(len + sizeof *oem, OFPT_ERROR, | |
1762 | oh ? oh->xid : 0, &buf); | |
1763 | oem->type = htons((unsigned int) error >> 16); | |
1764 | oem->code = htons(error & 0xffff); | |
1765 | memcpy(oem->data, data, len); | |
1766 | queue_tx(buf, ofconn, ofconn->reply_counter); | |
1767 | } | |
1768 | ||
1769 | static void | |
1770 | send_error_oh(const struct ofconn *ofconn, const struct ofp_header *oh, | |
1771 | int error) | |
1772 | { | |
1773 | size_t oh_length = ntohs(oh->length); | |
1774 | send_error(ofconn, oh, error, oh, MIN(oh_length, 64)); | |
1775 | } | |
1776 | ||
1777 | static void | |
1778 | hton_ofp_phy_port(struct ofp_phy_port *opp) | |
1779 | { | |
1780 | opp->port_no = htons(opp->port_no); | |
1781 | opp->config = htonl(opp->config); | |
1782 | opp->state = htonl(opp->state); | |
1783 | opp->curr = htonl(opp->curr); | |
1784 | opp->advertised = htonl(opp->advertised); | |
1785 | opp->supported = htonl(opp->supported); | |
1786 | opp->peer = htonl(opp->peer); | |
1787 | } | |
1788 | ||
1789 | static int | |
1790 | handle_echo_request(struct ofconn *ofconn, struct ofp_header *oh) | |
1791 | { | |
1792 | struct ofp_header *rq = oh; | |
1793 | queue_tx(make_echo_reply(rq), ofconn, ofconn->reply_counter); | |
1794 | return 0; | |
1795 | } | |
1796 | ||
1797 | static int | |
1798 | handle_features_request(struct ofproto *p, struct ofconn *ofconn, | |
1799 | struct ofp_header *oh) | |
1800 | { | |
1801 | struct ofp_switch_features *osf; | |
1802 | struct ofpbuf *buf; | |
1803 | unsigned int port_no; | |
1804 | struct ofport *port; | |
1805 | ||
1806 | osf = make_openflow_xid(sizeof *osf, OFPT_FEATURES_REPLY, oh->xid, &buf); | |
1807 | osf->datapath_id = htonll(p->datapath_id); | |
1808 | osf->n_buffers = htonl(pktbuf_capacity()); | |
1809 | osf->n_tables = 2; | |
1810 | osf->capabilities = htonl(OFPC_FLOW_STATS | OFPC_TABLE_STATS | | |
1811 | OFPC_PORT_STATS | OFPC_MULTI_PHY_TX); | |
1812 | osf->actions = htonl((1u << OFPAT_OUTPUT) | | |
1813 | (1u << OFPAT_SET_VLAN_VID) | | |
1814 | (1u << OFPAT_SET_VLAN_PCP) | | |
1815 | (1u << OFPAT_STRIP_VLAN) | | |
1816 | (1u << OFPAT_SET_DL_SRC) | | |
1817 | (1u << OFPAT_SET_DL_DST) | | |
1818 | (1u << OFPAT_SET_NW_SRC) | | |
1819 | (1u << OFPAT_SET_NW_DST) | | |
1820 | (1u << OFPAT_SET_TP_SRC) | | |
1821 | (1u << OFPAT_SET_TP_DST)); | |
1822 | ||
1823 | PORT_ARRAY_FOR_EACH (port, &p->ports, port_no) { | |
1824 | hton_ofp_phy_port(ofpbuf_put(buf, &port->opp, sizeof port->opp)); | |
1825 | } | |
1826 | ||
1827 | queue_tx(buf, ofconn, ofconn->reply_counter); | |
1828 | return 0; | |
1829 | } | |
1830 | ||
1831 | static int | |
1832 | handle_get_config_request(struct ofproto *p, struct ofconn *ofconn, | |
1833 | struct ofp_header *oh) | |
1834 | { | |
1835 | struct ofpbuf *buf; | |
1836 | struct ofp_switch_config *osc; | |
1837 | uint16_t flags; | |
1838 | bool drop_frags; | |
1839 | ||
1840 | /* Figure out flags. */ | |
c228a364 | 1841 | dpif_get_drop_frags(p->dpif, &drop_frags); |
064af421 BP |
1842 | flags = drop_frags ? OFPC_FRAG_DROP : OFPC_FRAG_NORMAL; |
1843 | if (ofconn->send_flow_exp) { | |
1844 | flags |= OFPC_SEND_FLOW_EXP; | |
1845 | } | |
1846 | ||
1847 | /* Send reply. */ | |
1848 | osc = make_openflow_xid(sizeof *osc, OFPT_GET_CONFIG_REPLY, oh->xid, &buf); | |
1849 | osc->flags = htons(flags); | |
1850 | osc->miss_send_len = htons(ofconn->miss_send_len); | |
1851 | queue_tx(buf, ofconn, ofconn->reply_counter); | |
1852 | ||
1853 | return 0; | |
1854 | } | |
1855 | ||
1856 | static int | |
1857 | handle_set_config(struct ofproto *p, struct ofconn *ofconn, | |
1858 | struct ofp_switch_config *osc) | |
1859 | { | |
1860 | uint16_t flags; | |
1861 | int error; | |
1862 | ||
1863 | error = check_ofp_message(&osc->header, OFPT_SET_CONFIG, sizeof *osc); | |
1864 | if (error) { | |
1865 | return error; | |
1866 | } | |
1867 | flags = ntohs(osc->flags); | |
1868 | ||
1869 | ofconn->send_flow_exp = (flags & OFPC_SEND_FLOW_EXP) != 0; | |
1870 | ||
1871 | if (ofconn == p->controller) { | |
1872 | switch (flags & OFPC_FRAG_MASK) { | |
1873 | case OFPC_FRAG_NORMAL: | |
c228a364 | 1874 | dpif_set_drop_frags(p->dpif, false); |
064af421 BP |
1875 | break; |
1876 | case OFPC_FRAG_DROP: | |
c228a364 | 1877 | dpif_set_drop_frags(p->dpif, true); |
064af421 BP |
1878 | break; |
1879 | default: | |
1880 | VLOG_WARN_RL(&rl, "requested bad fragment mode (flags=%"PRIx16")", | |
1881 | osc->flags); | |
1882 | break; | |
1883 | } | |
1884 | } | |
1885 | ||
1886 | if ((ntohs(osc->miss_send_len) != 0) != (ofconn->miss_send_len != 0)) { | |
1887 | if (ntohs(osc->miss_send_len) != 0) { | |
1888 | ofconn->pktbuf = pktbuf_create(); | |
1889 | } else { | |
1890 | pktbuf_destroy(ofconn->pktbuf); | |
1891 | } | |
1892 | } | |
1893 | ||
1894 | ofconn->miss_send_len = ntohs(osc->miss_send_len); | |
1895 | ||
1896 | return 0; | |
1897 | } | |
1898 | ||
1899 | static void | |
6a07af36 JG |
1900 | add_output_group_action(struct odp_actions *actions, uint16_t group, |
1901 | uint16_t *nf_output_iface) | |
064af421 BP |
1902 | { |
1903 | odp_actions_add(actions, ODPAT_OUTPUT_GROUP)->output_group.group = group; | |
6a07af36 JG |
1904 | |
1905 | if (group == DP_GROUP_ALL || group == DP_GROUP_FLOOD) { | |
1906 | *nf_output_iface = NF_OUT_FLOOD; | |
1907 | } | |
064af421 BP |
1908 | } |
1909 | ||
1910 | static void | |
1911 | add_controller_action(struct odp_actions *actions, | |
1912 | const struct ofp_action_output *oao) | |
1913 | { | |
1914 | union odp_action *a = odp_actions_add(actions, ODPAT_CONTROLLER); | |
1915 | a->controller.arg = oao->max_len ? ntohs(oao->max_len) : UINT32_MAX; | |
1916 | } | |
1917 | ||
1918 | struct action_xlate_ctx { | |
1919 | /* Input. */ | |
1920 | const flow_t *flow; /* Flow to which these actions correspond. */ | |
1921 | int recurse; /* Recursion level, via xlate_table_action. */ | |
1922 | struct ofproto *ofproto; | |
1923 | const struct ofpbuf *packet; /* The packet corresponding to 'flow', or a | |
1924 | * null pointer if we are revalidating | |
1925 | * without a packet to refer to. */ | |
1926 | ||
1927 | /* Output. */ | |
1928 | struct odp_actions *out; /* Datapath actions. */ | |
1929 | tag_type *tags; /* Tags associated with OFPP_NORMAL actions. */ | |
d6fbec6d | 1930 | bool may_set_up_flow; /* True ordinarily; false if the actions must |
064af421 | 1931 | * be reassessed for every packet. */ |
6a07af36 | 1932 | uint16_t nf_output_iface; /* Output interface index for NetFlow. */ |
064af421 BP |
1933 | }; |
1934 | ||
1935 | static void do_xlate_actions(const union ofp_action *in, size_t n_in, | |
1936 | struct action_xlate_ctx *ctx); | |
1937 | ||
1938 | static void | |
1939 | add_output_action(struct action_xlate_ctx *ctx, uint16_t port) | |
1940 | { | |
1941 | const struct ofport *ofport = port_array_get(&ctx->ofproto->ports, port); | |
6cfaf517 BP |
1942 | |
1943 | if (ofport) { | |
1944 | if (ofport->opp.config & OFPPC_NO_FWD) { | |
1945 | /* Forwarding disabled on port. */ | |
1946 | return; | |
1947 | } | |
1948 | } else { | |
1949 | /* | |
1950 | * We don't have an ofport record for this port, but it doesn't hurt to | |
1951 | * allow forwarding to it anyhow. Maybe such a port will appear later | |
1952 | * and we're pre-populating the flow table. | |
1953 | */ | |
064af421 | 1954 | } |
6cfaf517 BP |
1955 | |
1956 | odp_actions_add(ctx->out, ODPAT_OUTPUT)->output.port = port; | |
6a07af36 | 1957 | ctx->nf_output_iface = port; |
064af421 BP |
1958 | } |
1959 | ||
1960 | static struct rule * | |
1961 | lookup_valid_rule(struct ofproto *ofproto, const flow_t *flow) | |
1962 | { | |
1963 | struct rule *rule; | |
1964 | rule = rule_from_cls_rule(classifier_lookup(&ofproto->cls, flow)); | |
1965 | ||
1966 | /* The rule we found might not be valid, since we could be in need of | |
1967 | * revalidation. If it is not valid, don't return it. */ | |
1968 | if (rule | |
1969 | && rule->super | |
1970 | && ofproto->need_revalidate | |
1971 | && !revalidate_rule(ofproto, rule)) { | |
1972 | COVERAGE_INC(ofproto_invalidated); | |
1973 | return NULL; | |
1974 | } | |
1975 | ||
1976 | return rule; | |
1977 | } | |
1978 | ||
1979 | static void | |
1980 | xlate_table_action(struct action_xlate_ctx *ctx, uint16_t in_port) | |
1981 | { | |
1982 | if (!ctx->recurse) { | |
1983 | struct rule *rule; | |
1984 | flow_t flow; | |
1985 | ||
1986 | flow = *ctx->flow; | |
1987 | flow.in_port = in_port; | |
1988 | ||
1989 | rule = lookup_valid_rule(ctx->ofproto, &flow); | |
1990 | if (rule) { | |
1991 | if (rule->super) { | |
1992 | rule = rule->super; | |
1993 | } | |
1994 | ||
1995 | ctx->recurse++; | |
1996 | do_xlate_actions(rule->actions, rule->n_actions, ctx); | |
1997 | ctx->recurse--; | |
1998 | } | |
1999 | } | |
2000 | } | |
2001 | ||
2002 | static void | |
2003 | xlate_output_action(struct action_xlate_ctx *ctx, | |
2004 | const struct ofp_action_output *oao) | |
2005 | { | |
2006 | uint16_t odp_port; | |
6a07af36 JG |
2007 | uint16_t prev_nf_output_iface = ctx->nf_output_iface; |
2008 | ||
2009 | ctx->nf_output_iface = NF_OUT_DROP; | |
064af421 BP |
2010 | |
2011 | switch (ntohs(oao->port)) { | |
2012 | case OFPP_IN_PORT: | |
2013 | add_output_action(ctx, ctx->flow->in_port); | |
2014 | break; | |
2015 | case OFPP_TABLE: | |
2016 | xlate_table_action(ctx, ctx->flow->in_port); | |
2017 | break; | |
2018 | case OFPP_NORMAL: | |
2019 | if (!ctx->ofproto->ofhooks->normal_cb(ctx->flow, ctx->packet, | |
2020 | ctx->out, ctx->tags, | |
6a07af36 | 2021 | &ctx->nf_output_iface, |
064af421 BP |
2022 | ctx->ofproto->aux)) { |
2023 | COVERAGE_INC(ofproto_uninstallable); | |
d6fbec6d | 2024 | ctx->may_set_up_flow = false; |
064af421 BP |
2025 | } |
2026 | break; | |
2027 | case OFPP_FLOOD: | |
6a07af36 JG |
2028 | add_output_group_action(ctx->out, DP_GROUP_FLOOD, |
2029 | &ctx->nf_output_iface); | |
064af421 BP |
2030 | break; |
2031 | case OFPP_ALL: | |
6a07af36 | 2032 | add_output_group_action(ctx->out, DP_GROUP_ALL, &ctx->nf_output_iface); |
064af421 BP |
2033 | break; |
2034 | case OFPP_CONTROLLER: | |
2035 | add_controller_action(ctx->out, oao); | |
2036 | break; | |
2037 | case OFPP_LOCAL: | |
2038 | add_output_action(ctx, ODPP_LOCAL); | |
2039 | break; | |
2040 | default: | |
2041 | odp_port = ofp_port_to_odp_port(ntohs(oao->port)); | |
2042 | if (odp_port != ctx->flow->in_port) { | |
2043 | add_output_action(ctx, odp_port); | |
2044 | } | |
2045 | break; | |
2046 | } | |
6a07af36 JG |
2047 | |
2048 | if (prev_nf_output_iface == NF_OUT_FLOOD) { | |
2049 | ctx->nf_output_iface = NF_OUT_FLOOD; | |
2050 | } else if (ctx->nf_output_iface == NF_OUT_DROP) { | |
2051 | ctx->nf_output_iface = prev_nf_output_iface; | |
2052 | } else if (prev_nf_output_iface != NF_OUT_DROP && | |
2053 | ctx->nf_output_iface != NF_OUT_FLOOD) { | |
2054 | ctx->nf_output_iface = NF_OUT_MULTI; | |
2055 | } | |
064af421 BP |
2056 | } |
2057 | ||
2058 | static void | |
2059 | xlate_nicira_action(struct action_xlate_ctx *ctx, | |
2060 | const struct nx_action_header *nah) | |
2061 | { | |
2062 | const struct nx_action_resubmit *nar; | |
2063 | int subtype = ntohs(nah->subtype); | |
2064 | ||
2065 | assert(nah->vendor == htonl(NX_VENDOR_ID)); | |
2066 | switch (subtype) { | |
2067 | case NXAST_RESUBMIT: | |
2068 | nar = (const struct nx_action_resubmit *) nah; | |
2069 | xlate_table_action(ctx, ofp_port_to_odp_port(ntohs(nar->in_port))); | |
2070 | break; | |
2071 | ||
2072 | default: | |
2073 | VLOG_DBG_RL(&rl, "unknown Nicira action type %"PRIu16, subtype); | |
2074 | break; | |
2075 | } | |
2076 | } | |
2077 | ||
2078 | static void | |
2079 | do_xlate_actions(const union ofp_action *in, size_t n_in, | |
2080 | struct action_xlate_ctx *ctx) | |
2081 | { | |
2082 | struct actions_iterator iter; | |
2083 | const union ofp_action *ia; | |
2084 | const struct ofport *port; | |
2085 | ||
2086 | port = port_array_get(&ctx->ofproto->ports, ctx->flow->in_port); | |
2087 | if (port && port->opp.config & (OFPPC_NO_RECV | OFPPC_NO_RECV_STP) && | |
2088 | port->opp.config & (eth_addr_equals(ctx->flow->dl_dst, stp_eth_addr) | |
2089 | ? OFPPC_NO_RECV_STP : OFPPC_NO_RECV)) { | |
2090 | /* Drop this flow. */ | |
2091 | return; | |
2092 | } | |
2093 | ||
2094 | for (ia = actions_first(&iter, in, n_in); ia; ia = actions_next(&iter)) { | |
2095 | uint16_t type = ntohs(ia->type); | |
2096 | union odp_action *oa; | |
2097 | ||
2098 | switch (type) { | |
2099 | case OFPAT_OUTPUT: | |
2100 | xlate_output_action(ctx, &ia->output); | |
2101 | break; | |
2102 | ||
2103 | case OFPAT_SET_VLAN_VID: | |
2104 | oa = odp_actions_add(ctx->out, ODPAT_SET_VLAN_VID); | |
2105 | oa->vlan_vid.vlan_vid = ia->vlan_vid.vlan_vid; | |
2106 | break; | |
2107 | ||
2108 | case OFPAT_SET_VLAN_PCP: | |
2109 | oa = odp_actions_add(ctx->out, ODPAT_SET_VLAN_PCP); | |
2110 | oa->vlan_pcp.vlan_pcp = ia->vlan_pcp.vlan_pcp; | |
2111 | break; | |
2112 | ||
2113 | case OFPAT_STRIP_VLAN: | |
2114 | odp_actions_add(ctx->out, ODPAT_STRIP_VLAN); | |
2115 | break; | |
2116 | ||
2117 | case OFPAT_SET_DL_SRC: | |
2118 | oa = odp_actions_add(ctx->out, ODPAT_SET_DL_SRC); | |
2119 | memcpy(oa->dl_addr.dl_addr, | |
2120 | ((struct ofp_action_dl_addr *) ia)->dl_addr, ETH_ADDR_LEN); | |
2121 | break; | |
2122 | ||
2123 | case OFPAT_SET_DL_DST: | |
2124 | oa = odp_actions_add(ctx->out, ODPAT_SET_DL_DST); | |
2125 | memcpy(oa->dl_addr.dl_addr, | |
2126 | ((struct ofp_action_dl_addr *) ia)->dl_addr, ETH_ADDR_LEN); | |
2127 | break; | |
2128 | ||
2129 | case OFPAT_SET_NW_SRC: | |
2130 | oa = odp_actions_add(ctx->out, ODPAT_SET_NW_SRC); | |
2131 | oa->nw_addr.nw_addr = ia->nw_addr.nw_addr; | |
2132 | break; | |
2133 | ||
2d70a31a JP |
2134 | case OFPAT_SET_NW_DST: |
2135 | oa = odp_actions_add(ctx->out, ODPAT_SET_NW_DST); | |
2136 | oa->nw_addr.nw_addr = ia->nw_addr.nw_addr; | |
2137 | break; | |
2138 | ||
064af421 BP |
2139 | case OFPAT_SET_TP_SRC: |
2140 | oa = odp_actions_add(ctx->out, ODPAT_SET_TP_SRC); | |
2141 | oa->tp_port.tp_port = ia->tp_port.tp_port; | |
2142 | break; | |
2143 | ||
2d70a31a JP |
2144 | case OFPAT_SET_TP_DST: |
2145 | oa = odp_actions_add(ctx->out, ODPAT_SET_TP_DST); | |
2146 | oa->tp_port.tp_port = ia->tp_port.tp_port; | |
2147 | break; | |
2148 | ||
064af421 BP |
2149 | case OFPAT_VENDOR: |
2150 | xlate_nicira_action(ctx, (const struct nx_action_header *) ia); | |
2151 | break; | |
2152 | ||
2153 | default: | |
2154 | VLOG_DBG_RL(&rl, "unknown action type %"PRIu16, type); | |
2155 | break; | |
2156 | } | |
2157 | } | |
2158 | } | |
2159 | ||
2160 | static int | |
2161 | xlate_actions(const union ofp_action *in, size_t n_in, | |
2162 | const flow_t *flow, struct ofproto *ofproto, | |
2163 | const struct ofpbuf *packet, | |
6a07af36 JG |
2164 | struct odp_actions *out, tag_type *tags, bool *may_set_up_flow, |
2165 | uint16_t *nf_output_iface) | |
064af421 BP |
2166 | { |
2167 | tag_type no_tags = 0; | |
2168 | struct action_xlate_ctx ctx; | |
2169 | COVERAGE_INC(ofproto_ofp2odp); | |
2170 | odp_actions_init(out); | |
2171 | ctx.flow = flow; | |
2172 | ctx.recurse = 0; | |
2173 | ctx.ofproto = ofproto; | |
2174 | ctx.packet = packet; | |
2175 | ctx.out = out; | |
2176 | ctx.tags = tags ? tags : &no_tags; | |
d6fbec6d | 2177 | ctx.may_set_up_flow = true; |
6a07af36 | 2178 | ctx.nf_output_iface = NF_OUT_DROP; |
064af421 | 2179 | do_xlate_actions(in, n_in, &ctx); |
0ad9b732 | 2180 | |
d6fbec6d | 2181 | /* Check with in-band control to see if we're allowed to set up this |
0ad9b732 JP |
2182 | * flow. */ |
2183 | if (!in_band_rule_check(ofproto->in_band, flow, out)) { | |
d6fbec6d | 2184 | ctx.may_set_up_flow = false; |
0ad9b732 JP |
2185 | } |
2186 | ||
d6fbec6d BP |
2187 | if (may_set_up_flow) { |
2188 | *may_set_up_flow = ctx.may_set_up_flow; | |
064af421 | 2189 | } |
6a07af36 JG |
2190 | if (nf_output_iface) { |
2191 | *nf_output_iface = ctx.nf_output_iface; | |
064af421 BP |
2192 | } |
2193 | if (odp_actions_overflow(out)) { | |
2194 | odp_actions_init(out); | |
2195 | return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_TOO_MANY); | |
2196 | } | |
2197 | return 0; | |
2198 | } | |
2199 | ||
2200 | static int | |
2201 | handle_packet_out(struct ofproto *p, struct ofconn *ofconn, | |
2202 | struct ofp_header *oh) | |
2203 | { | |
2204 | struct ofp_packet_out *opo; | |
2205 | struct ofpbuf payload, *buffer; | |
2206 | struct odp_actions actions; | |
2207 | int n_actions; | |
2208 | uint16_t in_port; | |
2209 | flow_t flow; | |
2210 | int error; | |
2211 | ||
2212 | error = check_ofp_packet_out(oh, &payload, &n_actions, p->max_ports); | |
2213 | if (error) { | |
2214 | return error; | |
2215 | } | |
2216 | opo = (struct ofp_packet_out *) oh; | |
2217 | ||
2218 | COVERAGE_INC(ofproto_packet_out); | |
2219 | if (opo->buffer_id != htonl(UINT32_MAX)) { | |
2220 | error = pktbuf_retrieve(ofconn->pktbuf, ntohl(opo->buffer_id), | |
2221 | &buffer, &in_port); | |
7778bd15 | 2222 | if (error || !buffer) { |
064af421 BP |
2223 | return error; |
2224 | } | |
2225 | payload = *buffer; | |
2226 | } else { | |
2227 | buffer = NULL; | |
2228 | } | |
2229 | ||
2230 | flow_extract(&payload, ofp_port_to_odp_port(ntohs(opo->in_port)), &flow); | |
2231 | error = xlate_actions((const union ofp_action *) opo->actions, n_actions, | |
6a07af36 | 2232 | &flow, p, &payload, &actions, NULL, NULL, NULL); |
064af421 BP |
2233 | if (error) { |
2234 | return error; | |
2235 | } | |
2236 | ||
c228a364 | 2237 | dpif_execute(p->dpif, flow.in_port, actions.actions, actions.n_actions, |
064af421 BP |
2238 | &payload); |
2239 | ofpbuf_delete(buffer); | |
2240 | ||
2241 | return 0; | |
2242 | } | |
2243 | ||
2244 | static void | |
2245 | update_port_config(struct ofproto *p, struct ofport *port, | |
2246 | uint32_t config, uint32_t mask) | |
2247 | { | |
2248 | mask &= config ^ port->opp.config; | |
2249 | if (mask & OFPPC_PORT_DOWN) { | |
2250 | if (config & OFPPC_PORT_DOWN) { | |
2251 | netdev_turn_flags_off(port->netdev, NETDEV_UP, true); | |
2252 | } else { | |
2253 | netdev_turn_flags_on(port->netdev, NETDEV_UP, true); | |
2254 | } | |
2255 | } | |
2256 | #define REVALIDATE_BITS (OFPPC_NO_RECV | OFPPC_NO_RECV_STP | OFPPC_NO_FWD) | |
2257 | if (mask & REVALIDATE_BITS) { | |
2258 | COVERAGE_INC(ofproto_costly_flags); | |
2259 | port->opp.config ^= mask & REVALIDATE_BITS; | |
2260 | p->need_revalidate = true; | |
2261 | } | |
2262 | #undef REVALIDATE_BITS | |
2263 | if (mask & OFPPC_NO_FLOOD) { | |
2264 | port->opp.config ^= OFPPC_NO_FLOOD; | |
2265 | refresh_port_group(p, DP_GROUP_FLOOD); | |
2266 | } | |
2267 | if (mask & OFPPC_NO_PACKET_IN) { | |
2268 | port->opp.config ^= OFPPC_NO_PACKET_IN; | |
2269 | } | |
2270 | } | |
2271 | ||
2272 | static int | |
2273 | handle_port_mod(struct ofproto *p, struct ofp_header *oh) | |
2274 | { | |
2275 | const struct ofp_port_mod *opm; | |
2276 | struct ofport *port; | |
2277 | int error; | |
2278 | ||
2279 | error = check_ofp_message(oh, OFPT_PORT_MOD, sizeof *opm); | |
2280 | if (error) { | |
2281 | return error; | |
2282 | } | |
2283 | opm = (struct ofp_port_mod *) oh; | |
2284 | ||
2285 | port = port_array_get(&p->ports, | |
2286 | ofp_port_to_odp_port(ntohs(opm->port_no))); | |
2287 | if (!port) { | |
2288 | return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_PORT); | |
2289 | } else if (memcmp(port->opp.hw_addr, opm->hw_addr, OFP_ETH_ALEN)) { | |
2290 | return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_HW_ADDR); | |
2291 | } else { | |
2292 | update_port_config(p, port, ntohl(opm->config), ntohl(opm->mask)); | |
2293 | if (opm->advertise) { | |
2294 | netdev_set_advertisements(port->netdev, ntohl(opm->advertise)); | |
2295 | } | |
2296 | } | |
2297 | return 0; | |
2298 | } | |
2299 | ||
2300 | static struct ofpbuf * | |
2301 | make_stats_reply(uint32_t xid, uint16_t type, size_t body_len) | |
2302 | { | |
2303 | struct ofp_stats_reply *osr; | |
2304 | struct ofpbuf *msg; | |
2305 | ||
2306 | msg = ofpbuf_new(MIN(sizeof *osr + body_len, UINT16_MAX)); | |
2307 | osr = put_openflow_xid(sizeof *osr, OFPT_STATS_REPLY, xid, msg); | |
2308 | osr->type = type; | |
2309 | osr->flags = htons(0); | |
2310 | return msg; | |
2311 | } | |
2312 | ||
2313 | static struct ofpbuf * | |
2314 | start_stats_reply(const struct ofp_stats_request *request, size_t body_len) | |
2315 | { | |
2316 | return make_stats_reply(request->header.xid, request->type, body_len); | |
2317 | } | |
2318 | ||
2319 | static void * | |
2320 | append_stats_reply(size_t nbytes, struct ofconn *ofconn, struct ofpbuf **msgp) | |
2321 | { | |
2322 | struct ofpbuf *msg = *msgp; | |
2323 | assert(nbytes <= UINT16_MAX - sizeof(struct ofp_stats_reply)); | |
2324 | if (nbytes + msg->size > UINT16_MAX) { | |
2325 | struct ofp_stats_reply *reply = msg->data; | |
2326 | reply->flags = htons(OFPSF_REPLY_MORE); | |
2327 | *msgp = make_stats_reply(reply->header.xid, reply->type, nbytes); | |
2328 | queue_tx(msg, ofconn, ofconn->reply_counter); | |
2329 | } | |
2330 | return ofpbuf_put_uninit(*msgp, nbytes); | |
2331 | } | |
2332 | ||
2333 | static int | |
2334 | handle_desc_stats_request(struct ofproto *p, struct ofconn *ofconn, | |
2335 | struct ofp_stats_request *request) | |
2336 | { | |
2337 | struct ofp_desc_stats *ods; | |
2338 | struct ofpbuf *msg; | |
2339 | ||
2340 | msg = start_stats_reply(request, sizeof *ods); | |
2341 | ods = append_stats_reply(sizeof *ods, ofconn, &msg); | |
2342 | strncpy(ods->mfr_desc, p->manufacturer, sizeof ods->mfr_desc); | |
2343 | strncpy(ods->hw_desc, p->hardware, sizeof ods->hw_desc); | |
2344 | strncpy(ods->sw_desc, p->software, sizeof ods->sw_desc); | |
2345 | strncpy(ods->serial_num, p->serial, sizeof ods->serial_num); | |
2346 | queue_tx(msg, ofconn, ofconn->reply_counter); | |
2347 | ||
2348 | return 0; | |
2349 | } | |
2350 | ||
2351 | static void | |
2352 | count_subrules(struct cls_rule *cls_rule, void *n_subrules_) | |
2353 | { | |
2354 | struct rule *rule = rule_from_cls_rule(cls_rule); | |
2355 | int *n_subrules = n_subrules_; | |
2356 | ||
2357 | if (rule->super) { | |
2358 | (*n_subrules)++; | |
2359 | } | |
2360 | } | |
2361 | ||
2362 | static int | |
2363 | handle_table_stats_request(struct ofproto *p, struct ofconn *ofconn, | |
2364 | struct ofp_stats_request *request) | |
2365 | { | |
2366 | struct ofp_table_stats *ots; | |
2367 | struct ofpbuf *msg; | |
2368 | struct odp_stats dpstats; | |
2369 | int n_exact, n_subrules, n_wild; | |
2370 | ||
2371 | msg = start_stats_reply(request, sizeof *ots * 2); | |
2372 | ||
2373 | /* Count rules of various kinds. */ | |
2374 | n_subrules = 0; | |
2375 | classifier_for_each(&p->cls, CLS_INC_EXACT, count_subrules, &n_subrules); | |
2376 | n_exact = classifier_count_exact(&p->cls) - n_subrules; | |
2377 | n_wild = classifier_count(&p->cls) - classifier_count_exact(&p->cls); | |
2378 | ||
2379 | /* Hash table. */ | |
c228a364 | 2380 | dpif_get_dp_stats(p->dpif, &dpstats); |
064af421 BP |
2381 | ots = append_stats_reply(sizeof *ots, ofconn, &msg); |
2382 | memset(ots, 0, sizeof *ots); | |
2383 | ots->table_id = TABLEID_HASH; | |
2384 | strcpy(ots->name, "hash"); | |
2385 | ots->wildcards = htonl(0); | |
2386 | ots->max_entries = htonl(dpstats.max_capacity); | |
2387 | ots->active_count = htonl(n_exact); | |
2388 | ots->lookup_count = htonll(dpstats.n_frags + dpstats.n_hit + | |
2389 | dpstats.n_missed); | |
2390 | ots->matched_count = htonll(dpstats.n_hit); /* XXX */ | |
2391 | ||
2392 | /* Classifier table. */ | |
2393 | ots = append_stats_reply(sizeof *ots, ofconn, &msg); | |
2394 | memset(ots, 0, sizeof *ots); | |
2395 | ots->table_id = TABLEID_CLASSIFIER; | |
2396 | strcpy(ots->name, "classifier"); | |
2397 | ots->wildcards = htonl(OFPFW_ALL); | |
2398 | ots->max_entries = htonl(65536); | |
2399 | ots->active_count = htonl(n_wild); | |
2400 | ots->lookup_count = htonll(0); /* XXX */ | |
2401 | ots->matched_count = htonll(0); /* XXX */ | |
2402 | ||
2403 | queue_tx(msg, ofconn, ofconn->reply_counter); | |
2404 | return 0; | |
2405 | } | |
2406 | ||
2407 | static int | |
2408 | handle_port_stats_request(struct ofproto *p, struct ofconn *ofconn, | |
2409 | struct ofp_stats_request *request) | |
2410 | { | |
2411 | struct ofp_port_stats *ops; | |
2412 | struct ofpbuf *msg; | |
2413 | struct ofport *port; | |
2414 | unsigned int port_no; | |
2415 | ||
2416 | msg = start_stats_reply(request, sizeof *ops * 16); | |
2417 | PORT_ARRAY_FOR_EACH (port, &p->ports, port_no) { | |
2418 | struct netdev_stats stats; | |
2419 | ||
2420 | /* Intentionally ignore return value, since errors will set 'stats' to | |
2421 | * all-1s, which is correct for OpenFlow, and netdev_get_stats() will | |
2422 | * log errors. */ | |
2423 | netdev_get_stats(port->netdev, &stats); | |
2424 | ||
2425 | ops = append_stats_reply(sizeof *ops, ofconn, &msg); | |
2426 | ops->port_no = htons(odp_port_to_ofp_port(port_no)); | |
2427 | memset(ops->pad, 0, sizeof ops->pad); | |
2428 | ops->rx_packets = htonll(stats.rx_packets); | |
2429 | ops->tx_packets = htonll(stats.tx_packets); | |
2430 | ops->rx_bytes = htonll(stats.rx_bytes); | |
2431 | ops->tx_bytes = htonll(stats.tx_bytes); | |
2432 | ops->rx_dropped = htonll(stats.rx_dropped); | |
2433 | ops->tx_dropped = htonll(stats.tx_dropped); | |
2434 | ops->rx_errors = htonll(stats.rx_errors); | |
2435 | ops->tx_errors = htonll(stats.tx_errors); | |
2436 | ops->rx_frame_err = htonll(stats.rx_frame_errors); | |
2437 | ops->rx_over_err = htonll(stats.rx_over_errors); | |
2438 | ops->rx_crc_err = htonll(stats.rx_crc_errors); | |
2439 | ops->collisions = htonll(stats.collisions); | |
2440 | } | |
2441 | ||
2442 | queue_tx(msg, ofconn, ofconn->reply_counter); | |
2443 | return 0; | |
2444 | } | |
2445 | ||
2446 | struct flow_stats_cbdata { | |
2447 | struct ofproto *ofproto; | |
2448 | struct ofconn *ofconn; | |
2449 | uint16_t out_port; | |
2450 | struct ofpbuf *msg; | |
2451 | }; | |
2452 | ||
2453 | static void | |
2454 | query_stats(struct ofproto *p, struct rule *rule, | |
2455 | uint64_t *packet_countp, uint64_t *byte_countp) | |
2456 | { | |
2457 | uint64_t packet_count, byte_count; | |
2458 | struct rule *subrule; | |
2459 | struct odp_flow *odp_flows; | |
2460 | size_t n_odp_flows; | |
2461 | ||
b3137fe8 JG |
2462 | packet_count = rule->packet_count; |
2463 | byte_count = rule->byte_count; | |
2464 | ||
064af421 | 2465 | n_odp_flows = rule->cr.wc.wildcards ? list_size(&rule->list) : 1; |
ec6fde61 | 2466 | odp_flows = xzalloc(n_odp_flows * sizeof *odp_flows); |
064af421 BP |
2467 | if (rule->cr.wc.wildcards) { |
2468 | size_t i = 0; | |
2469 | LIST_FOR_EACH (subrule, struct rule, list, &rule->list) { | |
2470 | odp_flows[i++].key = subrule->cr.flow; | |
b3137fe8 JG |
2471 | packet_count += subrule->packet_count; |
2472 | byte_count += subrule->byte_count; | |
064af421 BP |
2473 | } |
2474 | } else { | |
2475 | odp_flows[0].key = rule->cr.flow; | |
2476 | } | |
2477 | ||
2478 | packet_count = rule->packet_count; | |
2479 | byte_count = rule->byte_count; | |
c228a364 | 2480 | if (!dpif_flow_get_multiple(p->dpif, odp_flows, n_odp_flows)) { |
064af421 BP |
2481 | size_t i; |
2482 | for (i = 0; i < n_odp_flows; i++) { | |
2483 | struct odp_flow *odp_flow = &odp_flows[i]; | |
2484 | packet_count += odp_flow->stats.n_packets; | |
2485 | byte_count += odp_flow->stats.n_bytes; | |
2486 | } | |
2487 | } | |
2488 | free(odp_flows); | |
2489 | ||
2490 | *packet_countp = packet_count; | |
2491 | *byte_countp = byte_count; | |
2492 | } | |
2493 | ||
2494 | static void | |
2495 | flow_stats_cb(struct cls_rule *rule_, void *cbdata_) | |
2496 | { | |
2497 | struct rule *rule = rule_from_cls_rule(rule_); | |
2498 | struct flow_stats_cbdata *cbdata = cbdata_; | |
2499 | struct ofp_flow_stats *ofs; | |
2500 | uint64_t packet_count, byte_count; | |
2501 | size_t act_len, len; | |
2502 | ||
2503 | if (rule_is_hidden(rule) || !rule_has_out_port(rule, cbdata->out_port)) { | |
2504 | return; | |
2505 | } | |
2506 | ||
2507 | act_len = sizeof *rule->actions * rule->n_actions; | |
2508 | len = offsetof(struct ofp_flow_stats, actions) + act_len; | |
2509 | ||
2510 | query_stats(cbdata->ofproto, rule, &packet_count, &byte_count); | |
2511 | ||
2512 | ofs = append_stats_reply(len, cbdata->ofconn, &cbdata->msg); | |
2513 | ofs->length = htons(len); | |
2514 | ofs->table_id = rule->cr.wc.wildcards ? TABLEID_CLASSIFIER : TABLEID_HASH; | |
2515 | ofs->pad = 0; | |
2516 | flow_to_match(&rule->cr.flow, rule->cr.wc.wildcards, &ofs->match); | |
2517 | ofs->duration = htonl((time_msec() - rule->created) / 1000); | |
2518 | ofs->priority = htons(rule->cr.priority); | |
2519 | ofs->idle_timeout = htons(rule->idle_timeout); | |
2520 | ofs->hard_timeout = htons(rule->hard_timeout); | |
2521 | memset(ofs->pad2, 0, sizeof ofs->pad2); | |
2522 | ofs->packet_count = htonll(packet_count); | |
2523 | ofs->byte_count = htonll(byte_count); | |
2524 | memcpy(ofs->actions, rule->actions, act_len); | |
2525 | } | |
2526 | ||
2527 | static int | |
2528 | table_id_to_include(uint8_t table_id) | |
2529 | { | |
2530 | return (table_id == TABLEID_HASH ? CLS_INC_EXACT | |
2531 | : table_id == TABLEID_CLASSIFIER ? CLS_INC_WILD | |
2532 | : table_id == 0xff ? CLS_INC_ALL | |
2533 | : 0); | |
2534 | } | |
2535 | ||
2536 | static int | |
2537 | handle_flow_stats_request(struct ofproto *p, struct ofconn *ofconn, | |
2538 | const struct ofp_stats_request *osr, | |
2539 | size_t arg_size) | |
2540 | { | |
2541 | struct ofp_flow_stats_request *fsr; | |
2542 | struct flow_stats_cbdata cbdata; | |
2543 | struct cls_rule target; | |
2544 | ||
2545 | if (arg_size != sizeof *fsr) { | |
2546 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LENGTH); | |
2547 | } | |
2548 | fsr = (struct ofp_flow_stats_request *) osr->body; | |
2549 | ||
2550 | COVERAGE_INC(ofproto_flows_req); | |
2551 | cbdata.ofproto = p; | |
2552 | cbdata.ofconn = ofconn; | |
2553 | cbdata.out_port = fsr->out_port; | |
2554 | cbdata.msg = start_stats_reply(osr, 1024); | |
2555 | cls_rule_from_match(&target, &fsr->match, 0); | |
2556 | classifier_for_each_match(&p->cls, &target, | |
2557 | table_id_to_include(fsr->table_id), | |
2558 | flow_stats_cb, &cbdata); | |
2559 | queue_tx(cbdata.msg, ofconn, ofconn->reply_counter); | |
2560 | return 0; | |
2561 | } | |
2562 | ||
4f2cad2c JP |
2563 | struct flow_stats_ds_cbdata { |
2564 | struct ofproto *ofproto; | |
2565 | struct ds *results; | |
2566 | }; | |
2567 | ||
2568 | static void | |
2569 | flow_stats_ds_cb(struct cls_rule *rule_, void *cbdata_) | |
2570 | { | |
2571 | struct rule *rule = rule_from_cls_rule(rule_); | |
2572 | struct flow_stats_ds_cbdata *cbdata = cbdata_; | |
2573 | struct ds *results = cbdata->results; | |
2574 | struct ofp_match match; | |
2575 | uint64_t packet_count, byte_count; | |
2576 | size_t act_len = sizeof *rule->actions * rule->n_actions; | |
2577 | ||
2578 | /* Don't report on subrules. */ | |
2579 | if (rule->super != NULL) { | |
2580 | return; | |
2581 | } | |
2582 | ||
2583 | query_stats(cbdata->ofproto, rule, &packet_count, &byte_count); | |
a26ef517 | 2584 | flow_to_ovs_match(&rule->cr.flow, rule->cr.wc.wildcards, &match); |
4f2cad2c JP |
2585 | |
2586 | ds_put_format(results, "duration=%llds, ", | |
2587 | (time_msec() - rule->created) / 1000); | |
52ae00b3 | 2588 | ds_put_format(results, "priority=%u, ", rule->cr.priority); |
4f2cad2c JP |
2589 | ds_put_format(results, "n_packets=%"PRIu64", ", packet_count); |
2590 | ds_put_format(results, "n_bytes=%"PRIu64", ", byte_count); | |
2591 | ofp_print_match(results, &match, true); | |
2592 | ofp_print_actions(results, &rule->actions->header, act_len); | |
2593 | ds_put_cstr(results, "\n"); | |
2594 | } | |
2595 | ||
2596 | /* Adds a pretty-printed description of all flows to 'results', including | |
2597 | * those marked hidden by secchan (e.g., by in-band control). */ | |
2598 | void | |
2599 | ofproto_get_all_flows(struct ofproto *p, struct ds *results) | |
2600 | { | |
2601 | struct ofp_match match; | |
2602 | struct cls_rule target; | |
2603 | struct flow_stats_ds_cbdata cbdata; | |
2604 | ||
2605 | memset(&match, 0, sizeof match); | |
2606 | match.wildcards = htonl(OFPFW_ALL); | |
2607 | ||
2608 | cbdata.ofproto = p; | |
2609 | cbdata.results = results; | |
2610 | ||
2611 | cls_rule_from_match(&target, &match, 0); | |
2612 | classifier_for_each_match(&p->cls, &target, CLS_INC_ALL, | |
2613 | flow_stats_ds_cb, &cbdata); | |
2614 | } | |
2615 | ||
064af421 BP |
2616 | struct aggregate_stats_cbdata { |
2617 | struct ofproto *ofproto; | |
2618 | uint16_t out_port; | |
2619 | uint64_t packet_count; | |
2620 | uint64_t byte_count; | |
2621 | uint32_t n_flows; | |
2622 | }; | |
2623 | ||
2624 | static void | |
2625 | aggregate_stats_cb(struct cls_rule *rule_, void *cbdata_) | |
2626 | { | |
2627 | struct rule *rule = rule_from_cls_rule(rule_); | |
2628 | struct aggregate_stats_cbdata *cbdata = cbdata_; | |
2629 | uint64_t packet_count, byte_count; | |
2630 | ||
2631 | if (rule_is_hidden(rule) || !rule_has_out_port(rule, cbdata->out_port)) { | |
2632 | return; | |
2633 | } | |
2634 | ||
2635 | query_stats(cbdata->ofproto, rule, &packet_count, &byte_count); | |
2636 | ||
2637 | cbdata->packet_count += packet_count; | |
2638 | cbdata->byte_count += byte_count; | |
2639 | cbdata->n_flows++; | |
2640 | } | |
2641 | ||
2642 | static int | |
2643 | handle_aggregate_stats_request(struct ofproto *p, struct ofconn *ofconn, | |
2644 | const struct ofp_stats_request *osr, | |
2645 | size_t arg_size) | |
2646 | { | |
2647 | struct ofp_aggregate_stats_request *asr; | |
2648 | struct ofp_aggregate_stats_reply *reply; | |
2649 | struct aggregate_stats_cbdata cbdata; | |
2650 | struct cls_rule target; | |
2651 | struct ofpbuf *msg; | |
2652 | ||
2653 | if (arg_size != sizeof *asr) { | |
2654 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LENGTH); | |
2655 | } | |
2656 | asr = (struct ofp_aggregate_stats_request *) osr->body; | |
2657 | ||
2658 | COVERAGE_INC(ofproto_agg_request); | |
2659 | cbdata.ofproto = p; | |
2660 | cbdata.out_port = asr->out_port; | |
2661 | cbdata.packet_count = 0; | |
2662 | cbdata.byte_count = 0; | |
2663 | cbdata.n_flows = 0; | |
2664 | cls_rule_from_match(&target, &asr->match, 0); | |
2665 | classifier_for_each_match(&p->cls, &target, | |
2666 | table_id_to_include(asr->table_id), | |
2667 | aggregate_stats_cb, &cbdata); | |
2668 | ||
2669 | msg = start_stats_reply(osr, sizeof *reply); | |
2670 | reply = append_stats_reply(sizeof *reply, ofconn, &msg); | |
2671 | reply->flow_count = htonl(cbdata.n_flows); | |
2672 | reply->packet_count = htonll(cbdata.packet_count); | |
2673 | reply->byte_count = htonll(cbdata.byte_count); | |
2674 | queue_tx(msg, ofconn, ofconn->reply_counter); | |
2675 | return 0; | |
2676 | } | |
2677 | ||
2678 | static int | |
2679 | handle_stats_request(struct ofproto *p, struct ofconn *ofconn, | |
2680 | struct ofp_header *oh) | |
2681 | { | |
2682 | struct ofp_stats_request *osr; | |
2683 | size_t arg_size; | |
2684 | int error; | |
2685 | ||
2686 | error = check_ofp_message_array(oh, OFPT_STATS_REQUEST, sizeof *osr, | |
2687 | 1, &arg_size); | |
2688 | if (error) { | |
2689 | return error; | |
2690 | } | |
2691 | osr = (struct ofp_stats_request *) oh; | |
2692 | ||
2693 | switch (ntohs(osr->type)) { | |
2694 | case OFPST_DESC: | |
2695 | return handle_desc_stats_request(p, ofconn, osr); | |
2696 | ||
2697 | case OFPST_FLOW: | |
2698 | return handle_flow_stats_request(p, ofconn, osr, arg_size); | |
2699 | ||
2700 | case OFPST_AGGREGATE: | |
2701 | return handle_aggregate_stats_request(p, ofconn, osr, arg_size); | |
2702 | ||
2703 | case OFPST_TABLE: | |
2704 | return handle_table_stats_request(p, ofconn, osr); | |
2705 | ||
2706 | case OFPST_PORT: | |
2707 | return handle_port_stats_request(p, ofconn, osr); | |
2708 | ||
2709 | case OFPST_VENDOR: | |
2710 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR); | |
2711 | ||
2712 | default: | |
2713 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT); | |
2714 | } | |
2715 | } | |
2716 | ||
2717 | static long long int | |
2718 | msec_from_nsec(uint64_t sec, uint32_t nsec) | |
2719 | { | |
2720 | return !sec ? 0 : sec * 1000 + nsec / 1000000; | |
2721 | } | |
2722 | ||
2723 | static void | |
0193b2af JG |
2724 | update_time(struct ofproto *ofproto, struct rule *rule, |
2725 | const struct odp_flow_stats *stats) | |
064af421 BP |
2726 | { |
2727 | long long int used = msec_from_nsec(stats->used_sec, stats->used_nsec); | |
2728 | if (used > rule->used) { | |
2729 | rule->used = used; | |
4836f9f2 JP |
2730 | if (rule->super && used > rule->super->used) { |
2731 | rule->super->used = used; | |
2732 | } | |
0193b2af | 2733 | netflow_flow_update_time(ofproto->netflow, &rule->nf_flow, used); |
064af421 BP |
2734 | } |
2735 | } | |
2736 | ||
2737 | static void | |
0193b2af JG |
2738 | update_stats(struct ofproto *ofproto, struct rule *rule, |
2739 | const struct odp_flow_stats *stats) | |
064af421 | 2740 | { |
064af421 | 2741 | if (stats->n_packets) { |
0193b2af JG |
2742 | update_time(ofproto, rule, stats); |
2743 | rule->packet_count += stats->n_packets; | |
2744 | rule->byte_count += stats->n_bytes; | |
2745 | netflow_flow_update_flags(&rule->nf_flow, stats->ip_tos, | |
2746 | stats->tcp_flags); | |
064af421 BP |
2747 | } |
2748 | } | |
2749 | ||
2750 | static int | |
2751 | add_flow(struct ofproto *p, struct ofconn *ofconn, | |
2752 | struct ofp_flow_mod *ofm, size_t n_actions) | |
2753 | { | |
2754 | struct ofpbuf *packet; | |
2755 | struct rule *rule; | |
2756 | uint16_t in_port; | |
2757 | int error; | |
2758 | ||
0193b2af | 2759 | rule = rule_create(p, NULL, (const union ofp_action *) ofm->actions, |
064af421 BP |
2760 | n_actions, ntohs(ofm->idle_timeout), |
2761 | ntohs(ofm->hard_timeout)); | |
2762 | cls_rule_from_match(&rule->cr, &ofm->match, ntohs(ofm->priority)); | |
2763 | ||
2764 | packet = NULL; | |
2765 | error = 0; | |
2766 | if (ofm->buffer_id != htonl(UINT32_MAX)) { | |
2767 | error = pktbuf_retrieve(ofconn->pktbuf, ntohl(ofm->buffer_id), | |
2768 | &packet, &in_port); | |
2769 | } | |
2770 | ||
2771 | rule_insert(p, rule, packet, in_port); | |
2772 | ofpbuf_delete(packet); | |
2773 | return error; | |
2774 | } | |
2775 | ||
2776 | static int | |
2777 | modify_flow(struct ofproto *p, const struct ofp_flow_mod *ofm, | |
2778 | size_t n_actions, uint16_t command, struct rule *rule) | |
2779 | { | |
2780 | if (rule_is_hidden(rule)) { | |
2781 | return 0; | |
2782 | } | |
2783 | ||
2784 | if (command == OFPFC_DELETE) { | |
2785 | rule_remove(p, rule); | |
2786 | } else { | |
2787 | size_t actions_len = n_actions * sizeof *rule->actions; | |
2788 | ||
2789 | if (n_actions == rule->n_actions | |
2790 | && !memcmp(ofm->actions, rule->actions, actions_len)) | |
2791 | { | |
2792 | return 0; | |
2793 | } | |
2794 | ||
2795 | free(rule->actions); | |
2796 | rule->actions = xmemdup(ofm->actions, actions_len); | |
2797 | rule->n_actions = n_actions; | |
2798 | ||
2799 | if (rule->cr.wc.wildcards) { | |
2800 | COVERAGE_INC(ofproto_mod_wc_flow); | |
2801 | p->need_revalidate = true; | |
2802 | } else { | |
2803 | rule_update_actions(p, rule); | |
2804 | } | |
2805 | } | |
2806 | ||
2807 | return 0; | |
2808 | } | |
2809 | ||
2810 | static int | |
2811 | modify_flows_strict(struct ofproto *p, const struct ofp_flow_mod *ofm, | |
2812 | size_t n_actions, uint16_t command) | |
2813 | { | |
2814 | struct rule *rule; | |
2815 | uint32_t wildcards; | |
2816 | flow_t flow; | |
2817 | ||
2818 | flow_from_match(&flow, &wildcards, &ofm->match); | |
2819 | rule = rule_from_cls_rule(classifier_find_rule_exactly( | |
2820 | &p->cls, &flow, wildcards, | |
2821 | ntohs(ofm->priority))); | |
2822 | ||
2823 | if (rule) { | |
2824 | if (command == OFPFC_DELETE | |
2825 | && ofm->out_port != htons(OFPP_NONE) | |
2826 | && !rule_has_out_port(rule, ofm->out_port)) { | |
2827 | return 0; | |
2828 | } | |
2829 | ||
2830 | modify_flow(p, ofm, n_actions, command, rule); | |
2831 | } | |
2832 | return 0; | |
2833 | } | |
2834 | ||
2835 | struct modify_flows_cbdata { | |
2836 | struct ofproto *ofproto; | |
2837 | const struct ofp_flow_mod *ofm; | |
2838 | uint16_t out_port; | |
2839 | size_t n_actions; | |
2840 | uint16_t command; | |
2841 | }; | |
2842 | ||
2843 | static void | |
2844 | modify_flows_cb(struct cls_rule *rule_, void *cbdata_) | |
2845 | { | |
2846 | struct rule *rule = rule_from_cls_rule(rule_); | |
2847 | struct modify_flows_cbdata *cbdata = cbdata_; | |
2848 | ||
2849 | if (cbdata->out_port != htons(OFPP_NONE) | |
2850 | && !rule_has_out_port(rule, cbdata->out_port)) { | |
2851 | return; | |
2852 | } | |
2853 | ||
2854 | modify_flow(cbdata->ofproto, cbdata->ofm, cbdata->n_actions, | |
2855 | cbdata->command, rule); | |
2856 | } | |
2857 | ||
2858 | static int | |
2859 | modify_flows_loose(struct ofproto *p, const struct ofp_flow_mod *ofm, | |
2860 | size_t n_actions, uint16_t command) | |
2861 | { | |
2862 | struct modify_flows_cbdata cbdata; | |
2863 | struct cls_rule target; | |
2864 | ||
2865 | cbdata.ofproto = p; | |
2866 | cbdata.ofm = ofm; | |
2867 | cbdata.out_port = (command == OFPFC_DELETE ? ofm->out_port | |
2868 | : htons(OFPP_NONE)); | |
2869 | cbdata.n_actions = n_actions; | |
2870 | cbdata.command = command; | |
2871 | ||
2872 | cls_rule_from_match(&target, &ofm->match, 0); | |
2873 | ||
2874 | classifier_for_each_match(&p->cls, &target, CLS_INC_ALL, | |
2875 | modify_flows_cb, &cbdata); | |
2876 | return 0; | |
2877 | } | |
2878 | ||
2879 | static int | |
2880 | handle_flow_mod(struct ofproto *p, struct ofconn *ofconn, | |
2881 | struct ofp_flow_mod *ofm) | |
2882 | { | |
2883 | size_t n_actions; | |
2884 | int error; | |
2885 | ||
2886 | error = check_ofp_message_array(&ofm->header, OFPT_FLOW_MOD, sizeof *ofm, | |
2887 | sizeof *ofm->actions, &n_actions); | |
2888 | if (error) { | |
2889 | return error; | |
2890 | } | |
2891 | ||
2892 | normalize_match(&ofm->match); | |
2893 | if (!ofm->match.wildcards) { | |
2894 | ofm->priority = htons(UINT16_MAX); | |
2895 | } | |
2896 | ||
2897 | error = validate_actions((const union ofp_action *) ofm->actions, | |
2898 | n_actions, p->max_ports); | |
2899 | if (error) { | |
2900 | return error; | |
2901 | } | |
2902 | ||
2903 | switch (ntohs(ofm->command)) { | |
2904 | case OFPFC_ADD: | |
2905 | return add_flow(p, ofconn, ofm, n_actions); | |
2906 | ||
2907 | case OFPFC_MODIFY: | |
2908 | return modify_flows_loose(p, ofm, n_actions, OFPFC_MODIFY); | |
2909 | ||
2910 | case OFPFC_MODIFY_STRICT: | |
2911 | return modify_flows_strict(p, ofm, n_actions, OFPFC_MODIFY); | |
2912 | ||
2913 | case OFPFC_DELETE: | |
2914 | return modify_flows_loose(p, ofm, n_actions, OFPFC_DELETE); | |
2915 | ||
2916 | case OFPFC_DELETE_STRICT: | |
2917 | return modify_flows_strict(p, ofm, n_actions, OFPFC_DELETE); | |
2918 | ||
2919 | default: | |
2920 | return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_COMMAND); | |
2921 | } | |
2922 | } | |
2923 | ||
2924 | static void | |
2925 | send_capability_reply(struct ofproto *p, struct ofconn *ofconn, uint32_t xid) | |
2926 | { | |
2927 | struct ofmp_capability_reply *ocr; | |
2928 | struct ofpbuf *b; | |
2929 | char capabilities[] = "com.nicira.mgmt.manager=false\n"; | |
2930 | ||
2931 | ocr = make_openflow_xid(sizeof(*ocr), OFPT_VENDOR, xid, &b); | |
2932 | ocr->header.header.vendor = htonl(NX_VENDOR_ID); | |
2933 | ocr->header.header.subtype = htonl(NXT_MGMT); | |
2934 | ocr->header.type = htons(OFMPT_CAPABILITY_REPLY); | |
2935 | ||
2936 | ocr->format = htonl(OFMPCOF_SIMPLE); | |
2937 | ocr->mgmt_id = htonll(p->mgmt_id); | |
2938 | ||
2939 | ofpbuf_put(b, capabilities, strlen(capabilities)); | |
2940 | ||
2941 | queue_tx(b, ofconn, ofconn->reply_counter); | |
2942 | } | |
2943 | ||
2944 | static int | |
2945 | handle_ofmp(struct ofproto *p, struct ofconn *ofconn, | |
2946 | struct ofmp_header *ofmph) | |
2947 | { | |
2948 | size_t msg_len = ntohs(ofmph->header.header.length); | |
2949 | if (msg_len < sizeof(*ofmph)) { | |
2886875a | 2950 | VLOG_WARN_RL(&rl, "dropping short managment message: %zu\n", msg_len); |
064af421 BP |
2951 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LENGTH); |
2952 | } | |
2953 | ||
2954 | if (ofmph->type == htons(OFMPT_CAPABILITY_REQUEST)) { | |
2955 | struct ofmp_capability_request *ofmpcr; | |
2956 | ||
2957 | if (msg_len < sizeof(struct ofmp_capability_request)) { | |
2886875a | 2958 | VLOG_WARN_RL(&rl, "dropping short capability request: %zu\n", |
064af421 BP |
2959 | msg_len); |
2960 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LENGTH); | |
2961 | } | |
2962 | ||
2963 | ofmpcr = (struct ofmp_capability_request *)ofmph; | |
2964 | if (ofmpcr->format != htonl(OFMPCAF_SIMPLE)) { | |
2965 | /* xxx Find a better type than bad subtype */ | |
2966 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE); | |
2967 | } | |
2968 | ||
2969 | send_capability_reply(p, ofconn, ofmph->header.header.xid); | |
2970 | return 0; | |
2971 | } else { | |
2972 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE); | |
2973 | } | |
2974 | } | |
2975 | ||
2976 | static int | |
2977 | handle_vendor(struct ofproto *p, struct ofconn *ofconn, void *msg) | |
2978 | { | |
2979 | struct ofp_vendor_header *ovh = msg; | |
2980 | struct nicira_header *nh; | |
2981 | ||
2982 | if (ntohs(ovh->header.length) < sizeof(struct ofp_vendor_header)) { | |
2983 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LENGTH); | |
2984 | } | |
2985 | if (ovh->vendor != htonl(NX_VENDOR_ID)) { | |
2986 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR); | |
2987 | } | |
2988 | if (ntohs(ovh->header.length) < sizeof(struct nicira_header)) { | |
2989 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LENGTH); | |
2990 | } | |
2991 | ||
2992 | nh = msg; | |
2993 | switch (ntohl(nh->subtype)) { | |
2994 | case NXT_STATUS_REQUEST: | |
2995 | return switch_status_handle_request(p->switch_status, ofconn->rconn, | |
2996 | msg); | |
2997 | ||
2998 | case NXT_ACT_SET_CONFIG: | |
2999 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE); /* XXX */ | |
3000 | ||
3001 | case NXT_ACT_GET_CONFIG: | |
3002 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE); /* XXX */ | |
3003 | ||
064af421 BP |
3004 | case NXT_MGMT: |
3005 | return handle_ofmp(p, ofconn, msg); | |
3006 | } | |
3007 | ||
3008 | return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE); | |
3009 | } | |
3010 | ||
3011 | static void | |
3012 | handle_openflow(struct ofconn *ofconn, struct ofproto *p, | |
3013 | struct ofpbuf *ofp_msg) | |
3014 | { | |
3015 | struct ofp_header *oh = ofp_msg->data; | |
3016 | int error; | |
3017 | ||
3018 | COVERAGE_INC(ofproto_recv_openflow); | |
3019 | switch (oh->type) { | |
3020 | case OFPT_ECHO_REQUEST: | |
3021 | error = handle_echo_request(ofconn, oh); | |
3022 | break; | |
3023 | ||
3024 | case OFPT_ECHO_REPLY: | |
3025 | error = 0; | |
3026 | break; | |
3027 | ||
3028 | case OFPT_FEATURES_REQUEST: | |
3029 | error = handle_features_request(p, ofconn, oh); | |
3030 | break; | |
3031 | ||
3032 | case OFPT_GET_CONFIG_REQUEST: | |
3033 | error = handle_get_config_request(p, ofconn, oh); | |
3034 | break; | |
3035 | ||
3036 | case OFPT_SET_CONFIG: | |
3037 | error = handle_set_config(p, ofconn, ofp_msg->data); | |
3038 | break; | |
3039 | ||
3040 | case OFPT_PACKET_OUT: | |
3041 | error = handle_packet_out(p, ofconn, ofp_msg->data); | |
3042 | break; | |
3043 | ||
3044 | case OFPT_PORT_MOD: | |
3045 | error = handle_port_mod(p, oh); | |
3046 | break; | |
3047 | ||
3048 | case OFPT_FLOW_MOD: | |
3049 | error = handle_flow_mod(p, ofconn, ofp_msg->data); | |
3050 | break; | |
3051 | ||
3052 | case OFPT_STATS_REQUEST: | |
3053 | error = handle_stats_request(p, ofconn, oh); | |
3054 | break; | |
3055 | ||
3056 | case OFPT_VENDOR: | |
3057 | error = handle_vendor(p, ofconn, ofp_msg->data); | |
3058 | break; | |
3059 | ||
3060 | default: | |
3061 | if (VLOG_IS_WARN_ENABLED()) { | |
3062 | char *s = ofp_to_string(oh, ntohs(oh->length), 2); | |
3063 | VLOG_DBG_RL(&rl, "OpenFlow message ignored: %s", s); | |
3064 | free(s); | |
3065 | } | |
3066 | error = ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE); | |
3067 | break; | |
3068 | } | |
3069 | ||
3070 | if (error) { | |
3071 | send_error_oh(ofconn, ofp_msg->data, error); | |
3072 | } | |
3073 | } | |
3074 | \f | |
3075 | static void | |
3076 | handle_odp_msg(struct ofproto *p, struct ofpbuf *packet) | |
3077 | { | |
3078 | struct odp_msg *msg = packet->data; | |
3079 | uint16_t in_port = odp_port_to_ofp_port(msg->port); | |
3080 | struct rule *rule; | |
3081 | struct ofpbuf payload; | |
3082 | flow_t flow; | |
3083 | ||
3084 | /* Handle controller actions. */ | |
3085 | if (msg->type == _ODPL_ACTION_NR) { | |
3086 | COVERAGE_INC(ofproto_ctlr_action); | |
3087 | pinsched_send(p->action_sched, in_port, packet, | |
3088 | send_packet_in_action, p); | |
3089 | return; | |
3090 | } | |
3091 | ||
3092 | payload.data = msg + 1; | |
3093 | payload.size = msg->length - sizeof *msg; | |
3094 | flow_extract(&payload, msg->port, &flow); | |
3095 | ||
0ad9b732 JP |
3096 | /* Check with in-band control to see if this packet should be sent |
3097 | * to the local port regardless of the flow table. */ | |
3098 | if (in_band_msg_in_hook(p->in_band, &flow, &payload)) { | |
3099 | union odp_action action; | |
3100 | ||
3101 | memset(&action, 0, sizeof(action)); | |
3102 | action.output.type = ODPAT_OUTPUT; | |
3103 | action.output.port = ODPP_LOCAL; | |
f1acd62b | 3104 | dpif_execute(p->dpif, flow.in_port, &action, 1, &payload); |
0ad9b732 JP |
3105 | } |
3106 | ||
064af421 BP |
3107 | rule = lookup_valid_rule(p, &flow); |
3108 | if (!rule) { | |
3109 | /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */ | |
3110 | struct ofport *port = port_array_get(&p->ports, msg->port); | |
3111 | if (port) { | |
3112 | if (port->opp.config & OFPPC_NO_PACKET_IN) { | |
3113 | COVERAGE_INC(ofproto_no_packet_in); | |
3114 | /* XXX install 'drop' flow entry */ | |
3115 | ofpbuf_delete(packet); | |
3116 | return; | |
3117 | } | |
3118 | } else { | |
3119 | VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16, msg->port); | |
3120 | } | |
3121 | ||
3122 | COVERAGE_INC(ofproto_packet_in); | |
3123 | pinsched_send(p->miss_sched, in_port, packet, send_packet_in_miss, p); | |
3124 | return; | |
3125 | } | |
3126 | ||
3127 | if (rule->cr.wc.wildcards) { | |
3128 | rule = rule_create_subrule(p, rule, &flow); | |
3129 | rule_make_actions(p, rule, packet); | |
3130 | } else { | |
3131 | if (!rule->may_install) { | |
3132 | /* The rule is not installable, that is, we need to process every | |
3133 | * packet, so process the current packet and set its actions into | |
3134 | * 'subrule'. */ | |
3135 | rule_make_actions(p, rule, packet); | |
3136 | } else { | |
3137 | /* XXX revalidate rule if it needs it */ | |
3138 | } | |
3139 | } | |
3140 | ||
3141 | rule_execute(p, rule, &payload, &flow); | |
3142 | rule_reinstall(p, rule); | |
7778bd15 BP |
3143 | |
3144 | if (rule->super && rule->super->cr.priority == FAIL_OPEN_PRIORITY | |
3145 | && rconn_is_connected(p->controller->rconn)) { | |
3146 | /* | |
3147 | * Extra-special case for fail-open mode. | |
3148 | * | |
3149 | * We are in fail-open mode and the packet matched the fail-open rule, | |
3150 | * but we are connected to a controller too. We should send the packet | |
3151 | * up to the controller in the hope that it will try to set up a flow | |
3152 | * and thereby allow us to exit fail-open. | |
3153 | * | |
3154 | * See the top-level comment in fail-open.c for more information. | |
3155 | */ | |
3156 | pinsched_send(p->miss_sched, in_port, packet, send_packet_in_miss, p); | |
3157 | } else { | |
3158 | ofpbuf_delete(packet); | |
3159 | } | |
064af421 BP |
3160 | } |
3161 | \f | |
3162 | static void | |
3163 | revalidate_cb(struct cls_rule *sub_, void *cbdata_) | |
3164 | { | |
3165 | struct rule *sub = rule_from_cls_rule(sub_); | |
3166 | struct revalidate_cbdata *cbdata = cbdata_; | |
3167 | ||
3168 | if (cbdata->revalidate_all | |
3169 | || (cbdata->revalidate_subrules && sub->super) | |
3170 | || (tag_set_intersects(&cbdata->revalidate_set, sub->tags))) { | |
3171 | revalidate_rule(cbdata->ofproto, sub); | |
3172 | } | |
3173 | } | |
3174 | ||
3175 | static bool | |
3176 | revalidate_rule(struct ofproto *p, struct rule *rule) | |
3177 | { | |
3178 | const flow_t *flow = &rule->cr.flow; | |
3179 | ||
3180 | COVERAGE_INC(ofproto_revalidate_rule); | |
3181 | if (rule->super) { | |
3182 | struct rule *super; | |
3183 | super = rule_from_cls_rule(classifier_lookup_wild(&p->cls, flow)); | |
3184 | if (!super) { | |
3185 | rule_remove(p, rule); | |
3186 | return false; | |
3187 | } else if (super != rule->super) { | |
3188 | COVERAGE_INC(ofproto_revalidate_moved); | |
3189 | list_remove(&rule->list); | |
3190 | list_push_back(&super->list, &rule->list); | |
3191 | rule->super = super; | |
3192 | rule->hard_timeout = super->hard_timeout; | |
3193 | rule->idle_timeout = super->idle_timeout; | |
3194 | rule->created = super->created; | |
3195 | rule->used = 0; | |
3196 | } | |
3197 | } | |
3198 | ||
3199 | rule_update_actions(p, rule); | |
3200 | return true; | |
3201 | } | |
3202 | ||
3203 | static struct ofpbuf * | |
3204 | compose_flow_exp(const struct rule *rule, long long int now, uint8_t reason) | |
3205 | { | |
3206 | struct ofp_flow_expired *ofe; | |
3207 | struct ofpbuf *buf; | |
3208 | ||
3209 | ofe = make_openflow(sizeof *ofe, OFPT_FLOW_EXPIRED, &buf); | |
3210 | flow_to_match(&rule->cr.flow, rule->cr.wc.wildcards, &ofe->match); | |
3211 | ofe->priority = htons(rule->cr.priority); | |
3212 | ofe->reason = reason; | |
3d3d15a0 JP |
3213 | ofe->duration = htonl((now - rule->created) / 1000); |
3214 | ofe->packet_count = htonll(rule->packet_count); | |
3215 | ofe->byte_count = htonll(rule->byte_count); | |
064af421 BP |
3216 | |
3217 | return buf; | |
3218 | } | |
3219 | ||
3220 | static void | |
3221 | send_flow_exp(struct ofproto *p, struct rule *rule, | |
3222 | long long int now, uint8_t reason) | |
3223 | { | |
3224 | struct ofconn *ofconn; | |
3225 | struct ofconn *prev; | |
b9b0ce61 | 3226 | struct ofpbuf *buf = NULL; |
064af421 BP |
3227 | |
3228 | /* We limit the maximum number of queued flow expirations it by accounting | |
3229 | * them under the counter for replies. That works because preventing | |
3230 | * OpenFlow requests from being processed also prevents new flows from | |
3231 | * being added (and expiring). (It also prevents processing OpenFlow | |
3232 | * requests that would not add new flows, so it is imperfect.) */ | |
3233 | ||
3234 | prev = NULL; | |
3235 | LIST_FOR_EACH (ofconn, struct ofconn, node, &p->all_conns) { | |
3236 | if (ofconn->send_flow_exp && rconn_is_connected(ofconn->rconn)) { | |
3237 | if (prev) { | |
431d8ad2 | 3238 | queue_tx(ofpbuf_clone(buf), prev, prev->reply_counter); |
064af421 BP |
3239 | } else { |
3240 | buf = compose_flow_exp(rule, now, reason); | |
3241 | } | |
3242 | prev = ofconn; | |
3243 | } | |
3244 | } | |
3245 | if (prev) { | |
431d8ad2 | 3246 | queue_tx(buf, prev, prev->reply_counter); |
064af421 BP |
3247 | } |
3248 | } | |
3249 | ||
3250 | static void | |
3251 | uninstall_idle_flow(struct ofproto *ofproto, struct rule *rule) | |
3252 | { | |
3253 | assert(rule->installed); | |
3254 | assert(!rule->cr.wc.wildcards); | |
3255 | ||
3256 | if (rule->super) { | |
3257 | rule_remove(ofproto, rule); | |
3258 | } else { | |
3259 | rule_uninstall(ofproto, rule); | |
3260 | } | |
3261 | } | |
3262 | ||
3263 | static void | |
3264 | expire_rule(struct cls_rule *cls_rule, void *p_) | |
3265 | { | |
3266 | struct ofproto *p = p_; | |
3267 | struct rule *rule = rule_from_cls_rule(cls_rule); | |
3268 | long long int hard_expire, idle_expire, expire, now; | |
3269 | ||
3270 | hard_expire = (rule->hard_timeout | |
3271 | ? rule->created + rule->hard_timeout * 1000 | |
3272 | : LLONG_MAX); | |
3273 | idle_expire = (rule->idle_timeout | |
3274 | && (rule->super || list_is_empty(&rule->list)) | |
3275 | ? rule->used + rule->idle_timeout * 1000 | |
3276 | : LLONG_MAX); | |
3277 | expire = MIN(hard_expire, idle_expire); | |
064af421 BP |
3278 | |
3279 | now = time_msec(); | |
3280 | if (now < expire) { | |
3281 | if (rule->installed && now >= rule->used + 5000) { | |
3282 | uninstall_idle_flow(p, rule); | |
0193b2af JG |
3283 | } else if (!rule->cr.wc.wildcards) { |
3284 | active_timeout(p, rule); | |
064af421 | 3285 | } |
0193b2af | 3286 | |
064af421 BP |
3287 | return; |
3288 | } | |
3289 | ||
3290 | COVERAGE_INC(ofproto_expired); | |
46d6f36f JG |
3291 | |
3292 | /* Update stats. This code will be a no-op if the rule expired | |
3293 | * due to an idle timeout. */ | |
064af421 | 3294 | if (rule->cr.wc.wildcards) { |
064af421 BP |
3295 | struct rule *subrule, *next; |
3296 | LIST_FOR_EACH_SAFE (subrule, next, struct rule, list, &rule->list) { | |
3297 | rule_remove(p, subrule); | |
3298 | } | |
46d6f36f JG |
3299 | } else { |
3300 | rule_uninstall(p, rule); | |
064af421 BP |
3301 | } |
3302 | ||
8fe1a59d JG |
3303 | if (!rule_is_hidden(rule)) { |
3304 | send_flow_exp(p, rule, now, | |
3305 | (now >= hard_expire | |
3306 | ? OFPER_HARD_TIMEOUT : OFPER_IDLE_TIMEOUT)); | |
3307 | } | |
064af421 BP |
3308 | rule_remove(p, rule); |
3309 | } | |
3310 | ||
0193b2af JG |
3311 | static void |
3312 | active_timeout(struct ofproto *ofproto, struct rule *rule) | |
3313 | { | |
3314 | if (ofproto->netflow && !is_controller_rule(rule) && | |
3315 | netflow_active_timeout_expired(ofproto->netflow, &rule->nf_flow)) { | |
3316 | struct ofexpired expired; | |
3317 | struct odp_flow odp_flow; | |
3318 | ||
3319 | /* Get updated flow stats. */ | |
3320 | memset(&odp_flow, 0, sizeof odp_flow); | |
094e1514 JG |
3321 | if (rule->installed) { |
3322 | odp_flow.key = rule->cr.flow; | |
3323 | odp_flow.flags = ODPFF_ZERO_TCP_FLAGS; | |
d65349ea | 3324 | dpif_flow_get(ofproto->dpif, &odp_flow); |
094e1514 JG |
3325 | |
3326 | if (odp_flow.stats.n_packets) { | |
3327 | update_time(ofproto, rule, &odp_flow.stats); | |
3328 | netflow_flow_update_flags(&rule->nf_flow, odp_flow.stats.ip_tos, | |
3329 | odp_flow.stats.tcp_flags); | |
3330 | } | |
0193b2af JG |
3331 | } |
3332 | ||
3333 | expired.flow = rule->cr.flow; | |
3334 | expired.packet_count = rule->packet_count + | |
3335 | odp_flow.stats.n_packets; | |
3336 | expired.byte_count = rule->byte_count + odp_flow.stats.n_bytes; | |
3337 | expired.used = rule->used; | |
3338 | ||
3339 | netflow_expire(ofproto->netflow, &rule->nf_flow, &expired); | |
3340 | ||
3341 | /* Schedule us to send the accumulated records once we have | |
3342 | * collected all of them. */ | |
3343 | poll_immediate_wake(); | |
3344 | } | |
3345 | } | |
3346 | ||
064af421 BP |
3347 | static void |
3348 | update_used(struct ofproto *p) | |
3349 | { | |
3350 | struct odp_flow *flows; | |
3351 | size_t n_flows; | |
3352 | size_t i; | |
3353 | int error; | |
3354 | ||
c228a364 | 3355 | error = dpif_flow_list_all(p->dpif, &flows, &n_flows); |
064af421 BP |
3356 | if (error) { |
3357 | return; | |
3358 | } | |
3359 | ||
3360 | for (i = 0; i < n_flows; i++) { | |
3361 | struct odp_flow *f = &flows[i]; | |
3362 | struct rule *rule; | |
3363 | ||
3364 | rule = rule_from_cls_rule( | |
3365 | classifier_find_rule_exactly(&p->cls, &f->key, 0, UINT16_MAX)); | |
3366 | if (!rule || !rule->installed) { | |
3367 | COVERAGE_INC(ofproto_unexpected_rule); | |
c228a364 | 3368 | dpif_flow_del(p->dpif, f); |
064af421 BP |
3369 | continue; |
3370 | } | |
3371 | ||
0193b2af | 3372 | update_time(p, rule, &f->stats); |
064af421 BP |
3373 | rule_account(p, rule, f->stats.n_bytes); |
3374 | } | |
3375 | free(flows); | |
3376 | } | |
3377 | ||
3378 | static void | |
3379 | do_send_packet_in(struct ofconn *ofconn, uint32_t buffer_id, | |
3380 | const struct ofpbuf *packet, int send_len) | |
3381 | { | |
372179d4 BP |
3382 | struct odp_msg *msg = packet->data; |
3383 | struct ofpbuf payload; | |
3384 | struct ofpbuf *opi; | |
3385 | uint8_t reason; | |
064af421 | 3386 | |
372179d4 | 3387 | /* Extract packet payload from 'msg'. */ |
064af421 BP |
3388 | payload.data = msg + 1; |
3389 | payload.size = msg->length - sizeof *msg; | |
3390 | ||
372179d4 BP |
3391 | /* Construct ofp_packet_in message. */ |
3392 | reason = msg->type == _ODPL_ACTION_NR ? OFPR_ACTION : OFPR_NO_MATCH; | |
3393 | opi = make_packet_in(buffer_id, odp_port_to_ofp_port(msg->port), reason, | |
3394 | &payload, send_len); | |
3395 | ||
3396 | /* Send. */ | |
3397 | rconn_send_with_limit(ofconn->rconn, opi, ofconn->packet_in_counter, 100); | |
064af421 BP |
3398 | } |
3399 | ||
3400 | static void | |
3401 | send_packet_in_action(struct ofpbuf *packet, void *p_) | |
3402 | { | |
3403 | struct ofproto *p = p_; | |
3404 | struct ofconn *ofconn; | |
3405 | struct odp_msg *msg; | |
3406 | ||
3407 | msg = packet->data; | |
3408 | LIST_FOR_EACH (ofconn, struct ofconn, node, &p->all_conns) { | |
3409 | if (ofconn == p->controller || ofconn->miss_send_len) { | |
3410 | do_send_packet_in(ofconn, UINT32_MAX, packet, msg->arg); | |
3411 | } | |
3412 | } | |
3413 | ofpbuf_delete(packet); | |
3414 | } | |
3415 | ||
3416 | static void | |
3417 | send_packet_in_miss(struct ofpbuf *packet, void *p_) | |
3418 | { | |
3419 | struct ofproto *p = p_; | |
7778bd15 | 3420 | bool in_fail_open = p->fail_open && fail_open_is_active(p->fail_open); |
064af421 BP |
3421 | struct ofconn *ofconn; |
3422 | struct ofpbuf payload; | |
3423 | struct odp_msg *msg; | |
3424 | ||
3425 | msg = packet->data; | |
3426 | payload.data = msg + 1; | |
3427 | payload.size = msg->length - sizeof *msg; | |
3428 | LIST_FOR_EACH (ofconn, struct ofconn, node, &p->all_conns) { | |
3429 | if (ofconn->miss_send_len) { | |
7778bd15 BP |
3430 | struct pktbuf *pb = ofconn->pktbuf; |
3431 | uint32_t buffer_id = (in_fail_open | |
3432 | ? pktbuf_get_null() | |
3433 | : pktbuf_save(pb, &payload, msg->port)); | |
064af421 BP |
3434 | int send_len = (buffer_id != UINT32_MAX ? ofconn->miss_send_len |
3435 | : UINT32_MAX); | |
3436 | do_send_packet_in(ofconn, buffer_id, packet, send_len); | |
3437 | } | |
3438 | } | |
3439 | ofpbuf_delete(packet); | |
3440 | } | |
3441 | ||
3442 | static uint64_t | |
fa60c019 | 3443 | pick_datapath_id(const struct ofproto *ofproto) |
064af421 | 3444 | { |
fa60c019 | 3445 | const struct ofport *port; |
064af421 | 3446 | |
fa60c019 BP |
3447 | port = port_array_get(&ofproto->ports, ODPP_LOCAL); |
3448 | if (port) { | |
3449 | uint8_t ea[ETH_ADDR_LEN]; | |
3450 | int error; | |
3451 | ||
3452 | error = netdev_get_etheraddr(port->netdev, ea); | |
064af421 BP |
3453 | if (!error) { |
3454 | return eth_addr_to_uint64(ea); | |
3455 | } | |
3456 | VLOG_WARN("could not get MAC address for %s (%s)", | |
fa60c019 | 3457 | netdev_get_name(port->netdev), strerror(error)); |
064af421 | 3458 | } |
fa60c019 | 3459 | return ofproto->fallback_dpid; |
064af421 BP |
3460 | } |
3461 | ||
3462 | static uint64_t | |
3463 | pick_fallback_dpid(void) | |
3464 | { | |
3465 | uint8_t ea[ETH_ADDR_LEN]; | |
3466 | eth_addr_random(ea); | |
3467 | ea[0] = 0x00; /* Set Nicira OUI. */ | |
3468 | ea[1] = 0x23; | |
3469 | ea[2] = 0x20; | |
3470 | return eth_addr_to_uint64(ea); | |
3471 | } | |
3472 | \f | |
3473 | static bool | |
3474 | default_normal_ofhook_cb(const flow_t *flow, const struct ofpbuf *packet, | |
3475 | struct odp_actions *actions, tag_type *tags, | |
6a07af36 | 3476 | uint16_t *nf_output_iface, void *ofproto_) |
064af421 BP |
3477 | { |
3478 | struct ofproto *ofproto = ofproto_; | |
3479 | int out_port; | |
3480 | ||
3481 | /* Drop frames for reserved multicast addresses. */ | |
3482 | if (eth_addr_is_reserved(flow->dl_dst)) { | |
3483 | return true; | |
3484 | } | |
3485 | ||
3486 | /* Learn source MAC (but don't try to learn from revalidation). */ | |
3487 | if (packet != NULL) { | |
3488 | tag_type rev_tag = mac_learning_learn(ofproto->ml, flow->dl_src, | |
3489 | 0, flow->in_port); | |
3490 | if (rev_tag) { | |
3491 | /* The log messages here could actually be useful in debugging, | |
3492 | * so keep the rate limit relatively high. */ | |
3493 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300); | |
3494 | VLOG_DBG_RL(&rl, "learned that "ETH_ADDR_FMT" is on port %"PRIu16, | |
3495 | ETH_ADDR_ARGS(flow->dl_src), flow->in_port); | |
3496 | ofproto_revalidate(ofproto, rev_tag); | |
3497 | } | |
3498 | } | |
3499 | ||
3500 | /* Determine output port. */ | |
3501 | out_port = mac_learning_lookup_tag(ofproto->ml, flow->dl_dst, 0, tags); | |
3502 | if (out_port < 0) { | |
6a07af36 | 3503 | add_output_group_action(actions, DP_GROUP_FLOOD, nf_output_iface); |
064af421 BP |
3504 | } else if (out_port != flow->in_port) { |
3505 | odp_actions_add(actions, ODPAT_OUTPUT)->output.port = out_port; | |
6a07af36 | 3506 | *nf_output_iface = out_port; |
064af421 BP |
3507 | } else { |
3508 | /* Drop. */ | |
3509 | } | |
3510 | ||
3511 | return true; | |
3512 | } | |
3513 | ||
3514 | static const struct ofhooks default_ofhooks = { | |
3515 | NULL, | |
3516 | default_normal_ofhook_cb, | |
3517 | NULL, | |
3518 | NULL | |
3519 | }; |