2 * Copyright (c) 2010 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "wdp-xflow.h"
26 #include "mac-learning.h"
32 #include "openflow/nicira-ext.h"
33 #include "openflow/openflow.h"
35 #include "poll-loop.h"
36 #include "port-array.h"
43 #include "wdp-provider.h"
45 #include "xflow-util.h"
48 #include <linux/types.h> /* XXX */
49 #include <linux/pkt_sched.h> /* XXX */
51 #define THIS_MODULE VLM_wdp_xflow
56 TABLEID_CLASSIFIER = 1
59 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
61 /* Maximum numbers of rules. */
62 #define WX_MAX_WILD 65536 /* Wildcarded rules. */
63 #define WX_MAX_EXACT 1048576 /* Exact-match rules. */
66 struct list list_node;
69 struct classifier cls;
70 struct netdev_monitor *netdev_monitor;
71 struct port_array ports; /* Index is xflow port nr;
72 * wdp_port->opp.port_no is OFP port nr. */
73 struct shash port_by_name;
74 long long int next_expiration;
76 /* Rules that might need to be revalidated. */
77 bool need_revalidate; /* Revalidate all subrules? */
78 bool revalidate_all; /* Revalidate all subrules and other rules? */
79 struct tag_set revalidate_set; /* Tag set of (sub)rules to revalidate. */
81 /* Hooks for ovs-vswitchd. */
82 const struct ofhooks *ofhooks;
85 /* Used by default ofhooks. */
86 struct mac_learning *ml;
89 static const struct ofhooks default_ofhooks;
91 static struct list all_wx = LIST_INITIALIZER(&all_wx);
93 static int wx_port_init(struct wx *);
94 static void wx_port_process_change(struct wx *wx, int error, char *devname,
95 wdp_port_poll_cb_func *cb, void *aux);
96 static void wx_port_refresh_groups(struct wx *);
104 wx_cast(const struct wdp *wdp)
106 return CONTAINER_OF(wdp, struct wx, wdp);
110 wx_xlate_actions(struct wx *, const union ofp_action *, size_t n,
111 const flow_t *flow, const struct ofpbuf *packet,
112 tag_type *tags, struct xflow_actions *out,
113 bool *may_set_up_flow);
118 uint64_t packet_count; /* Number of packets received. */
119 uint64_t byte_count; /* Number of bytes received. */
120 uint64_t accounted_bytes; /* Number of bytes passed to account_cb. */
121 long long int used; /* Last-used time (0 if never used). */
122 tag_type tags; /* Tags (set only by hooks). */
124 /* If 'super' is non-NULL, this rule is a subrule, that is, it is an
125 * exact-match rule (having cr.wc.wildcards of 0) generated from the
126 * wildcard rule 'super'. In this case, 'list' is an element of the
129 * If 'super' is NULL, this rule is a super-rule, and 'list' is the head of
130 * a list of subrules. A super-rule with no wildcards (where
131 * cr.wc.wildcards is 0) will never have any subrules. */
132 struct wx_rule *super;
137 * A super-rule with wildcard fields never has xflow actions (since the
138 * datapath only supports exact-match flows). */
139 bool installed; /* Installed in datapath? */
140 bool may_install; /* True ordinarily; false if actions must
141 * be reassessed for every packet. */
143 union xflow_action *xflow_actions;
146 static void wx_rule_destroy(struct wx *, struct wx_rule *);
147 static void wx_rule_update_actions(struct wx *, struct wx_rule *);
148 static void wx_rule_execute(struct wx *, struct wx_rule *,
149 struct ofpbuf *packet, const flow_t *);
150 static bool wx_rule_make_actions(struct wx *, struct wx_rule *,
151 const struct ofpbuf *packet);
152 static void wx_rule_install(struct wx *, struct wx_rule *,
153 struct wx_rule *displaced_rule);
155 static struct wx_rule *
156 wx_rule_cast(const struct cls_rule *cls_rule)
158 return cls_rule ? CONTAINER_OF(cls_rule, struct wx_rule, wr.cr) : NULL;
161 /* Returns true if 'rule' is merely an implementation detail that should be
162 * hidden from the client. */
164 wx_rule_is_hidden(const struct wx_rule *rule)
166 return rule->super != NULL;
170 wx_rule_free(struct wx_rule *rule)
172 wdp_rule_uninit(&rule->wr);
173 free(rule->xflow_actions);
178 wx_rule_account(struct wx *wx OVS_UNUSED, struct wx_rule *rule OVS_UNUSED,
179 uint64_t extra_bytes OVS_UNUSED)
181 /* XXX call account_cb hook */
185 wx_rule_post_uninstall(struct wx *wx, struct wx_rule *rule)
187 struct wx_rule *super = rule->super;
189 wx_rule_account(wx, rule, 0);
191 /* XXX netflow expiration */
194 super->packet_count += rule->packet_count;
195 super->byte_count += rule->byte_count;
197 /* Reset counters to prevent double counting if the rule ever gets
199 rule->packet_count = 0;
200 rule->byte_count = 0;
201 rule->accounted_bytes = 0;
203 //XXX netflow_flow_clear(&rule->nf_flow);
208 xflow_flow_stats_to_msec(const struct xflow_flow_stats *stats)
210 return (stats->used_sec
211 ? stats->used_sec * 1000 + stats->used_nsec / 1000000
216 wx_rule_update_time(struct wx *wx OVS_UNUSED, struct wx_rule *rule,
217 const struct xflow_flow_stats *stats)
219 long long int used = xflow_flow_stats_to_msec(stats);
220 if (used > rule->used) {
222 if (rule->super && used > rule->super->used) {
223 rule->super->used = used;
225 //XXX netflow_flow_update_time(ofproto->netflow, &rule->nf_flow, used);
230 wx_rule_update_stats(struct wx *wx, struct wx_rule *rule,
231 const struct xflow_flow_stats *stats)
233 if (stats->n_packets) {
234 wx_rule_update_time(wx, rule, stats);
235 rule->packet_count += stats->n_packets;
236 rule->byte_count += stats->n_bytes;
237 /* XXX netflow_flow_update_flags(&rule->nf_flow, stats->ip_tos,
238 stats->tcp_flags); */
243 wx_rule_uninstall(struct wx *wx, struct wx_rule *rule)
245 assert(!rule->wr.cr.flow.wildcards);
246 if (rule->installed) {
247 struct xflow_flow xflow_flow;
249 xflow_key_from_flow(&xflow_flow.key, &rule->wr.cr.flow);
250 xflow_flow.actions = NULL;
251 xflow_flow.n_actions = 0;
252 xflow_flow.flags = 0;
253 if (!xfif_flow_del(wx->xfif, &xflow_flow)) {
254 wx_rule_update_stats(wx, rule, &xflow_flow.stats);
256 rule->installed = false;
258 wx_rule_post_uninstall(wx, rule);
264 is_controller_rule(struct wx_rule *rule)
266 /* If the only action is send to the controller then don't report
267 * NetFlow expiration messages since it is just part of the control
268 * logic for the network and not real traffic. */
272 && rule->super->n_actions == 1
273 && action_outputs_to_port(&rule->super->actions[0],
274 htons(OFPP_CONTROLLER)));
279 wx_rule_remove(struct wx *wx, struct wx_rule *rule)
281 if (rule->wr.cr.flow.wildcards) {
282 COVERAGE_INC(wx_del_wc_flow);
283 wx->need_revalidate = true;
285 wx_rule_uninstall(wx, rule);
287 classifier_remove(&wx->cls, &rule->wr.cr);
288 wx_rule_destroy(wx, rule);
292 wx_rule_revalidate(struct wx *wx, struct wx_rule *rule)
294 const flow_t *flow = &rule->wr.cr.flow;
296 COVERAGE_INC(wx_rule_revalidate);
298 struct wx_rule *super;
299 super = wx_rule_cast(classifier_lookup_wild(&wx->cls, flow));
301 wx_rule_remove(wx, rule);
303 } else if (super != rule->super) {
304 COVERAGE_INC(wx_revalidate_moved);
305 list_remove(&rule->list);
306 list_push_back(&super->list, &rule->list);
308 rule->wr.hard_timeout = super->wr.hard_timeout;
309 rule->wr.idle_timeout = super->wr.idle_timeout;
310 rule->wr.created = super->wr.created;
315 wx_rule_update_actions(wx, rule);
319 /* Destroys 'rule'. If 'rule' is a subrule, also removes it from its
320 * super-rule's list of subrules. If 'rule' is a super-rule, also iterates
321 * through all of its subrules and revalidates them, destroying any that no
322 * longer has a super-rule (which is probably all of them).
324 * Before calling this function, the caller must make have removed 'rule' from
325 * the classifier. If 'rule' is an exact-match rule, the caller is also
326 * responsible for ensuring that it has been uninstalled from the datapath. */
328 wx_rule_destroy(struct wx *wx, struct wx_rule *rule)
331 struct wx_rule *subrule, *next;
332 LIST_FOR_EACH_SAFE (subrule, next, struct wx_rule, list, &rule->list) {
333 wx_rule_revalidate(wx, subrule);
336 list_remove(&rule->list);
343 wx_rule_has_out_port(const struct wx_rule *rule, uint16_t out_port)
345 const union ofp_action *oa;
346 struct actions_iterator i;
348 if (out_port == htons(OFPP_NONE)) {
351 for (oa = actions_first(&i, rule->wr.actions,
354 oa = actions_next(&i)) {
355 if (oa->type == htons(OFPAT_OUTPUT) && oa->output.port == out_port) {
363 /* Caller is responsible for initializing the 'cr' and ofp_table_id members of
364 * the returned rule. */
365 static struct wx_rule *
366 wx_rule_create(struct wx_rule *super,
367 const union ofp_action *actions, size_t n_actions,
368 uint16_t idle_timeout, uint16_t hard_timeout)
370 struct wx_rule *rule = xzalloc(sizeof *rule);
371 wdp_rule_init(&rule->wr, actions, n_actions);
372 rule->wr.idle_timeout = idle_timeout;
373 rule->wr.hard_timeout = hard_timeout;
374 rule->used = rule->wr.created;
377 list_push_back(&super->list, &rule->list);
379 list_init(&rule->list);
382 netflow_flow_clear(&rule->nf_flow);
383 netflow_flow_update_time(ofproto->netflow, &rule->nf_flow, rule->created);
389 /* Executes the actions indicated by 'rule' on 'packet', which is in flow
390 * 'flow' and is considered to have arrived on xflow port 'in_port'.
392 * The flow that 'packet' actually contains does not need to actually match
393 * 'rule'; the actions in 'rule' will be applied to it either way. Likewise,
394 * the packet and byte counters for 'rule' will be credited for the packet sent
395 * out whether or not the packet actually matches 'rule'.
397 * If 'rule' is an exact-match rule and 'flow' actually equals the rule's flow,
398 * the caller must already have accurately composed xflow actions for it given
399 * 'packet' using rule_make_actions(). If 'rule' is a wildcard rule, or if
400 * 'rule' is an exact-match rule but 'flow' is not the rule's flow, then this
401 * function will compose a set of xflow actions based on 'rule''s OpenFlow
402 * actions and apply them to 'packet'. */
404 wx_rule_execute(struct wx *wx, struct wx_rule *rule,
405 struct ofpbuf *packet, const flow_t *flow)
407 const union xflow_action *actions;
409 struct xflow_actions a;
411 /* Grab or compose the xflow actions.
413 * The special case for an exact-match 'rule' where 'flow' is not the
414 * rule's flow is important to avoid, e.g., sending a packet out its input
415 * port simply because the xflow actions were composed for the wrong
417 if (rule->wr.cr.flow.wildcards
418 || !flow_equal(flow, &rule->wr.cr.flow))
420 struct wx_rule *super = rule->super ? rule->super : rule;
421 if (wx_xlate_actions(wx, super->wr.actions, super->wr.n_actions, flow,
422 packet, NULL, &a, NULL)) {
426 n_actions = a.n_actions;
428 actions = rule->xflow_actions;
429 n_actions = rule->n_xflow_actions;
432 /* Execute the xflow actions. */
433 if (!xfif_execute(wx->xfif, flow->in_port,
434 actions, n_actions, packet)) {
435 struct xflow_flow_stats stats;
436 flow_extract_stats(flow, packet, &stats);
437 wx_rule_update_stats(wx, rule, &stats);
438 rule->used = time_msec();
439 //XXX netflow_flow_update_time(wx->netflow, &rule->nf_flow, rule->used);
444 wx_rule_insert(struct wx *wx, struct wx_rule *rule, struct ofpbuf *packet,
447 struct wx_rule *displaced_rule;
449 /* Insert the rule in the classifier. */
450 displaced_rule = wx_rule_cast(classifier_insert(&wx->cls, &rule->wr.cr));
451 if (!rule->wr.cr.flow.wildcards) {
452 wx_rule_make_actions(wx, rule, packet);
455 /* Send the packet and credit it to the rule. */
458 flow_extract(packet, 0, in_port, &flow);
459 wx_rule_execute(wx, rule, packet, &flow);
462 /* Install the rule in the datapath only after sending the packet, to
463 * avoid packet reordering. */
464 if (rule->wr.cr.flow.wildcards) {
465 COVERAGE_INC(wx_add_wc_flow);
466 wx->need_revalidate = true;
468 wx_rule_install(wx, rule, displaced_rule);
471 /* Free the rule that was displaced, if any. */
472 if (displaced_rule) {
473 rule->wr.client_data = displaced_rule->wr.client_data;
474 wx_rule_destroy(wx, displaced_rule);
478 static struct wx_rule *
479 wx_rule_create_subrule(struct wx *wx, struct wx_rule *rule, const flow_t *flow)
481 struct wx_rule *subrule;
483 subrule = wx_rule_create(rule, NULL, 0,
484 rule->wr.idle_timeout,
485 rule->wr.hard_timeout);
486 /* Subrules aren't really in any OpenFlow table, so don't bother with
487 * subrule->wr.ofp_table_id. */
488 COVERAGE_INC(wx_subrule_create);
489 cls_rule_from_flow(flow, &subrule->wr.cr);
490 classifier_insert_exact(&wx->cls, &subrule->wr.cr);
495 /* Returns true if the actions changed, false otherwise. */
497 wx_rule_make_actions(struct wx *wx, struct wx_rule *rule,
498 const struct ofpbuf *packet)
500 const struct wx_rule *super;
501 struct xflow_actions a;
504 assert(!rule->wr.cr.flow.wildcards);
506 super = rule->super ? rule->super : rule;
507 wx_xlate_actions(wx, super->wr.actions, super->wr.n_actions,
508 &rule->wr.cr.flow, packet,
509 &rule->tags, &a, &rule->may_install);
511 actions_len = a.n_actions * sizeof *a.actions;
512 if (rule->n_xflow_actions != a.n_actions
513 || memcmp(rule->xflow_actions, a.actions, actions_len)) {
514 COVERAGE_INC(wx_xflow_unchanged);
515 free(rule->xflow_actions);
516 rule->n_xflow_actions = a.n_actions;
517 rule->xflow_actions = xmemdup(a.actions, actions_len);
525 do_put_flow(struct wx *wx, struct wx_rule *rule, int flags,
526 struct xflow_flow_put *put)
528 memset(&put->flow.stats, 0, sizeof put->flow.stats);
529 xflow_key_from_flow(&put->flow.key, &rule->wr.cr.flow);
530 put->flow.actions = rule->xflow_actions;
531 put->flow.n_actions = rule->n_xflow_actions;
534 return xfif_flow_put(wx->xfif, put);
538 wx_rule_install(struct wx *wx, struct wx_rule *rule, struct wx_rule *displaced_rule)
540 assert(!rule->wr.cr.flow.wildcards);
542 if (rule->may_install) {
543 struct xflow_flow_put put;
544 if (!do_put_flow(wx, rule,
545 XFLOWPF_CREATE | XFLOWPF_MODIFY | XFLOWPF_ZERO_STATS,
547 rule->installed = true;
548 if (displaced_rule) {
549 wx_rule_update_stats(wx, displaced_rule, &put.flow.stats);
550 wx_rule_post_uninstall(wx, displaced_rule);
553 } else if (displaced_rule) {
554 wx_rule_uninstall(wx, displaced_rule);
559 wx_rule_reinstall(struct wx *wx, struct wx_rule *rule)
561 if (rule->installed) {
562 struct xflow_flow_put put;
563 COVERAGE_INC(wx_dp_missed);
564 do_put_flow(wx, rule, XFLOWPF_CREATE | XFLOWPF_MODIFY, &put);
566 wx_rule_install(wx, rule, NULL);
571 wx_rule_update_actions(struct wx *wx, struct wx_rule *rule)
573 bool actions_changed;
575 uint16_t new_out_iface, old_out_iface;
577 old_out_iface = rule->nf_flow.output_iface;
579 actions_changed = wx_rule_make_actions(wx, rule, NULL);
581 if (rule->may_install) {
582 if (rule->installed) {
583 if (actions_changed) {
584 struct xflow_flow_put put;
585 do_put_flow(wx, rule, XFLOWPF_CREATE | XFLOWPF_MODIFY
586 | XFLOWPF_ZERO_STATS, &put);
587 wx_rule_update_stats(wx, rule, &put.flow.stats);
589 /* Temporarily set the old output iface so that NetFlow
590 * messages have the correct output interface for the old
592 new_out_iface = rule->nf_flow.output_iface;
593 rule->nf_flow.output_iface = old_out_iface;
595 wx_rule_post_uninstall(wx, rule);
596 //rule->nf_flow.output_iface = new_out_iface;
599 wx_rule_install(wx, rule, NULL);
602 wx_rule_uninstall(wx, rule);
607 add_output_group_action(struct xflow_actions *actions, uint16_t group,
608 uint16_t *nf_output_iface)
610 xflow_actions_add(actions, XFLOWAT_OUTPUT_GROUP)->output_group.group = group;
612 if (group == WX_GROUP_ALL || group == WX_GROUP_FLOOD) {
613 *nf_output_iface = NF_OUT_FLOOD;
618 add_controller_action(struct xflow_actions *actions,
619 const struct ofp_action_output *oao)
621 union xflow_action *a = xflow_actions_add(actions, XFLOWAT_CONTROLLER);
622 a->controller.arg = ntohs(oao->max_len);
625 struct wx_xlate_ctx {
627 flow_t flow; /* Flow to which these actions correspond. */
628 int recurse; /* Recursion level, via xlate_table_action. */
630 const struct ofpbuf *packet; /* The packet corresponding to 'flow', or a
631 * null pointer if we are revalidating
632 * without a packet to refer to. */
635 struct xflow_actions *out; /* Datapath actions. */
636 tag_type *tags; /* Tags associated with OFPP_NORMAL actions. */
637 bool may_set_up_flow; /* True ordinarily; false if the actions must
638 * be reassessed for every packet. */
639 uint16_t nf_output_iface; /* Output interface index for NetFlow. */
642 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
643 struct wx_xlate_ctx *ctx);
646 add_output_action(struct wx_xlate_ctx *ctx, uint16_t port)
648 const struct wdp_port *wdp_port = port_array_get(&ctx->wx->ports, port);
651 if (wdp_port->opp.config & OFPPC_NO_FWD) {
652 /* Forwarding disabled on port. */
657 * We don't have an ofport record for this port, but it doesn't hurt to
658 * allow forwarding to it anyhow. Maybe such a port will appear later
659 * and we're pre-populating the flow table.
663 xflow_actions_add(ctx->out, XFLOWAT_OUTPUT)->output.port = port;
664 //ctx->nf_output_iface = port;
667 static struct wx_rule *
668 wx_rule_lookup_valid(struct wx *wx, const flow_t *flow)
670 struct wx_rule *rule = wx_rule_cast(classifier_lookup(&wx->cls, flow));
672 /* The rule we found might not be valid, since we could be in need of
673 * revalidation. If it is not valid, don't return it. */
676 && wx->need_revalidate
677 && !wx_rule_revalidate(wx, rule)) {
678 COVERAGE_INC(wx_invalidated);
686 xlate_table_action(struct wx_xlate_ctx *ctx, uint16_t in_port)
689 uint16_t old_in_port;
690 struct wx_rule *rule;
692 /* Look up a flow with 'in_port' as the input port. Then restore the
693 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
694 * have surprising behavior). */
695 old_in_port = ctx->flow.in_port;
696 ctx->flow.in_port = in_port;
697 rule = wx_rule_lookup_valid(ctx->wx, &ctx->flow);
698 ctx->flow.in_port = old_in_port;
706 do_xlate_actions(rule->wr.actions, rule->wr.n_actions, ctx);
713 xlate_output_action(struct wx_xlate_ctx *ctx,
714 const struct ofp_action_output *oao)
717 uint16_t prev_nf_output_iface = ctx->nf_output_iface;
719 ctx->nf_output_iface = NF_OUT_DROP;
721 switch (ntohs(oao->port)) {
723 add_output_action(ctx, ctx->flow.in_port);
726 xlate_table_action(ctx, ctx->flow.in_port);
729 if (!ctx->wx->ofhooks->normal_cb(&ctx->flow, ctx->packet,
731 &ctx->nf_output_iface,
733 COVERAGE_INC(wx_uninstallable);
734 ctx->may_set_up_flow = false;
739 add_output_group_action(ctx->out, WX_GROUP_FLOOD,
740 &ctx->nf_output_iface);
743 add_output_group_action(ctx->out, WX_GROUP_ALL, &ctx->nf_output_iface);
745 case OFPP_CONTROLLER:
746 add_controller_action(ctx->out, oao);
749 add_output_action(ctx, XFLOWP_LOCAL);
752 xflow_port = ofp_port_to_xflow_port(ntohs(oao->port));
753 if (xflow_port != ctx->flow.in_port) {
754 add_output_action(ctx, xflow_port);
759 if (prev_nf_output_iface == NF_OUT_FLOOD) {
760 ctx->nf_output_iface = NF_OUT_FLOOD;
761 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
762 ctx->nf_output_iface = prev_nf_output_iface;
763 } else if (prev_nf_output_iface != NF_OUT_DROP &&
764 ctx->nf_output_iface != NF_OUT_FLOOD) {
765 ctx->nf_output_iface = NF_OUT_MULTI;
769 /* If the final xflow action in 'ctx' is "pop priority", drop it, as an
770 * optimization, because we're going to add another action that sets the
771 * priority immediately after, or because there are no actions following the
774 remove_pop_action(struct wx_xlate_ctx *ctx)
776 size_t n = ctx->out->n_actions;
777 if (n > 0 && ctx->out->actions[n - 1].type == XFLOWAT_POP_PRIORITY) {
778 ctx->out->n_actions--;
783 xlate_enqueue_action(struct wx_xlate_ctx *ctx,
784 const struct ofp_action_enqueue *oae)
786 uint16_t ofp_port, xflow_port;
788 /* Figure out xflow output port. */
789 ofp_port = ntohs(oae->port);
790 if (ofp_port != OFPP_IN_PORT) {
791 xflow_port = ofp_port_to_xflow_port(ofp_port);
793 xflow_port = ctx->flow.in_port;
796 /* Add xflow actions. */
797 remove_pop_action(ctx);
798 xflow_actions_add(ctx->out, XFLOWAT_SET_PRIORITY)->priority.priority
799 = TC_H_MAKE(1, ntohl(oae->queue_id)); /* XXX */
800 add_output_action(ctx, xflow_port);
801 xflow_actions_add(ctx->out, XFLOWAT_POP_PRIORITY);
803 /* Update NetFlow output port. */
804 if (ctx->nf_output_iface == NF_OUT_DROP) {
805 ctx->nf_output_iface = xflow_port;
806 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
807 ctx->nf_output_iface = NF_OUT_MULTI;
812 xlate_nicira_action(struct wx_xlate_ctx *ctx,
813 const struct nx_action_header *nah)
815 const struct nx_action_resubmit *nar;
816 const struct nx_action_set_tunnel *nast;
817 union xflow_action *oa;
818 int subtype = ntohs(nah->subtype);
820 assert(nah->vendor == htonl(NX_VENDOR_ID));
823 nar = (const struct nx_action_resubmit *) nah;
824 xlate_table_action(ctx, ofp_port_to_xflow_port(ntohs(nar->in_port)));
827 case NXAST_SET_TUNNEL:
828 nast = (const struct nx_action_set_tunnel *) nah;
829 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_TUNNEL);
830 ctx->flow.tun_id = oa->tunnel.tun_id = nast->tun_id;
833 /* If you add a new action here that modifies flow data, don't forget to
834 * update the flow key in ctx->flow at the same time. */
837 VLOG_DBG_RL(&rl, "unknown Nicira action type %"PRIu16, subtype);
843 do_xlate_actions(const union ofp_action *in, size_t n_in,
844 struct wx_xlate_ctx *ctx)
846 struct actions_iterator iter;
847 const union ofp_action *ia;
848 const struct wdp_port *port;
850 port = port_array_get(&ctx->wx->ports, ctx->flow.in_port);
851 if (port && port->opp.config & (OFPPC_NO_RECV | OFPPC_NO_RECV_STP) &&
852 port->opp.config & (eth_addr_equals(ctx->flow.dl_dst, stp_eth_addr)
853 ? OFPPC_NO_RECV_STP : OFPPC_NO_RECV)) {
854 /* Drop this flow. */
858 for (ia = actions_first(&iter, in, n_in); ia; ia = actions_next(&iter)) {
859 uint16_t type = ntohs(ia->type);
860 union xflow_action *oa;
864 xlate_output_action(ctx, &ia->output);
867 case OFPAT_SET_VLAN_VID:
868 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_DL_TCI);
869 oa->dl_tci.tci = ia->vlan_vid.vlan_vid & htons(VLAN_VID_MASK);
870 oa->dl_tci.mask = htons(VLAN_VID_MASK);
871 ctx->flow.dl_vlan = ia->vlan_vid.vlan_vid;
874 case OFPAT_SET_VLAN_PCP:
875 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_DL_TCI);
876 oa->dl_tci.tci = htons((ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT)
878 oa->dl_tci.mask = htons(VLAN_PCP_MASK);
880 if (ctx->flow.dl_vlan == htons(OFP_VLAN_NONE)) {
881 ctx->flow.dl_vlan = htons(0);
883 ctx->flow.dl_vlan_pcp = ia->vlan_pcp.vlan_pcp;
886 case OFPAT_STRIP_VLAN:
887 xflow_actions_add(ctx->out, XFLOWAT_STRIP_VLAN);
888 ctx->flow.dl_vlan = htons(OFP_VLAN_NONE);
889 ctx->flow.dl_vlan_pcp = 0;
892 case OFPAT_SET_DL_SRC:
893 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_DL_SRC);
894 memcpy(oa->dl_addr.dl_addr,
895 ((struct ofp_action_dl_addr *) ia)->dl_addr, ETH_ADDR_LEN);
896 memcpy(ctx->flow.dl_src,
897 ((struct ofp_action_dl_addr *) ia)->dl_addr, ETH_ADDR_LEN);
900 case OFPAT_SET_DL_DST:
901 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_DL_DST);
902 memcpy(oa->dl_addr.dl_addr,
903 ((struct ofp_action_dl_addr *) ia)->dl_addr, ETH_ADDR_LEN);
904 memcpy(ctx->flow.dl_dst,
905 ((struct ofp_action_dl_addr *) ia)->dl_addr, ETH_ADDR_LEN);
908 case OFPAT_SET_NW_SRC:
909 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_NW_SRC);
910 ctx->flow.nw_src = oa->nw_addr.nw_addr = ia->nw_addr.nw_addr;
913 case OFPAT_SET_NW_DST:
914 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_NW_DST);
915 ctx->flow.nw_dst = oa->nw_addr.nw_addr = ia->nw_addr.nw_addr;
918 case OFPAT_SET_NW_TOS:
919 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_NW_TOS);
920 ctx->flow.nw_tos = oa->nw_tos.nw_tos = ia->nw_tos.nw_tos;
923 case OFPAT_SET_TP_SRC:
924 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_TP_SRC);
925 ctx->flow.tp_src = oa->tp_port.tp_port = ia->tp_port.tp_port;
928 case OFPAT_SET_TP_DST:
929 oa = xflow_actions_add(ctx->out, XFLOWAT_SET_TP_DST);
930 ctx->flow.tp_dst = oa->tp_port.tp_port = ia->tp_port.tp_port;
934 xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
938 xlate_nicira_action(ctx, (const struct nx_action_header *) ia);
942 VLOG_DBG_RL(&rl, "unknown action type %"PRIu16, type);
948 /* Returns true if 'flow' and 'actions' may be set up as a flow in the kernel.
949 * This is true most of the time, but we don't allow flows that would prevent
950 * DHCP replies from being seen by the local port to be set up in the
953 * We only need this, strictly speaking, when in-band control is turned on. */
955 wx_may_set_up(const flow_t *flow, const struct xflow_actions *actions)
957 if (flow->dl_type == htons(ETH_TYPE_IP)
958 && flow->nw_proto == IP_TYPE_UDP
959 && flow->tp_src == htons(DHCP_SERVER_PORT)
960 && flow->tp_dst == htons(DHCP_CLIENT_PORT)) {
963 for (i = 0; i < actions->n_actions; i++) {
964 const struct xflow_action_output *oao = &actions->actions[i].output;
965 if (oao->type == XFLOWAT_OUTPUT && oao->port == XFLOWP_LOCAL) {
976 wx_xlate_actions(struct wx *wx, const union ofp_action *in, size_t n_in,
977 const flow_t *flow, const struct ofpbuf *packet,
978 tag_type *tags, struct xflow_actions *out,
979 bool *may_set_up_flow)
981 tag_type no_tags = 0;
982 struct wx_xlate_ctx ctx;
983 COVERAGE_INC(wx_ofp2xflow);
984 xflow_actions_init(out);
990 ctx.tags = tags ? tags : &no_tags;
991 ctx.may_set_up_flow = true;
992 ctx.nf_output_iface = NF_OUT_DROP;
993 do_xlate_actions(in, n_in, &ctx);
994 remove_pop_action(&ctx);
996 if (may_set_up_flow) {
997 *may_set_up_flow = ctx.may_set_up_flow && wx_may_set_up(flow, out);
1000 if (nf_output_iface) {
1001 *nf_output_iface = ctx.nf_output_iface;
1004 if (xflow_actions_overflow(out)) {
1005 xflow_actions_init(out);
1006 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_TOO_MANY);
1012 update_used(struct wx *wx)
1014 struct xflow_flow *flows;
1019 error = xfif_flow_list_all(wx->xfif, &flows, &n_flows);
1024 for (i = 0; i < n_flows; i++) {
1025 struct xflow_flow *f = &flows[i];
1026 struct wx_rule *rule;
1029 xflow_key_to_flow(&f->key, &flow);
1030 rule = wx_rule_cast(classifier_find_rule_exactly(&wx->cls, &flow));
1031 if (!rule || !rule->installed) {
1032 COVERAGE_INC(wx_unexpected_rule);
1033 xfif_flow_del(wx->xfif, f);
1037 wx_rule_update_time(wx, rule, &f->stats);
1038 wx_rule_account(wx, rule, f->stats.n_bytes);
1044 uninstall_idle_flow(struct wx *wx, struct wx_rule *rule)
1046 assert(rule->installed);
1047 assert(!rule->wr.cr.flow.wildcards);
1050 wx_rule_remove(wx, rule);
1052 wx_rule_uninstall(wx, rule);
1057 expire_rule(struct cls_rule *cls_rule, void *wx_)
1059 struct wx *wx = wx_;
1060 struct wx_rule *rule = wx_rule_cast(cls_rule);
1061 long long int hard_expire, idle_expire, expire, now;
1063 hard_expire = (rule->wr.hard_timeout
1064 ? rule->wr.created + rule->wr.hard_timeout * 1000
1066 idle_expire = (rule->wr.idle_timeout
1067 && (rule->super || list_is_empty(&rule->list))
1068 ? rule->used + rule->wr.idle_timeout * 1000
1070 expire = MIN(hard_expire, idle_expire);
1074 if (rule->installed && now >= rule->used + 5000) {
1075 uninstall_idle_flow(wx, rule);
1076 } else if (!rule->wr.cr.flow.wildcards) {
1077 //XXX active_timeout(wx, rule);
1083 COVERAGE_INC(wx_expired);
1085 /* Update stats. This code will be a no-op if the rule expired
1086 * due to an idle timeout. */
1087 if (rule->wr.cr.flow.wildcards) {
1088 struct wx_rule *subrule, *next;
1089 LIST_FOR_EACH_SAFE (subrule, next, struct wx_rule, list, &rule->list) {
1090 wx_rule_remove(wx, subrule);
1093 wx_rule_uninstall(wx, rule);
1097 if (!wx_rule_is_hidden(rule)) {
1098 send_flow_removed(wx, rule, now,
1100 ? OFPRR_HARD_TIMEOUT : OFPRR_IDLE_TIMEOUT));
1103 wx_rule_remove(wx, rule);
1106 struct revalidate_cbdata {
1108 bool revalidate_all; /* Revalidate all exact-match rules? */
1109 bool revalidate_subrules; /* Revalidate all exact-match subrules? */
1110 struct tag_set revalidate_set; /* Set of tags to revalidate. */
1114 revalidate_rule(struct wx *wx, struct wx_rule *rule)
1116 const flow_t *flow = &rule->wr.cr.flow;
1118 COVERAGE_INC(wx_revalidate_rule);
1120 struct wx_rule *super;
1121 super = wx_rule_cast(classifier_lookup_wild(&wx->cls, flow));
1123 wx_rule_remove(wx, rule);
1125 } else if (super != rule->super) {
1126 COVERAGE_INC(wx_revalidate_moved);
1127 list_remove(&rule->list);
1128 list_push_back(&super->list, &rule->list);
1129 rule->super = super;
1130 rule->wr.hard_timeout = super->wr.hard_timeout;
1131 rule->wr.idle_timeout = super->wr.idle_timeout;
1132 rule->wr.created = super->wr.created;
1137 wx_rule_update_actions(wx, rule);
1142 revalidate_cb(struct cls_rule *sub_, void *cbdata_)
1144 struct wx_rule *sub = wx_rule_cast(sub_);
1145 struct revalidate_cbdata *cbdata = cbdata_;
1147 if (cbdata->revalidate_all
1148 || (cbdata->revalidate_subrules && sub->super)
1149 || tag_set_intersects(&cbdata->revalidate_set, sub->tags)) {
1150 revalidate_rule(cbdata->wx, sub);
1155 wx_run_one(struct wx *wx)
1157 if (time_msec() >= wx->next_expiration) {
1158 COVERAGE_INC(wx_expiration);
1159 wx->next_expiration = time_msec() + 1000;
1162 classifier_for_each(&wx->cls, CLS_INC_ALL, expire_rule, wx);
1164 /* XXX account_checkpoint_cb */
1167 if (wx->need_revalidate || !tag_set_is_empty(&wx->revalidate_set)) {
1168 struct revalidate_cbdata cbdata;
1170 cbdata.revalidate_all = wx->revalidate_all;
1171 cbdata.revalidate_subrules = wx->need_revalidate;
1172 cbdata.revalidate_set = wx->revalidate_set;
1173 tag_set_init(&wx->revalidate_set);
1174 COVERAGE_INC(wx_revalidate);
1175 classifier_for_each(&wx->cls, CLS_INC_EXACT, revalidate_cb, &cbdata);
1176 wx->need_revalidate = false;
1185 LIST_FOR_EACH (wx, struct wx, list_node, &all_wx) {
1192 wx_wait_one(struct wx *wx)
1194 if (wx->need_revalidate || !tag_set_is_empty(&wx->revalidate_set)) {
1195 poll_immediate_wake();
1196 } else if (wx->next_expiration != LLONG_MAX) {
1197 poll_timer_wait_until(wx->next_expiration);
1206 LIST_FOR_EACH (wx, struct wx, list_node, &all_wx) {
1212 static int wx_flow_flush(struct wdp *);
1215 wx_enumerate(const struct wdp_class *wdp_class, struct svec *all_wdps)
1217 struct svec names = SVEC_EMPTY_INITIALIZER;
1218 int error = xf_enumerate_names(wdp_class->type, &names);
1219 svec_move(all_wdps, &names);
1224 wx_open(const struct wdp_class *wdp_class, const char *name, bool create,
1231 ? xfif_create_and_open(name, wdp_class->type, &xfif)
1232 : xfif_open(name, wdp_class->type, &xfif));
1236 wx = xzalloc(sizeof *wx);
1237 list_push_back(&all_wx, &wx->list_node);
1238 wdp_init(&wx->wdp, wdp_class, name, 0, 0);
1240 classifier_init(&wx->cls);
1241 wx->netdev_monitor = netdev_monitor_create();
1242 port_array_init(&wx->ports);
1243 shash_init(&wx->port_by_name);
1244 wx->next_expiration = time_msec() + 1000;
1245 tag_set_init(&wx->revalidate_set);
1249 wx->ofhooks = &default_ofhooks;
1251 wx->ml = mac_learning_create();
1260 wx_close(struct wdp *wdp)
1262 struct wx *wx = wx_cast(wdp);
1265 xfif_close(wx->xfif);
1266 classifier_destroy(&wx->cls);
1267 netdev_monitor_destroy(wx->netdev_monitor);
1268 list_remove(&wx->list_node);
1269 mac_learning_destroy(wx->ml);
1274 wx_get_all_names(const struct wdp *wdp, struct svec *all_names)
1276 struct wx *wx = wx_cast(wdp);
1278 return xfif_get_all_names(wx->xfif, all_names);
1282 wx_destroy(struct wdp *wdp)
1284 struct wx *wx = wx_cast(wdp);
1286 return xfif_delete(wx->xfif);
1290 wx_get_features(const struct wdp *wdp, struct ofpbuf **featuresp)
1292 struct wx *wx = wx_cast(wdp);
1293 struct ofp_switch_features *osf;
1295 unsigned int port_no;
1296 struct wdp_port *port;
1298 buf = ofpbuf_new(sizeof *osf);
1299 osf = ofpbuf_put_zeros(buf, sizeof *osf);
1301 osf->capabilities = htonl(OFPC_ARP_MATCH_IP);
1302 osf->actions = htonl((1u << OFPAT_OUTPUT) |
1303 (1u << OFPAT_SET_VLAN_VID) |
1304 (1u << OFPAT_SET_VLAN_PCP) |
1305 (1u << OFPAT_STRIP_VLAN) |
1306 (1u << OFPAT_SET_DL_SRC) |
1307 (1u << OFPAT_SET_DL_DST) |
1308 (1u << OFPAT_SET_NW_SRC) |
1309 (1u << OFPAT_SET_NW_DST) |
1310 (1u << OFPAT_SET_NW_TOS) |
1311 (1u << OFPAT_SET_TP_SRC) |
1312 (1u << OFPAT_SET_TP_DST) |
1313 (1u << OFPAT_ENQUEUE));
1315 PORT_ARRAY_FOR_EACH (port, &wx->ports, port_no) {
1316 hton_ofp_phy_port(ofpbuf_put(buf, &port->opp, sizeof port->opp));
1324 count_subrules(struct cls_rule *cls_rule, void *n_subrules_)
1326 struct wx_rule *rule = wx_rule_cast(cls_rule);
1327 int *n_subrules = n_subrules_;
1335 wx_get_stats(const struct wdp *wdp, struct wdp_stats *stats)
1337 struct wx *wx = wx_cast(wdp);
1338 struct xflow_stats xflow_stats;
1341 error = xfif_get_xf_stats(wx->xfif, &xflow_stats);
1342 stats->max_ports = xflow_stats.max_ports;
1347 wx_get_table_stats(const struct wdp *wdp, struct ofpbuf *stats)
1349 struct wx *wx = wx_cast(wdp);
1350 struct xflow_stats xflow_stats;
1351 struct ofp_table_stats *exact, *wild;
1354 xfif_get_xf_stats(wx->xfif, &xflow_stats);
1355 /* XXX should pass up errors, but there are no appropriate OpenFlow error
1359 classifier_for_each(&wx->cls, CLS_INC_EXACT, count_subrules, &n_subrules);
1361 exact = ofpbuf_put_zeros(stats, sizeof *exact);
1362 exact->table_id = TABLEID_HASH;
1363 strcpy(exact->name, "exact");
1364 exact->wildcards = htonl(0);
1365 exact->max_entries = htonl(MIN(WX_MAX_EXACT, xflow_stats.max_capacity));
1366 exact->active_count = htonl(classifier_count_exact(&wx->cls) - n_subrules);
1367 exact->lookup_count = htonll(xflow_stats.n_hit + xflow_stats.n_missed);
1368 exact->matched_count = htonll(xflow_stats.n_hit);
1370 wild = ofpbuf_put_zeros(stats, sizeof *exact);
1371 wild->table_id = TABLEID_CLASSIFIER;
1372 strcpy(wild->name, "classifier");
1373 wild->wildcards = htonl(OVSFW_ALL);
1374 wild->max_entries = htonl(WX_MAX_WILD);
1375 wild->active_count = htonl(classifier_count_wild(&wx->cls));
1376 wild->lookup_count = htonll(0); /* XXX */
1377 wild->matched_count = htonll(0); /* XXX */
1383 wx_get_drop_frags(const struct wdp *wdp, bool *drop_frags)
1385 struct wx *wx = wx_cast(wdp);
1387 return xfif_get_drop_frags(wx->xfif, drop_frags);
1391 wx_set_drop_frags(struct wdp *wdp, bool drop_frags)
1393 struct wx *wx = wx_cast(wdp);
1395 return xfif_set_drop_frags(wx->xfif, drop_frags);
1399 wx_port_add(struct wdp *wdp, const char *devname,
1400 bool internal, uint16_t *port_no)
1402 struct wx *wx = wx_cast(wdp);
1403 uint16_t xflow_flags = internal ? XFLOW_PORT_INTERNAL : 0;
1404 return xfif_port_add(wx->xfif, devname, xflow_flags, port_no);
1408 wx_port_del(struct wdp *wdp, uint16_t port_no)
1410 struct wx *wx = wx_cast(wdp);
1412 return xfif_port_del(wx->xfif, port_no);
1416 wx_answer_port_query(const struct wdp_port *port, struct wdp_port *portp)
1419 wdp_port_copy(portp, port);
1427 wx_port_query_by_number(const struct wdp *wdp, uint16_t port_no,
1428 struct wdp_port *portp)
1430 struct wx *wx = wx_cast(wdp);
1431 const struct wdp_port *port;
1433 port = port_array_get(&wx->ports, ofp_port_to_xflow_port(port_no));
1434 return wx_answer_port_query(port, portp);
1438 wx_port_query_by_name(const struct wdp *wdp, const char *devname,
1439 struct wdp_port *portp)
1441 struct wx *wx = wx_cast(wdp);
1443 return wx_answer_port_query(shash_find_data(&wx->port_by_name, devname),
1448 wx_port_set_config(struct wdp *wdp, uint16_t port_no, uint32_t config)
1450 struct wx *wx = wx_cast(wdp);
1451 struct wdp_port *port;
1454 port = port_array_get(&wx->ports, ofp_port_to_xflow_port(port_no));
1458 changes = config ^ port->opp.config;
1460 if (changes & OFPPC_PORT_DOWN) {
1462 if (config & OFPPC_PORT_DOWN) {
1463 error = netdev_turn_flags_off(port->netdev, NETDEV_UP, true);
1465 error = netdev_turn_flags_on(port->netdev, NETDEV_UP, true);
1468 port->opp.config ^= OFPPC_PORT_DOWN;
1472 #define REVALIDATE_BITS (OFPPC_NO_RECV | OFPPC_NO_RECV_STP | OFPPC_NO_FWD)
1473 if (changes & REVALIDATE_BITS) {
1474 COVERAGE_INC(wx_costly_flags);
1475 port->opp.config ^= changes & REVALIDATE_BITS;
1476 wx->need_revalidate = true;
1478 #undef REVALIDATE_BITS
1480 if (changes & OFPPC_NO_FLOOD) {
1481 port->opp.config ^= OFPPC_NO_FLOOD;
1482 wx_port_refresh_groups(wx);
1485 if (changes & OFPPC_NO_PACKET_IN) {
1486 port->opp.config ^= OFPPC_NO_PACKET_IN;
1493 wx_port_list(const struct wdp *wdp, struct wdp_port **portsp, size_t *n_portsp)
1495 struct wx *wx = wx_cast(wdp);
1496 struct wdp_port *ports, *port;
1497 unsigned int port_no;
1500 *n_portsp = n_ports = port_array_count(&wx->ports);
1501 *portsp = ports = xmalloc(n_ports * sizeof *ports);
1503 PORT_ARRAY_FOR_EACH (port, &wx->ports, port_no) {
1504 wdp_port_copy(&ports[i++], port);
1506 assert(i == n_ports);
1512 wx_port_poll(struct wdp *wdp, wdp_port_poll_cb_func *cb, void *aux)
1514 struct wx *wx = wx_cast(wdp);
1520 while ((error = xfif_port_poll(wx->xfif, &devname)) != EAGAIN) {
1521 wx_port_process_change(wx, error, devname, cb, aux);
1522 if (error && error != ENOBUFS) {
1526 while ((error = netdev_monitor_poll(wx->netdev_monitor,
1527 &devname)) != EAGAIN) {
1528 wx_port_process_change(wx, error, devname, cb, aux);
1529 if (error && error != ENOBUFS) {
1537 wx_port_poll_wait(const struct wdp *wdp)
1539 struct wx *wx = wx_cast(wdp);
1541 xfif_port_poll_wait(wx->xfif);
1542 netdev_monitor_poll_wait(wx->netdev_monitor);
1546 static struct wdp_rule *
1547 wx_flow_get(const struct wdp *wdp, const flow_t *flow)
1549 struct wx *wx = wx_cast(wdp);
1550 struct wx_rule *rule;
1552 rule = wx_rule_cast(classifier_find_rule_exactly(&wx->cls, flow));
1553 return rule && !wx_rule_is_hidden(rule) ? &rule->wr : NULL;
1556 static struct wdp_rule *
1557 wx_flow_match(const struct wdp *wdp, const flow_t *flow)
1559 struct wx *wx = wx_cast(wdp);
1560 struct wx_rule *rule;
1562 rule = wx_rule_cast(classifier_lookup(&wx->cls, flow));
1564 if (wx_rule_is_hidden(rule)) {
1573 struct wx_for_each_thunk_aux {
1574 wdp_flow_cb_func *client_callback;
1579 wx_for_each_thunk(struct cls_rule *cls_rule, void *aux_)
1581 struct wx_for_each_thunk_aux *aux = aux_;
1582 struct wx_rule *rule = wx_rule_cast(cls_rule);
1584 if (!wx_rule_is_hidden(rule)) {
1585 aux->client_callback(&rule->wr, aux->client_aux);
1590 wx_flow_for_each_match(const struct wdp *wdp, const flow_t *target,
1591 unsigned int include,
1592 wdp_flow_cb_func *client_callback, void *client_aux)
1594 struct wx *wx = wx_cast(wdp);
1595 struct wx_for_each_thunk_aux aux;
1599 if (include & (1u << TABLEID_HASH)) {
1600 cls_include |= CLS_INC_EXACT;
1602 if (include & (1u << TABLEID_CLASSIFIER)) {
1603 cls_include |= CLS_INC_WILD;
1606 aux.client_callback = client_callback;
1607 aux.client_aux = client_aux;
1608 classifier_for_each_match(&wx->cls, target, cls_include,
1609 wx_for_each_thunk, &aux);
1612 /* Obtains statistic counters for 'rule' within 'wx' and stores them into
1613 * '*stats'. If 'rule' is a wildcarded rule, the returned statistic include
1614 * statistics for all of 'rule''s subrules. */
1616 query_stats(struct wx *wx, struct wx_rule *rule, struct wdp_flow_stats *stats)
1618 struct wx_rule *subrule;
1619 struct xflow_flow *xflow_flows;
1620 size_t n_xflow_flows;
1622 /* Start from historical data for 'rule' itself that are no longer tracked
1623 * by the datapath. This counts, for example, subrules that have
1625 stats->n_packets = rule->packet_count;
1626 stats->n_bytes = rule->byte_count;
1627 stats->inserted = rule->wr.created;
1628 stats->used = LLONG_MIN;
1629 stats->tcp_flags = 0;
1632 /* Prepare to ask the datapath for statistics on 'rule', or if it is
1633 * wildcarded then on all of its subrules.
1635 * Also, add any statistics that are not tracked by the datapath for each
1636 * subrule. This includes, for example, statistics for packets that were
1637 * executed "by hand" by ofproto via xfif_execute() but must be accounted
1639 n_xflow_flows = rule->wr.cr.flow.wildcards ? list_size(&rule->list) : 1;
1640 xflow_flows = xzalloc(n_xflow_flows * sizeof *xflow_flows);
1641 if (rule->wr.cr.flow.wildcards) {
1643 LIST_FOR_EACH (subrule, struct wx_rule, list, &rule->list) {
1644 xflow_key_from_flow(&xflow_flows[i++].key, &subrule->wr.cr.flow);
1645 stats->n_packets += subrule->packet_count;
1646 stats->n_bytes += subrule->byte_count;
1649 xflow_key_from_flow(&xflow_flows[0].key, &rule->wr.cr.flow);
1652 /* Fetch up-to-date statistics from the datapath and add them in. */
1653 if (!xfif_flow_get_multiple(wx->xfif, xflow_flows, n_xflow_flows)) {
1655 for (i = 0; i < n_xflow_flows; i++) {
1656 struct xflow_flow *xflow_flow = &xflow_flows[i];
1659 stats->n_packets += xflow_flow->stats.n_packets;
1660 stats->n_bytes += xflow_flow->stats.n_bytes;
1661 used = xflow_flow_stats_to_msec(&xflow_flow->stats);
1662 if (used > stats->used) {
1664 if (xflow_flow->key.dl_type == htons(ETH_TYPE_IP)
1665 && xflow_flow->key.nw_proto == IP_TYPE_TCP) {
1666 stats->ip_tos = xflow_flow->stats.ip_tos;
1669 stats->tcp_flags |= xflow_flow->stats.tcp_flags;
1676 wx_flow_get_stats(const struct wdp *wdp,
1677 const struct wdp_rule *wdp_rule,
1678 struct wdp_flow_stats *stats)
1680 struct wx *wx = wx_cast(wdp);
1681 struct wx_rule *rule = wx_rule_cast(&wdp_rule->cr);
1683 query_stats(wx, rule, stats);
1688 wx_flow_overlaps(const struct wdp *wdp, const flow_t *flow)
1690 struct wx *wx = wx_cast(wdp);
1692 /* XXX overlap with a subrule? */
1693 return classifier_rule_overlaps(&wx->cls, flow);
1697 wx_flow_put(struct wdp *wdp, const struct wdp_flow_put *put,
1698 struct wdp_flow_stats *old_stats, struct wdp_rule **rulep)
1700 struct wx *wx = wx_cast(wdp);
1701 struct wx_rule *rule;
1703 rule = wx_rule_cast(classifier_find_rule_exactly(&wx->cls, put->flow));
1704 if (rule && wx_rule_is_hidden(rule)) {
1709 if (!(put->flags & WDP_PUT_MODIFY)) {
1713 if (!(put->flags & WDP_PUT_CREATE)) {
1716 if ((put->flow->wildcards
1717 ? classifier_count_wild(&wx->cls) >= WX_MAX_WILD
1718 : classifier_count_exact(&wx->cls) >= WX_MAX_EXACT)) {
1719 /* XXX subrules should not count against exact-match limit */
1724 rule = wx_rule_create(NULL, put->actions, put->n_actions,
1725 put->idle_timeout, put->hard_timeout);
1726 cls_rule_from_flow(put->flow, &rule->wr.cr);
1727 rule->wr.ofp_table_id = (put->flow->wildcards
1728 ? TABLEID_CLASSIFIER : TABLEID_HASH);
1729 wx_rule_insert(wx, rule, NULL, 0);
1733 memset(old_stats, 0, sizeof *old_stats);
1743 wx_flow_delete(struct wdp *wdp, struct wdp_rule *wdp_rule,
1744 struct wdp_flow_stats *final_stats)
1746 struct wx *wx = wx_cast(wdp);
1747 struct wx_rule *rule = wx_rule_cast(&wdp_rule->cr);
1749 wx_rule_remove(wx, rule);
1751 memset(final_stats, 0, sizeof *final_stats); /* XXX */
1757 wx_flush_rule(struct cls_rule *cls_rule, void *wx_)
1759 struct wx_rule *rule = wx_rule_cast(cls_rule);
1760 struct wx *wx = wx_;
1762 /* Mark the flow as not installed, even though it might really be
1763 * installed, so that wx_rule_remove() doesn't bother trying to uninstall
1764 * it. There is no point in uninstalling it individually since we are
1765 * about to blow away all the flows with xfif_flow_flush(). */
1766 rule->installed = false;
1768 wx_rule_remove(wx, rule);
1772 wx_flow_flush(struct wdp *wdp)
1774 struct wx *wx = wx_cast(wdp);
1776 COVERAGE_INC(wx_flow_flush);
1777 classifier_for_each(&wx->cls, CLS_INC_ALL, wx_flush_rule, wx);
1778 xfif_flow_flush(wx->xfif);
1783 wx_execute(struct wdp *wdp, uint16_t in_port,
1784 const union ofp_action actions[], int n_actions,
1785 const struct ofpbuf *packet)
1787 struct wx *wx = wx_cast(wdp);
1788 struct xflow_actions xflow_actions;
1792 flow_extract((struct ofpbuf *) packet, 0, in_port, &flow);
1793 error = wx_xlate_actions(wx, actions, n_actions, &flow, packet,
1794 NULL, &xflow_actions, NULL);
1798 xfif_execute(wx->xfif, ofp_port_to_xflow_port(in_port),
1799 xflow_actions.actions, xflow_actions.n_actions, packet);
1804 wx_flow_inject(struct wdp *wdp, struct wdp_rule *wdp_rule,
1805 uint16_t in_port, const struct ofpbuf *packet)
1807 struct wx_rule *rule = wx_rule_cast(&wdp_rule->cr);
1810 error = wx_execute(wdp, in_port, rule->wr.actions, rule->wr.n_actions,
1813 rule->packet_count++;
1814 rule->byte_count += packet->size;
1815 rule->used = time_msec();
1821 wx_recv_get_mask(const struct wdp *wdp, int *listen_mask)
1823 struct wx *wx = wx_cast(wdp);
1824 int xflow_listen_mask;
1827 error = xfif_recv_get_mask(wx->xfif, &xflow_listen_mask);
1830 if (xflow_listen_mask & XFLOWL_MISS) {
1831 *listen_mask |= 1 << WDP_CHAN_MISS;
1833 if (xflow_listen_mask & XFLOWL_ACTION) {
1834 *listen_mask |= 1 << WDP_CHAN_ACTION;
1836 if (xflow_listen_mask & XFLOWL_SFLOW) {
1837 *listen_mask |= 1 << WDP_CHAN_SFLOW;
1844 wx_recv_set_mask(struct wdp *wdp, int listen_mask)
1846 struct wx *wx = wx_cast(wdp);
1847 int xflow_listen_mask;
1849 xflow_listen_mask = 0;
1850 if (listen_mask & (1 << WDP_CHAN_MISS)) {
1851 xflow_listen_mask |= XFLOWL_MISS;
1853 if (listen_mask & (1 << WDP_CHAN_ACTION)) {
1854 xflow_listen_mask |= XFLOWL_ACTION;
1856 if (listen_mask & (1 << WDP_CHAN_SFLOW)) {
1857 xflow_listen_mask |= XFLOWL_SFLOW;
1860 return xfif_recv_set_mask(wx->xfif, xflow_listen_mask);
1864 wx_get_sflow_probability(const struct wdp *wdp, uint32_t *probability)
1866 struct wx *wx = wx_cast(wdp);
1868 return xfif_get_sflow_probability(wx->xfif, probability);
1872 wx_set_sflow_probability(struct wdp *wdp, uint32_t probability)
1874 struct wx *wx = wx_cast(wdp);
1876 return xfif_set_sflow_probability(wx->xfif, probability);
1880 wx_translate_xflow_msg(struct xflow_msg *msg, struct ofpbuf *payload,
1881 struct wdp_packet *packet)
1883 packet->in_port = xflow_port_to_ofp_port(msg->port);
1884 packet->send_len = 0;
1887 switch (msg->type) {
1888 case _XFLOWL_MISS_NR:
1889 packet->channel = WDP_CHAN_MISS;
1890 packet->payload = payload;
1891 packet->tun_id = msg->arg;
1894 case _XFLOWL_ACTION_NR:
1895 packet->channel = WDP_CHAN_ACTION;
1896 packet->payload = payload;
1897 packet->send_len = msg->arg;
1900 case _XFLOWL_SFLOW_NR:
1902 ofpbuf_delete(payload);
1906 VLOG_WARN_RL(&rl, "received XFLOW message of unexpected type %"PRIu32,
1908 ofpbuf_delete(payload);
1913 static const uint8_t *
1914 get_local_mac(const struct wx *wx)
1916 const struct wdp_port *port = port_array_get(&wx->ports, XFLOWP_LOCAL);
1917 return port ? port->opp.hw_addr : NULL;
1920 /* Returns true if 'packet' is a DHCP reply to the local port. Such a reply
1921 * should be sent to the local port regardless of the flow table.
1923 * We only need this, strictly speaking, when in-band control is turned on. */
1925 wx_is_local_dhcp_reply(const struct wx *wx,
1926 const flow_t *flow, const struct ofpbuf *packet)
1928 if (flow->dl_type == htons(ETH_TYPE_IP)
1929 && flow->nw_proto == IP_TYPE_UDP
1930 && flow->tp_src == htons(DHCP_SERVER_PORT)
1931 && flow->tp_dst == htons(DHCP_CLIENT_PORT)
1934 const uint8_t *local_mac = get_local_mac(wx);
1935 struct dhcp_header *dhcp = ofpbuf_at(
1936 packet, (char *)packet->l7 - (char *)packet->data, sizeof *dhcp);
1937 return dhcp && local_mac && eth_addr_equals(dhcp->chaddr, local_mac);
1944 wx_explode_rule(struct wx *wx, struct xflow_msg *msg, struct ofpbuf *payload)
1946 struct wx_rule *rule;
1949 flow_extract(payload, 0, xflow_port_to_ofp_port(msg->port), &flow);
1951 if (wx_is_local_dhcp_reply(wx, &flow, payload)) {
1952 union xflow_action action;
1954 memset(&action, 0, sizeof(action));
1955 action.output.type = XFLOWAT_OUTPUT;
1956 action.output.port = XFLOWP_LOCAL;
1957 xfif_execute(wx->xfif, msg->port, &action, 1, payload);
1960 rule = wx_rule_lookup_valid(wx, &flow);
1965 if (rule->wr.cr.flow.wildcards) {
1966 rule = wx_rule_create_subrule(wx, rule, &flow);
1967 wx_rule_make_actions(wx, rule, payload);
1969 if (!rule->may_install) {
1970 /* The rule is not installable, that is, we need to process every
1971 * packet, so process the current packet and set its actions into
1973 wx_rule_make_actions(wx, rule, payload);
1975 /* XXX revalidate rule if it needs it */
1979 wx_rule_execute(wx, rule, payload, &flow);
1980 wx_rule_reinstall(wx, rule);
1986 wx_recv(struct wdp *wdp, struct wdp_packet *packet)
1988 struct wx *wx = wx_cast(wdp);
1991 /* XXX need to avoid 50*50 potential cost for caller. */
1992 for (i = 0; i < 50; i++) {
1993 struct xflow_msg *msg;
1997 error = xfif_recv(wx->xfif, &buf);
2002 msg = ofpbuf_pull(buf, sizeof *msg);
2003 if (msg->type != _XFLOWL_MISS_NR || !wx_explode_rule(wx, msg, buf)) {
2004 return wx_translate_xflow_msg(msg, buf, packet);
2012 wx_recv_purge_queue__(struct wx *wx, int max, int xflow_listen_mask,
2017 error = xfif_recv_set_mask(wx->xfif, xflow_listen_mask);
2021 while (max > 0 && (error = xfif_recv(wx->xfif, &buf)) == 0) {
2026 if (error && error != EAGAIN) {
2032 wx_recv_purge(struct wdp *wdp)
2034 struct wx *wx = wx_cast(wdp);
2035 struct xflow_stats xflow_stats;
2036 int xflow_listen_mask;
2039 xfif_get_xf_stats(wx->xfif, &xflow_stats);
2041 error = xfif_recv_get_mask(wx->xfif, &xflow_listen_mask);
2042 if (error || !(xflow_listen_mask & XFLOWL_ALL)) {
2046 if (xflow_listen_mask & XFLOWL_MISS) {
2047 wx_recv_purge_queue__(wx, xflow_stats.max_miss_queue, XFLOWL_MISS,
2050 if (xflow_listen_mask & XFLOWL_ACTION) {
2051 wx_recv_purge_queue__(wx, xflow_stats.max_action_queue, XFLOWL_ACTION,
2054 if (xflow_listen_mask & XFLOWL_SFLOW) {
2055 wx_recv_purge_queue__(wx, xflow_stats.max_sflow_queue, XFLOWL_SFLOW,
2059 retval = xfif_recv_set_mask(wx->xfif, xflow_listen_mask);
2060 return retval ? retval : error;
2065 wx_recv_wait(struct wdp *wdp)
2067 struct wx *wx = wx_cast(wdp);
2069 xfif_recv_wait(wx->xfif);
2073 wx_set_ofhooks(struct wdp *wdp, const struct ofhooks *ofhooks, void *aux)
2075 struct wx *wx = wx_cast(wdp);
2077 if (wx->ofhooks == &default_ofhooks) {
2078 mac_learning_destroy(wx->ml);
2082 wx->ofhooks = ofhooks;
2088 wx_revalidate(struct wdp *wdp, tag_type tag)
2090 struct wx *wx = wx_cast(wdp);
2092 tag_set_add(&wx->revalidate_set, tag);
2096 wx_revalidate_all(struct wdp *wdp)
2098 struct wx *wx = wx_cast(wdp);
2100 wx->revalidate_all = true;
2103 static void wx_port_update(struct wx *, const char *devname,
2104 wdp_port_poll_cb_func *cb, void *aux);
2105 static void wx_port_reinit(struct wx *, wdp_port_poll_cb_func *cb, void *aux);
2108 wx_port_process_change(struct wx *wx, int error, char *devname,
2109 wdp_port_poll_cb_func *cb, void *aux)
2111 if (error == ENOBUFS) {
2112 wx_port_reinit(wx, cb, aux);
2113 } else if (!error) {
2114 wx_port_update(wx, devname, cb, aux);
2120 wx_port_refresh_group(struct wx *wx, unsigned int group)
2124 struct wdp_port *port;
2125 unsigned int port_no;
2127 assert(group == WX_GROUP_ALL || group == WX_GROUP_FLOOD);
2129 ports = xmalloc(port_array_count(&wx->ports) * sizeof *ports);
2131 PORT_ARRAY_FOR_EACH (port, &wx->ports, port_no) {
2132 if (group == WX_GROUP_ALL || !(port->opp.config & OFPPC_NO_FLOOD)) {
2133 ports[n_ports++] = port_no;
2136 xfif_port_group_set(wx->xfif, group, ports, n_ports);
2143 wx_port_refresh_groups(struct wx *wx)
2145 wx_port_refresh_group(wx, WX_GROUP_FLOOD);
2146 wx_port_refresh_group(wx, WX_GROUP_ALL);
2150 wx_port_reinit(struct wx *wx, wdp_port_poll_cb_func *cb, void *aux)
2152 struct svec devnames;
2153 struct wdp_port *wdp_port;
2154 unsigned int port_no;
2155 struct xflow_port *xflow_ports;
2156 size_t n_xflow_ports;
2159 svec_init(&devnames);
2160 PORT_ARRAY_FOR_EACH (wdp_port, &wx->ports, port_no) {
2161 svec_add (&devnames, (char *) wdp_port->opp.name);
2163 xfif_port_list(wx->xfif, &xflow_ports, &n_xflow_ports);
2164 for (i = 0; i < n_xflow_ports; i++) {
2165 svec_add(&devnames, xflow_ports[i].devname);
2169 svec_sort_unique(&devnames);
2170 for (i = 0; i < devnames.n; i++) {
2171 wx_port_update(wx, devnames.names[i], cb, aux);
2173 svec_destroy(&devnames);
2175 wx_port_refresh_groups(wx);
2178 static struct wdp_port *
2179 make_wdp_port(const struct xflow_port *xflow_port)
2181 struct netdev_options netdev_options;
2182 enum netdev_flags flags;
2183 struct wdp_port *wdp_port;
2184 struct netdev *netdev;
2188 memset(&netdev_options, 0, sizeof netdev_options);
2189 netdev_options.name = xflow_port->devname;
2190 netdev_options.ethertype = NETDEV_ETH_TYPE_NONE;
2192 error = netdev_open(&netdev_options, &netdev);
2194 VLOG_WARN_RL(&rl, "ignoring port %s (%"PRIu16") because netdev %s "
2195 "cannot be opened (%s)",
2196 xflow_port->devname, xflow_port->port,
2197 xflow_port->devname, strerror(error));
2201 wdp_port = xmalloc(sizeof *wdp_port);
2202 wdp_port->netdev = netdev;
2203 wdp_port->opp.port_no = xflow_port_to_ofp_port(xflow_port->port);
2204 netdev_get_etheraddr(netdev, wdp_port->opp.hw_addr);
2205 strncpy((char *) wdp_port->opp.name, xflow_port->devname,
2206 sizeof wdp_port->opp.name);
2207 wdp_port->opp.name[sizeof wdp_port->opp.name - 1] = '\0';
2209 netdev_get_flags(netdev, &flags);
2210 wdp_port->opp.config = flags & NETDEV_UP ? 0 : OFPPC_PORT_DOWN;
2212 netdev_get_carrier(netdev, &carrier);
2213 wdp_port->opp.state = carrier ? 0 : OFPPS_LINK_DOWN;
2215 netdev_get_features(netdev,
2216 &wdp_port->opp.curr, &wdp_port->opp.advertised,
2217 &wdp_port->opp.supported, &wdp_port->opp.peer);
2219 wdp_port->devname = xstrdup(xflow_port->devname);
2220 wdp_port->internal = (xflow_port->flags & XFLOW_PORT_INTERNAL) != 0;
2225 wx_port_conflicts(const struct wx *wx, const struct xflow_port *xflow_port)
2227 if (port_array_get(&wx->ports, xflow_port->port)) {
2228 VLOG_WARN_RL(&rl, "ignoring duplicate port %"PRIu16" in datapath",
2231 } else if (shash_find(&wx->port_by_name, xflow_port->devname)) {
2232 VLOG_WARN_RL(&rl, "ignoring duplicate device %s in datapath",
2233 xflow_port->devname);
2241 wdp_port_equal(const struct wdp_port *a_, const struct wdp_port *b_)
2243 const struct ofp_phy_port *a = &a_->opp;
2244 const struct ofp_phy_port *b = &b_->opp;
2246 BUILD_ASSERT_DECL(sizeof *a == 48); /* Detect ofp_phy_port changes. */
2247 return (a->port_no == b->port_no
2248 && !memcmp(a->hw_addr, b->hw_addr, sizeof a->hw_addr)
2249 && !strcmp((char *) a->name, (char *) b->name)
2250 && a->state == b->state
2251 && a->config == b->config
2252 && a->curr == b->curr
2253 && a->advertised == b->advertised
2254 && a->supported == b->supported
2255 && a->peer == b->peer);
2259 wx_port_install(struct wx *wx, struct wdp_port *wdp_port)
2261 uint16_t xflow_port = ofp_port_to_xflow_port(wdp_port->opp.port_no);
2262 const char *netdev_name = (const char *) wdp_port->opp.name;
2264 netdev_monitor_add(wx->netdev_monitor, wdp_port->netdev);
2265 port_array_set(&wx->ports, xflow_port, wdp_port);
2266 shash_add(&wx->port_by_name, netdev_name, wdp_port);
2270 wx_port_remove(struct wx *wx, struct wdp_port *wdp_port)
2272 uint16_t xflow_port = ofp_port_to_xflow_port(wdp_port->opp.port_no);
2274 netdev_monitor_remove(wx->netdev_monitor, wdp_port->netdev);
2275 port_array_delete(&wx->ports, xflow_port);
2276 shash_delete(&wx->port_by_name,
2277 shash_find(&wx->port_by_name, (char *) wdp_port->opp.name));
2281 wx_port_free(struct wdp_port *wdp_port)
2284 netdev_close(wdp_port->netdev);
2290 wx_port_update(struct wx *wx, const char *devname,
2291 wdp_port_poll_cb_func *cb, void *aux)
2293 struct xflow_port xflow_port;
2294 struct wdp_port *old_wdp_port;
2295 struct wdp_port *new_wdp_port;
2298 COVERAGE_INC(wx_update_port);
2300 /* Query the datapath for port information. */
2301 error = xfif_port_query_by_name(wx->xfif, devname, &xflow_port);
2303 /* Find the old wdp_port. */
2304 old_wdp_port = shash_find_data(&wx->port_by_name, devname);
2306 if (!old_wdp_port) {
2307 /* There's no port named 'devname' but there might be a port with
2308 * the same port number. This could happen if a port is deleted
2309 * and then a new one added in its place very quickly, or if a port
2310 * is renamed. In the former case we want to send an OFPPR_DELETE
2311 * and an OFPPR_ADD, and in the latter case we want to send a
2312 * single OFPPR_MODIFY. We can distinguish the cases by comparing
2313 * the old port's ifindex against the new port, or perhaps less
2314 * reliably but more portably by comparing the old port's MAC
2315 * against the new port's MAC. However, this code isn't that smart
2316 * and always sends an OFPPR_MODIFY (XXX). */
2317 old_wdp_port = port_array_get(&wx->ports, xflow_port.port);
2319 } else if (error != ENOENT && error != ENODEV) {
2320 VLOG_WARN_RL(&rl, "xfif_port_query_by_name returned unexpected error "
2321 "%s", strerror(error));
2325 /* Create a new wdp_port. */
2326 new_wdp_port = !error ? make_wdp_port(&xflow_port) : NULL;
2328 /* Eliminate a few pathological cases. */
2329 if (!old_wdp_port && !new_wdp_port) {
2331 } else if (old_wdp_port && new_wdp_port) {
2332 /* Most of the 'config' bits are OpenFlow soft state, but
2333 * OFPPC_PORT_DOWN is maintained by the kernel. So transfer the
2334 * OpenFlow bits from old_wdp_port. (make_wdp_port() only sets
2335 * OFPPC_PORT_DOWN and leaves the other bits 0.) */
2336 new_wdp_port->opp.config |= old_wdp_port->opp.config & ~OFPPC_PORT_DOWN;
2338 if (wdp_port_equal(old_wdp_port, new_wdp_port)) {
2339 /* False alarm--no change. */
2340 wx_port_free(new_wdp_port);
2345 /* Now deal with the normal cases. */
2347 wx_port_remove(wx, old_wdp_port);
2350 wx_port_install(wx, new_wdp_port);
2354 if (!old_wdp_port) {
2355 (*cb)(&new_wdp_port->opp, OFPPR_ADD, aux);
2356 } else if (!new_wdp_port) {
2357 (*cb)(&old_wdp_port->opp, OFPPR_DELETE, aux);
2359 (*cb)(&new_wdp_port->opp, OFPPR_MODIFY, aux);
2362 /* Update port groups. */
2363 wx_port_refresh_groups(wx);
2366 wx_port_free(old_wdp_port);
2370 wx_port_init(struct wx *wx)
2372 struct xflow_port *ports;
2377 error = xfif_port_list(wx->xfif, &ports, &n_ports);
2382 for (i = 0; i < n_ports; i++) {
2383 const struct xflow_port *xflow_port = &ports[i];
2384 if (!wx_port_conflicts(wx, xflow_port)) {
2385 struct wdp_port *wdp_port = make_wdp_port(xflow_port);
2387 wx_port_install(wx, wdp_port);
2392 wx_port_refresh_groups(wx);
2397 wdp_xflow_register(void)
2399 static const struct wdp_class wdp_xflow_class = {
2415 wx_port_query_by_number,
2416 wx_port_query_by_name,
2423 wx_flow_for_each_match,
2433 wx_get_sflow_probability,
2434 wx_set_sflow_probability,
2443 static bool inited = false;
2456 xf_enumerate_types(&types);
2459 SVEC_FOR_EACH (i, type, &types) {
2460 struct wdp_class *class;
2462 class = xmalloc(sizeof *class);
2463 *class = wdp_xflow_class;
2464 class->type = xstrdup(type);
2469 if (!wdp_register_provider(class)) {
2474 svec_destroy(&types);
2478 default_normal_ofhook_cb(const flow_t *flow, const struct ofpbuf *packet,
2479 struct xflow_actions *actions, tag_type *tags,
2480 uint16_t *nf_output_iface, void *wx_)
2482 struct wx *wx = wx_;
2485 /* Drop frames for reserved multicast addresses. */
2486 if (eth_addr_is_reserved(flow->dl_dst)) {
2490 /* Learn source MAC (but don't try to learn from revalidation). */
2491 if (packet != NULL) {
2492 tag_type rev_tag = mac_learning_learn(wx->ml, flow->dl_src,
2494 GRAT_ARP_LOCK_NONE);
2496 /* The log messages here could actually be useful in debugging,
2497 * so keep the rate limit relatively high. */
2498 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
2499 VLOG_DBG_RL(&rl, "learned that "ETH_ADDR_FMT" is on port %"PRIu16,
2500 ETH_ADDR_ARGS(flow->dl_src), flow->in_port);
2501 tag_set_add(&wx->revalidate_set, rev_tag);
2505 /* Determine output port. */
2506 out_port = mac_learning_lookup_tag(wx->ml, flow->dl_dst, 0, tags,
2509 add_output_group_action(actions, WX_GROUP_FLOOD, nf_output_iface);
2510 } else if (out_port != flow->in_port) {
2511 xflow_actions_add(actions, XFLOWAT_OUTPUT)->output.port = out_port;
2512 *nf_output_iface = out_port;
2520 static const struct ofhooks default_ofhooks = {
2522 default_normal_ofhook_cb,