2 * Copyright (c) 2009, 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.
25 #include <netinet/in.h>
26 #include <sys/socket.h>
30 #include <sys/ioctl.h>
39 #include "xflow-util.h"
40 #include "ofp-print.h"
43 #include "poll-loop.h"
48 #include "xfif-provider.h"
50 VLOG_DEFINE_THIS_MODULE(xfif_netdev)
52 /* Configuration parameters. */
53 enum { N_QUEUES = 2 }; /* Number of queues for xfif_recv(). */
54 enum { MAX_QUEUE_LEN = 100 }; /* Maximum number of packets per queue. */
55 enum { N_GROUPS = 16 }; /* Number of port groups. */
56 enum { MAX_PORTS = 256 }; /* Maximum number of ports. */
57 enum { MAX_FLOWS = 65536 }; /* Maximum number of flows in flow table. */
59 /* Enough headroom to add a vlan tag, plus an extra 2 bytes to allow IP
60 * headers to be aligned on a 4-byte boundary. */
61 enum { XF_NETDEV_HEADROOM = 2 + VLAN_HEADER_LEN };
63 /* Datapath based on the network device interface from netdev.h. */
70 bool drop_frags; /* Drop all IP fragments, if true. */
71 struct ovs_queue queues[N_QUEUES]; /* Messages queued for xfif_recv(). */
72 struct hmap flow_table; /* Flow table. */
73 struct xflow_port_group groups[N_GROUPS];
76 long long int n_frags; /* Number of dropped IP fragments. */
77 long long int n_hit; /* Number of flow table matches. */
78 long long int n_missed; /* Number of flow table misses. */
79 long long int n_lost; /* Number of misses not passed to client. */
83 struct xf_netdev_port *ports[MAX_PORTS];
84 struct list port_list;
88 /* A port in a netdev-based datapath. */
89 struct xf_netdev_port {
90 int port_no; /* Index into xf_netdev's 'ports'. */
91 struct list node; /* Element in xf_netdev's 'port_list'. */
92 struct netdev *netdev;
93 bool internal; /* Internal port (as XFLOW_PORT_INTERNAL)? */
96 /* A flow in xf_netdev's 'flow_table'. */
97 struct xf_netdev_flow {
98 struct hmap_node node; /* Element in xf_netdev's 'flow_table'. */
102 struct timespec used; /* Last used time. */
103 long long int packet_count; /* Number of packets matched. */
104 long long int byte_count; /* Number of bytes matched. */
105 uint8_t ip_tos; /* IP TOS value. */
106 uint16_t tcp_ctl; /* Bitwise-OR of seen tcp_ctl values. */
109 union xflow_action *actions;
110 unsigned int n_actions;
113 /* Interface to netdev-based datapath. */
116 struct xf_netdev *xf;
118 unsigned int xf_serial;
121 /* All netdev-based datapaths. */
122 static struct xf_netdev *xf_netdevs[256];
123 struct list xf_netdev_list = LIST_INITIALIZER(&xf_netdev_list);
124 enum { N_XF_NETDEVS = ARRAY_SIZE(xf_netdevs) };
126 /* Maximum port MTU seen so far. */
127 static int max_mtu = ETH_PAYLOAD_MAX;
129 static int get_port_by_number(struct xf_netdev *, uint16_t port_no,
130 struct xf_netdev_port **portp);
131 static int get_port_by_name(struct xf_netdev *, const char *devname,
132 struct xf_netdev_port **portp);
133 static void xf_netdev_free(struct xf_netdev *);
134 static void xf_netdev_flow_flush(struct xf_netdev *);
135 static int do_add_port(struct xf_netdev *, const char *devname, uint16_t flags,
137 static int do_del_port(struct xf_netdev *, uint16_t port_no);
138 static int xf_netdev_output_control(struct xf_netdev *, const struct ofpbuf *,
139 int queue_no, int port_no, uint32_t arg);
140 static int xf_netdev_execute_actions(struct xf_netdev *,
141 struct ofpbuf *, struct xflow_key *,
142 const union xflow_action *, int n);
144 static struct xfif_netdev *
145 xfif_netdev_cast(const struct xfif *xfif)
147 xfif_assert_class(xfif, &xfif_netdev_class);
148 return CONTAINER_OF(xfif, struct xfif_netdev, xfif);
151 static struct xf_netdev *
152 get_xf_netdev(const struct xfif *xfif)
154 return xfif_netdev_cast(xfif)->xf;
158 name_to_xf_idx(const char *name)
160 if (!strncmp(name, "xf", 2) && isdigit((unsigned char)name[2])) {
161 int xf_idx = atoi(name + 2);
162 if (xf_idx >= 0 && xf_idx < N_XF_NETDEVS) {
169 static struct xf_netdev *
170 find_xf_netdev(const char *name)
175 xf_idx = name_to_xf_idx(name);
177 return xf_netdevs[xf_idx];
180 for (i = 0; i < N_XF_NETDEVS; i++) {
181 struct xf_netdev *xf = xf_netdevs[i];
183 struct xf_netdev_port *port;
184 if (!get_port_by_name(xf, name, &port)) {
193 create_xfif_netdev(struct xf_netdev *xf)
195 struct xfif_netdev *xfif;
200 xfname = xasprintf("xf%d", xf->xf_idx);
201 xfif = xmalloc(sizeof *xfif);
202 xfif_init(&xfif->xfif, &xfif_netdev_class, xfname, xf->xf_idx, xf->xf_idx);
204 xfif->listen_mask = 0;
205 xfif->xf_serial = xf->serial;
212 create_xf_netdev(const char *name, int xf_idx, struct xfif **xfifp)
214 struct xf_netdev *xf;
218 if (xf_netdevs[xf_idx]) {
222 /* Create datapath. */
223 xf_netdevs[xf_idx] = xf = xzalloc(sizeof *xf);
224 list_push_back(&xf_netdev_list, &xf->node);
227 xf->drop_frags = false;
228 for (i = 0; i < N_QUEUES; i++) {
229 queue_init(&xf->queues[i]);
231 hmap_init(&xf->flow_table);
232 for (i = 0; i < N_GROUPS; i++) {
233 xf->groups[i].ports = NULL;
234 xf->groups[i].n_ports = 0;
235 xf->groups[i].group = i;
237 list_init(&xf->port_list);
238 error = do_add_port(xf, name, XFLOW_PORT_INTERNAL, XFLOWP_LOCAL);
244 *xfifp = create_xfif_netdev(xf);
249 xfif_netdev_open(const char *name, const char *type OVS_UNUSED, bool create,
253 if (find_xf_netdev(name)) {
256 int xf_idx = name_to_xf_idx(name);
258 return create_xf_netdev(name, xf_idx, xfifp);
260 /* Scan for unused xf_idx number. */
261 for (xf_idx = 0; xf_idx < N_XF_NETDEVS; xf_idx++) {
262 int error = create_xf_netdev(name, xf_idx, xfifp);
263 if (error != EBUSY) {
268 /* All datapath numbers in use. */
273 struct xf_netdev *xf = find_xf_netdev(name);
275 *xfifp = create_xfif_netdev(xf);
284 xf_netdev_free(struct xf_netdev *xf)
288 xf_netdev_flow_flush(xf);
289 while (xf->n_ports > 0) {
290 struct xf_netdev_port *port = CONTAINER_OF(
291 xf->port_list.next, struct xf_netdev_port, node);
292 do_del_port(xf, port->port_no);
294 for (i = 0; i < N_QUEUES; i++) {
295 queue_destroy(&xf->queues[i]);
297 hmap_destroy(&xf->flow_table);
298 for (i = 0; i < N_GROUPS; i++) {
299 free(xf->groups[i].ports);
301 xf_netdevs[xf->xf_idx] = NULL;
302 list_remove(&xf->node);
307 xfif_netdev_close(struct xfif *xfif)
309 struct xf_netdev *xf = get_xf_netdev(xfif);
310 assert(xf->open_cnt > 0);
311 if (--xf->open_cnt == 0 && xf->destroyed) {
318 xfif_netdev_destroy(struct xfif *xfif)
320 struct xf_netdev *xf = get_xf_netdev(xfif);
321 xf->destroyed = true;
326 xfif_netdev_get_stats(const struct xfif *xfif, struct xflow_stats *stats)
328 struct xf_netdev *xf = get_xf_netdev(xfif);
329 memset(stats, 0, sizeof *stats);
330 stats->n_flows = hmap_count(&xf->flow_table);
331 stats->cur_capacity = hmap_capacity(&xf->flow_table);
332 stats->max_capacity = MAX_FLOWS;
333 stats->n_ports = xf->n_ports;
334 stats->max_ports = MAX_PORTS;
335 stats->max_groups = N_GROUPS;
336 stats->n_frags = xf->n_frags;
337 stats->n_hit = xf->n_hit;
338 stats->n_missed = xf->n_missed;
339 stats->n_lost = xf->n_lost;
340 stats->max_miss_queue = MAX_QUEUE_LEN;
341 stats->max_action_queue = MAX_QUEUE_LEN;
346 xfif_netdev_get_drop_frags(const struct xfif *xfif, bool *drop_fragsp)
348 struct xf_netdev *xf = get_xf_netdev(xfif);
349 *drop_fragsp = xf->drop_frags;
354 xfif_netdev_set_drop_frags(struct xfif *xfif, bool drop_frags)
356 struct xf_netdev *xf = get_xf_netdev(xfif);
357 xf->drop_frags = drop_frags;
362 do_add_port(struct xf_netdev *xf, const char *devname, uint16_t flags,
365 bool internal = (flags & XFLOW_PORT_INTERNAL) != 0;
366 struct xf_netdev_port *port;
367 struct netdev_options netdev_options;
368 struct netdev *netdev;
372 /* XXX reject devices already in some xf_netdev. */
374 /* Open and validate network device. */
375 memset(&netdev_options, 0, sizeof netdev_options);
376 netdev_options.name = devname;
377 netdev_options.ethertype = NETDEV_ETH_TYPE_ANY;
379 netdev_options.type = "tap";
382 error = netdev_open(&netdev_options, &netdev);
386 /* XXX reject loopback devices */
387 /* XXX reject non-Ethernet devices */
389 error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, false);
391 netdev_close(netdev);
395 port = xmalloc(sizeof *port);
396 port->port_no = port_no;
397 port->netdev = netdev;
398 port->internal = internal;
400 netdev_get_mtu(netdev, &mtu);
405 list_push_back(&xf->port_list, &port->node);
406 xf->ports[port_no] = port;
414 xfif_netdev_port_add(struct xfif *xfif, const char *devname, uint16_t flags,
417 struct xf_netdev *xf = get_xf_netdev(xfif);
420 for (port_no = 0; port_no < MAX_PORTS; port_no++) {
421 if (!xf->ports[port_no]) {
423 return do_add_port(xf, devname, flags, port_no);
430 xfif_netdev_port_del(struct xfif *xfif, uint16_t port_no)
432 struct xf_netdev *xf = get_xf_netdev(xfif);
433 return port_no == XFLOWP_LOCAL ? EINVAL : do_del_port(xf, port_no);
437 is_valid_port_number(uint16_t port_no)
439 return port_no < MAX_PORTS;
443 get_port_by_number(struct xf_netdev *xf,
444 uint16_t port_no, struct xf_netdev_port **portp)
446 if (!is_valid_port_number(port_no)) {
450 *portp = xf->ports[port_no];
451 return *portp ? 0 : ENOENT;
456 get_port_by_name(struct xf_netdev *xf,
457 const char *devname, struct xf_netdev_port **portp)
459 struct xf_netdev_port *port;
461 LIST_FOR_EACH (port, struct xf_netdev_port, node, &xf->port_list) {
462 if (!strcmp(netdev_get_name(port->netdev), devname)) {
471 do_del_port(struct xf_netdev *xf, uint16_t port_no)
473 struct xf_netdev_port *port;
477 error = get_port_by_number(xf, port_no, &port);
482 list_remove(&port->node);
483 xf->ports[port->port_no] = NULL;
487 name = xstrdup(netdev_get_name(port->netdev));
488 netdev_close(port->netdev);
497 answer_port_query(const struct xf_netdev_port *port, struct xflow_port *xflow_port)
499 memset(xflow_port, 0, sizeof *xflow_port);
500 ovs_strlcpy(xflow_port->devname, netdev_get_name(port->netdev),
501 sizeof xflow_port->devname);
502 xflow_port->port = port->port_no;
503 xflow_port->flags = port->internal ? XFLOW_PORT_INTERNAL : 0;
507 xfif_netdev_port_query_by_number(const struct xfif *xfif, uint16_t port_no,
508 struct xflow_port *xflow_port)
510 struct xf_netdev *xf = get_xf_netdev(xfif);
511 struct xf_netdev_port *port;
514 error = get_port_by_number(xf, port_no, &port);
516 answer_port_query(port, xflow_port);
522 xfif_netdev_port_query_by_name(const struct xfif *xfif, const char *devname,
523 struct xflow_port *xflow_port)
525 struct xf_netdev *xf = get_xf_netdev(xfif);
526 struct xf_netdev_port *port;
529 error = get_port_by_name(xf, devname, &port);
531 answer_port_query(port, xflow_port);
537 xf_netdev_free_flow(struct xf_netdev *xf, struct xf_netdev_flow *flow)
539 hmap_remove(&xf->flow_table, &flow->node);
545 xf_netdev_flow_flush(struct xf_netdev *xf)
547 struct xf_netdev_flow *flow, *next;
549 HMAP_FOR_EACH_SAFE (flow, next, struct xf_netdev_flow, node,
551 xf_netdev_free_flow(xf, flow);
556 xfif_netdev_flow_flush(struct xfif *xfif)
558 struct xf_netdev *xf = get_xf_netdev(xfif);
559 xf_netdev_flow_flush(xf);
564 xfif_netdev_port_list(const struct xfif *xfif, struct xflow_port *ports, int n)
566 struct xf_netdev *xf = get_xf_netdev(xfif);
567 struct xf_netdev_port *port;
571 LIST_FOR_EACH (port, struct xf_netdev_port, node, &xf->port_list) {
572 struct xflow_port *xflow_port = &ports[i];
576 answer_port_query(port, xflow_port);
583 xfif_netdev_port_poll(const struct xfif *xfif_, char **devnamep OVS_UNUSED)
585 struct xfif_netdev *xfif = xfif_netdev_cast(xfif_);
586 if (xfif->xf_serial != xfif->xf->serial) {
587 xfif->xf_serial = xfif->xf->serial;
595 xfif_netdev_port_poll_wait(const struct xfif *xfif_)
597 struct xfif_netdev *xfif = xfif_netdev_cast(xfif_);
598 if (xfif->xf_serial != xfif->xf->serial) {
599 poll_immediate_wake();
604 get_port_group(const struct xfif *xfif, int group_no,
605 struct xflow_port_group **groupp)
607 struct xf_netdev *xf = get_xf_netdev(xfif);
609 if (group_no >= 0 && group_no < N_GROUPS) {
610 *groupp = &xf->groups[group_no];
619 xfif_netdev_port_group_get(const struct xfif *xfif, int group_no,
620 uint16_t ports[], int n)
622 struct xflow_port_group *group;
629 error = get_port_group(xfif, group_no, &group);
631 memcpy(ports, group->ports, MIN(n, group->n_ports) * sizeof *ports);
632 return group->n_ports;
639 xfif_netdev_port_group_set(struct xfif *xfif, int group_no,
640 const uint16_t ports[], int n)
642 struct xflow_port_group *group;
645 if (n < 0 || n > MAX_PORTS) {
649 error = get_port_group(xfif, group_no, &group);
652 group->ports = xmemdup(ports, n * sizeof *group->ports);
654 group->group = group_no;
659 static struct xf_netdev_flow *
660 xf_netdev_lookup_flow(const struct xf_netdev *xf,
661 const struct xflow_key *key)
663 struct xf_netdev_flow *flow;
665 HMAP_FOR_EACH_WITH_HASH (flow, struct xf_netdev_flow, node,
666 xflow_key_hash(key, 0), &xf->flow_table) {
667 if (xflow_key_equal(&flow->key, key)) {
675 answer_flow_query(struct xf_netdev_flow *flow, uint32_t query_flags,
676 struct xflow_flow *xflow_flow)
679 xflow_flow->key = flow->key;
680 xflow_flow->stats.n_packets = flow->packet_count;
681 xflow_flow->stats.n_bytes = flow->byte_count;
682 xflow_flow->stats.used_sec = flow->used.tv_sec;
683 xflow_flow->stats.used_nsec = flow->used.tv_nsec;
684 xflow_flow->stats.tcp_flags = TCP_FLAGS(flow->tcp_ctl);
685 xflow_flow->stats.ip_tos = flow->ip_tos;
686 xflow_flow->stats.error = 0;
687 if (xflow_flow->n_actions > 0) {
688 unsigned int n = MIN(xflow_flow->n_actions, flow->n_actions);
689 memcpy(xflow_flow->actions, flow->actions,
690 n * sizeof *xflow_flow->actions);
691 xflow_flow->n_actions = flow->n_actions;
694 if (query_flags & XFLOWFF_ZERO_TCP_FLAGS) {
699 xflow_flow->stats.error = ENOENT;
704 xfif_netdev_flow_get(const struct xfif *xfif, struct xflow_flow flows[], int n)
706 struct xf_netdev *xf = get_xf_netdev(xfif);
709 for (i = 0; i < n; i++) {
710 struct xflow_flow *xflow_flow = &flows[i];
711 answer_flow_query(xf_netdev_lookup_flow(xf, &xflow_flow->key),
712 xflow_flow->flags, xflow_flow);
718 xfif_netdev_validate_actions(const union xflow_action *actions, int n_actions,
724 for (i = 0; i < n_actions; i++) {
725 const union xflow_action *a = &actions[i];
728 if (a->output.port >= MAX_PORTS) {
733 case XFLOWAT_OUTPUT_GROUP:
735 if (a->output_group.group >= N_GROUPS) {
740 case XFLOWAT_CONTROLLER:
743 case XFLOWAT_SET_DL_TCI:
745 if (a->dl_tci.mask != htons(VLAN_VID_MASK)
746 && a->dl_tci.mask != htons(VLAN_PCP_MASK)
747 && a->dl_tci.mask != htons(VLAN_VID_MASK | VLAN_PCP_MASK)) {
750 if (a->dl_tci.tci & ~a->dl_tci.mask){
755 case XFLOWAT_SET_NW_TOS:
757 if (a->nw_tos.nw_tos & IP_ECN_MASK) {
762 case XFLOWAT_STRIP_VLAN:
763 case XFLOWAT_SET_DL_SRC:
764 case XFLOWAT_SET_DL_DST:
765 case XFLOWAT_SET_NW_SRC:
766 case XFLOWAT_SET_NW_DST:
767 case XFLOWAT_SET_TP_SRC:
768 case XFLOWAT_SET_TP_DST:
780 set_flow_actions(struct xf_netdev_flow *flow, struct xflow_flow *xflow_flow)
786 if (xflow_flow->n_actions >= 4096 / sizeof *xflow_flow->actions) {
789 error = xfif_netdev_validate_actions(xflow_flow->actions,
790 xflow_flow->n_actions, &mutates);
795 n_bytes = xflow_flow->n_actions * sizeof *flow->actions;
796 flow->actions = xrealloc(flow->actions, n_bytes);
797 flow->n_actions = xflow_flow->n_actions;
798 memcpy(flow->actions, xflow_flow->actions, n_bytes);
803 add_flow(struct xfif *xfif, struct xflow_flow *xflow_flow)
805 struct xf_netdev *xf = get_xf_netdev(xfif);
806 struct xf_netdev_flow *flow;
809 flow = xzalloc(sizeof *flow);
810 flow->key = xflow_flow->key;
812 error = set_flow_actions(flow, xflow_flow);
818 hmap_insert(&xf->flow_table, &flow->node,
819 xflow_key_hash(&flow->key, 0));
824 clear_stats(struct xf_netdev_flow *flow)
826 flow->used.tv_sec = 0;
827 flow->used.tv_nsec = 0;
828 flow->packet_count = 0;
829 flow->byte_count = 0;
835 xfif_netdev_flow_put(struct xfif *xfif, struct xflow_flow_put *put)
837 struct xf_netdev *xf = get_xf_netdev(xfif);
838 struct xf_netdev_flow *flow;
840 flow = xf_netdev_lookup_flow(xf, &put->flow.key);
842 if (put->flags & XFLOWPF_CREATE) {
843 if (hmap_count(&xf->flow_table) < MAX_FLOWS) {
844 return add_flow(xfif, &put->flow);
852 if (put->flags & XFLOWPF_MODIFY) {
853 int error = set_flow_actions(flow, &put->flow);
854 if (!error && put->flags & XFLOWPF_ZERO_STATS) {
866 xfif_netdev_flow_del(struct xfif *xfif, struct xflow_flow *xflow_flow)
868 struct xf_netdev *xf = get_xf_netdev(xfif);
869 struct xf_netdev_flow *flow;
871 flow = xf_netdev_lookup_flow(xf, &xflow_flow->key);
873 answer_flow_query(flow, 0, xflow_flow);
874 xf_netdev_free_flow(xf, flow);
882 xfif_netdev_flow_list(const struct xfif *xfif, struct xflow_flow flows[], int n)
884 struct xf_netdev *xf = get_xf_netdev(xfif);
885 struct xf_netdev_flow *flow;
889 HMAP_FOR_EACH (flow, struct xf_netdev_flow, node, &xf->flow_table) {
893 answer_flow_query(flow, 0, &flows[i++]);
895 return hmap_count(&xf->flow_table);
899 xfif_netdev_execute(struct xfif *xfif, uint16_t in_port,
900 const union xflow_action actions[], int n_actions,
901 const struct ofpbuf *packet)
903 struct xf_netdev *xf = get_xf_netdev(xfif);
906 struct xflow_key key;
910 if (packet->size < ETH_HEADER_LEN || packet->size > UINT16_MAX) {
914 error = xfif_netdev_validate_actions(actions, n_actions, &mutates);
920 /* We need a deep copy of 'packet' since we're going to modify its
922 ofpbuf_init(©, XF_NETDEV_HEADROOM + packet->size);
923 copy.data = (char*)copy.base + XF_NETDEV_HEADROOM;
924 ofpbuf_put(©, packet->data, packet->size);
926 /* We still need a shallow copy of 'packet', even though we won't
927 * modify its data, because flow_extract() modifies packet->l2, etc.
928 * We could probably get away with modifying those but it's more polite
932 flow_extract(©, 0, in_port, &flow);
933 xflow_key_from_flow(&key, &flow);
934 error = xf_netdev_execute_actions(xf, ©, &key, actions, n_actions);
936 ofpbuf_uninit(©);
942 xfif_netdev_recv_get_mask(const struct xfif *xfif, int *listen_mask)
944 struct xfif_netdev *xfif_netdev = xfif_netdev_cast(xfif);
945 *listen_mask = xfif_netdev->listen_mask;
950 xfif_netdev_recv_set_mask(struct xfif *xfif, int listen_mask)
952 struct xfif_netdev *xfif_netdev = xfif_netdev_cast(xfif);
953 if (!(listen_mask & ~XFLOWL_ALL)) {
954 xfif_netdev->listen_mask = listen_mask;
961 static struct ovs_queue *
962 find_nonempty_queue(struct xfif *xfif)
964 struct xfif_netdev *xfif_netdev = xfif_netdev_cast(xfif);
965 struct xf_netdev *xf = get_xf_netdev(xfif);
966 int mask = xfif_netdev->listen_mask;
969 for (i = 0; i < N_QUEUES; i++) {
970 struct ovs_queue *q = &xf->queues[i];
971 if (q->n && mask & (1u << i)) {
979 xfif_netdev_recv(struct xfif *xfif, struct ofpbuf **bufp)
981 struct ovs_queue *q = find_nonempty_queue(xfif);
983 *bufp = queue_pop_head(q);
991 xfif_netdev_recv_wait(struct xfif *xfif)
993 struct ovs_queue *q = find_nonempty_queue(xfif);
995 poll_immediate_wake();
997 /* No messages ready to be received, and xf_wait() will ensure that we
998 * wake up to queue new messages, so there is nothing to do. */
1003 xf_netdev_flow_used(struct xf_netdev_flow *flow,
1004 const struct xflow_key *key,
1005 const struct ofpbuf *packet)
1007 time_timespec(&flow->used);
1008 flow->packet_count++;
1009 flow->byte_count += packet->size;
1010 if (key->dl_type == htons(ETH_TYPE_IP)) {
1011 struct ip_header *nh = packet->l3;
1012 flow->ip_tos = nh->ip_tos;
1014 if (key->nw_proto == IPPROTO_TCP) {
1015 struct tcp_header *th = packet->l4;
1016 flow->tcp_ctl |= th->tcp_ctl;
1022 xf_netdev_port_input(struct xf_netdev *xf, struct xf_netdev_port *port,
1023 struct ofpbuf *packet)
1025 struct xf_netdev_flow *flow;
1026 struct xflow_key key;
1029 if (flow_extract(packet, 0, port->port_no, &f) && xf->drop_frags) {
1033 xflow_key_from_flow(&key, &f);
1035 flow = xf_netdev_lookup_flow(xf, &key);
1037 xf_netdev_flow_used(flow, &key, packet);
1038 xf_netdev_execute_actions(xf, packet, &key,
1039 flow->actions, flow->n_actions);
1043 xf_netdev_output_control(xf, packet, _XFLOWL_MISS_NR, port->port_no, 0);
1050 struct ofpbuf packet;
1051 struct xf_netdev *xf;
1053 ofpbuf_init(&packet, XF_NETDEV_HEADROOM + max_mtu);
1054 LIST_FOR_EACH (xf, struct xf_netdev, node, &xf_netdev_list) {
1055 struct xf_netdev_port *port;
1057 LIST_FOR_EACH (port, struct xf_netdev_port, node, &xf->port_list) {
1060 /* Reset packet contents. */
1061 packet.data = (char*)packet.base + XF_NETDEV_HEADROOM;
1064 error = netdev_recv(port->netdev, &packet);
1066 xf_netdev_port_input(xf, port, &packet);
1067 } else if (error != EAGAIN) {
1068 struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1069 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1070 netdev_get_name(port->netdev), strerror(error));
1074 ofpbuf_uninit(&packet);
1078 xf_netdev_wait(void)
1080 struct xf_netdev *xf;
1082 LIST_FOR_EACH (xf, struct xf_netdev, node, &xf_netdev_list) {
1083 struct xf_netdev_port *port;
1084 LIST_FOR_EACH (port, struct xf_netdev_port, node, &xf->port_list) {
1085 netdev_recv_wait(port->netdev);
1091 /* Modify or add a 802.1Q header in 'packet' according to 'a'. */
1093 xf_netdev_set_dl_tci(struct ofpbuf *packet, struct xflow_key *key,
1094 const struct xflow_action_dl_tci *a)
1096 struct vlan_eth_header *veh;
1100 veh->veth_tci = (veh->veth_tci & ~a->mask) | a->tci;
1102 /* Insert new 802.1Q header. */
1103 struct eth_header *eh = packet->l2;
1104 struct vlan_eth_header tmp;
1105 memcpy(tmp.veth_dst, eh->eth_dst, ETH_ADDR_LEN);
1106 memcpy(tmp.veth_src, eh->eth_src, ETH_ADDR_LEN);
1107 tmp.veth_type = htons(ETH_TYPE_VLAN);
1108 tmp.veth_tci = htons(a->tci);
1109 tmp.veth_next_type = eh->eth_type;
1111 veh = ofpbuf_push_uninit(packet, VLAN_HEADER_LEN);
1112 memcpy(veh, &tmp, sizeof tmp);
1113 packet->l2 = (char*)packet->l2 - VLAN_HEADER_LEN;
1116 key->dl_tci = veh->veth_tci | htons(XFLOW_TCI_PRESENT);
1120 xf_netdev_strip_vlan(struct ofpbuf *packet, struct xflow_key *key)
1122 struct vlan_eth_header *veh = packet->l2;
1123 if (veh->veth_type == htons(ETH_TYPE_VLAN)) {
1124 struct eth_header tmp;
1126 memcpy(tmp.eth_dst, veh->veth_dst, ETH_ADDR_LEN);
1127 memcpy(tmp.eth_src, veh->veth_src, ETH_ADDR_LEN);
1128 tmp.eth_type = veh->veth_next_type;
1130 packet->size -= VLAN_HEADER_LEN;
1131 packet->data = (char*)packet->data + VLAN_HEADER_LEN;
1132 packet->l2 = (char*)packet->l2 + VLAN_HEADER_LEN;
1133 memcpy(packet->data, &tmp, sizeof tmp);
1135 key->dl_tci = htons(0);
1140 xf_netdev_set_dl_src(struct ofpbuf *packet,
1141 const uint8_t dl_addr[ETH_ADDR_LEN])
1143 struct eth_header *eh = packet->l2;
1144 memcpy(eh->eth_src, dl_addr, sizeof eh->eth_src);
1148 xf_netdev_set_dl_dst(struct ofpbuf *packet,
1149 const uint8_t dl_addr[ETH_ADDR_LEN])
1151 struct eth_header *eh = packet->l2;
1152 memcpy(eh->eth_dst, dl_addr, sizeof eh->eth_dst);
1156 xf_netdev_set_nw_addr(struct ofpbuf *packet, const struct xflow_key *key,
1157 const struct xflow_action_nw_addr *a)
1159 if (key->dl_type == htons(ETH_TYPE_IP)) {
1160 struct ip_header *nh = packet->l3;
1163 field = a->type == XFLOWAT_SET_NW_SRC ? &nh->ip_src : &nh->ip_dst;
1164 if (key->nw_proto == IP_TYPE_TCP) {
1165 struct tcp_header *th = packet->l4;
1166 th->tcp_csum = recalc_csum32(th->tcp_csum, *field, a->nw_addr);
1167 } else if (key->nw_proto == IP_TYPE_UDP) {
1168 struct udp_header *uh = packet->l4;
1170 uh->udp_csum = recalc_csum32(uh->udp_csum, *field, a->nw_addr);
1171 if (!uh->udp_csum) {
1172 uh->udp_csum = 0xffff;
1176 nh->ip_csum = recalc_csum32(nh->ip_csum, *field, a->nw_addr);
1177 *field = a->nw_addr;
1182 xf_netdev_set_nw_tos(struct ofpbuf *packet, const struct xflow_key *key,
1183 const struct xflow_action_nw_tos *a)
1185 if (key->dl_type == htons(ETH_TYPE_IP)) {
1186 struct ip_header *nh = packet->l3;
1187 uint8_t *field = &nh->ip_tos;
1189 /* Set the DSCP bits and preserve the ECN bits. */
1190 uint8_t new = a->nw_tos | (nh->ip_tos & IP_ECN_MASK);
1192 nh->ip_csum = recalc_csum16(nh->ip_csum, htons((uint16_t)*field),
1193 htons((uint16_t)a->nw_tos));
1199 xf_netdev_set_tp_port(struct ofpbuf *packet, const struct xflow_key *key,
1200 const struct xflow_action_tp_port *a)
1202 if (key->dl_type == htons(ETH_TYPE_IP)) {
1204 if (key->nw_proto == IPPROTO_TCP) {
1205 struct tcp_header *th = packet->l4;
1206 field = a->type == XFLOWAT_SET_TP_SRC ? &th->tcp_src : &th->tcp_dst;
1207 th->tcp_csum = recalc_csum16(th->tcp_csum, *field, a->tp_port);
1208 *field = a->tp_port;
1209 } else if (key->nw_proto == IPPROTO_UDP) {
1210 struct udp_header *uh = packet->l4;
1211 field = a->type == XFLOWAT_SET_TP_SRC ? &uh->udp_src : &uh->udp_dst;
1212 uh->udp_csum = recalc_csum16(uh->udp_csum, *field, a->tp_port);
1213 *field = a->tp_port;
1221 xf_netdev_output_port(struct xf_netdev *xf, struct ofpbuf *packet,
1224 struct xf_netdev_port *p = xf->ports[out_port];
1226 netdev_send(p->netdev, packet);
1231 xf_netdev_output_group(struct xf_netdev *xf, uint16_t group, uint16_t in_port,
1232 struct ofpbuf *packet)
1234 struct xflow_port_group *g = &xf->groups[group];
1237 for (i = 0; i < g->n_ports; i++) {
1238 uint16_t out_port = g->ports[i];
1239 if (out_port != in_port) {
1240 xf_netdev_output_port(xf, packet, out_port);
1246 xf_netdev_output_control(struct xf_netdev *xf, const struct ofpbuf *packet,
1247 int queue_no, int port_no, uint32_t arg)
1249 struct ovs_queue *q = &xf->queues[queue_no];
1250 struct xflow_msg *header;
1254 if (q->n >= MAX_QUEUE_LEN) {
1259 msg_size = sizeof *header + packet->size;
1260 msg = ofpbuf_new(msg_size + XFIF_RECV_MSG_PADDING);
1261 header = ofpbuf_put_uninit(msg, sizeof *header);
1262 ofpbuf_reserve(msg, XFIF_RECV_MSG_PADDING);
1263 header->type = queue_no;
1264 header->length = msg_size;
1265 header->port = port_no;
1267 ofpbuf_put(msg, packet->data, packet->size);
1268 queue_push_tail(q, msg);
1274 xf_netdev_execute_actions(struct xf_netdev *xf,
1275 struct ofpbuf *packet, struct xflow_key *key,
1276 const union xflow_action *actions, int n_actions)
1279 for (i = 0; i < n_actions; i++) {
1280 const union xflow_action *a = &actions[i];
1283 case XFLOWAT_OUTPUT:
1284 xf_netdev_output_port(xf, packet, a->output.port);
1287 case XFLOWAT_OUTPUT_GROUP:
1288 xf_netdev_output_group(xf, a->output_group.group, key->in_port,
1292 case XFLOWAT_CONTROLLER:
1293 xf_netdev_output_control(xf, packet, _XFLOWL_ACTION_NR,
1294 key->in_port, a->controller.arg);
1297 case XFLOWAT_SET_DL_TCI:
1298 xf_netdev_set_dl_tci(packet, key, &a->dl_tci);
1301 case XFLOWAT_STRIP_VLAN:
1302 xf_netdev_strip_vlan(packet, key);
1305 case XFLOWAT_SET_DL_SRC:
1306 xf_netdev_set_dl_src(packet, a->dl_addr.dl_addr);
1309 case XFLOWAT_SET_DL_DST:
1310 xf_netdev_set_dl_dst(packet, a->dl_addr.dl_addr);
1313 case XFLOWAT_SET_NW_SRC:
1314 case XFLOWAT_SET_NW_DST:
1315 xf_netdev_set_nw_addr(packet, key, &a->nw_addr);
1318 case XFLOWAT_SET_NW_TOS:
1319 xf_netdev_set_nw_tos(packet, key, &a->nw_tos);
1322 case XFLOWAT_SET_TP_SRC:
1323 case XFLOWAT_SET_TP_DST:
1324 xf_netdev_set_tp_port(packet, key, &a->tp_port);
1331 const struct xfif_class xfif_netdev_class = {
1335 NULL, /* enumerate */
1338 NULL, /* get_all_names */
1339 xfif_netdev_destroy,
1340 xfif_netdev_get_stats,
1341 xfif_netdev_get_drop_frags,
1342 xfif_netdev_set_drop_frags,
1343 xfif_netdev_port_add,
1344 xfif_netdev_port_del,
1345 xfif_netdev_port_query_by_number,
1346 xfif_netdev_port_query_by_name,
1347 xfif_netdev_port_list,
1348 xfif_netdev_port_poll,
1349 xfif_netdev_port_poll_wait,
1350 xfif_netdev_port_group_get,
1351 xfif_netdev_port_group_set,
1352 xfif_netdev_flow_get,
1353 xfif_netdev_flow_put,
1354 xfif_netdev_flow_del,
1355 xfif_netdev_flow_flush,
1356 xfif_netdev_flow_list,
1357 xfif_netdev_execute,
1358 xfif_netdev_recv_get_mask,
1359 xfif_netdev_recv_set_mask,
1360 NULL, /* get_sflow_probability */
1361 NULL, /* set_sflow_probability */
1362 NULL, /* queue_to_priority */
1364 xfif_netdev_recv_wait,