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