X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=lib%2Fdpif-netdev.c;h=eddd18bef0a4da88123724073017d77eb309c6bd;hb=05abb769d32d440dbd521e99484d8418f95431bd;hp=2fb13391590884d8092c4ca9f0409a82a61163fc;hpb=14608a1539b73f8f9812e0e791adb60825fee38b;p=openvswitch diff --git a/lib/dpif-netdev.c b/lib/dpif-netdev.c index 2fb13391..eddd18be 100644 --- a/lib/dpif-netdev.c +++ b/lib/dpif-netdev.c @@ -32,7 +32,9 @@ #include #include "csum.h" +#include "dpif.h" #include "dpif-provider.h" +#include "dummy.h" #include "flow.h" #include "hmap.h" #include "list.h" @@ -42,12 +44,12 @@ #include "ofpbuf.h" #include "packets.h" #include "poll-loop.h" -#include "queue.h" +#include "shash.h" #include "timeval.h" #include "util.h" #include "vlog.h" -VLOG_DEFINE_THIS_MODULE(dpif_netdev) +VLOG_DEFINE_THIS_MODULE(dpif_netdev); /* Configuration parameters. */ enum { N_QUEUES = 2 }; /* Number of queues for dpif_recv(). */ @@ -61,13 +63,14 @@ enum { DP_NETDEV_HEADROOM = 2 + VLAN_HEADER_LEN }; /* Datapath based on the network device interface from netdev.h. */ struct dp_netdev { - struct list node; - int dp_idx; + const struct dpif_class *class; + char *name; int open_cnt; bool destroyed; bool drop_frags; /* Drop all IP fragments, if true. */ - struct ovs_queue queues[N_QUEUES]; /* Messages queued for dpif_recv(). */ + struct list queues[N_QUEUES]; /* Contain ofpbufs queued for dpif_recv(). */ + size_t queue_len[N_QUEUES]; /* Number of packets in each queue. */ struct hmap flow_table; /* Flow table. */ /* Statistics. */ @@ -88,7 +91,7 @@ struct dp_netdev_port { int port_no; /* Index into dp_netdev's 'ports'. */ struct list node; /* Element in dp_netdev's 'port_list'. */ struct netdev *netdev; - bool internal; /* Internal port (as ODP_PORT_INTERNAL)? */ + bool internal; /* Internal port? */ }; /* A flow in dp_netdev's 'flow_table'. */ @@ -116,9 +119,7 @@ struct dpif_netdev { }; /* All netdev-based datapaths. */ -static struct dp_netdev *dp_netdevs[256]; -struct list dp_netdev_list = LIST_INITIALIZER(&dp_netdev_list); -enum { N_DP_NETDEVS = ARRAY_SIZE(dp_netdevs) }; +static struct shash dp_netdevs = SHASH_INITIALIZER(&dp_netdevs); /* Maximum port MTU seen so far. */ static int max_mtu = ETH_PAYLOAD_MAX; @@ -129,19 +130,23 @@ static int get_port_by_name(struct dp_netdev *, const char *devname, struct dp_netdev_port **portp); static void dp_netdev_free(struct dp_netdev *); static void dp_netdev_flow_flush(struct dp_netdev *); -static int do_add_port(struct dp_netdev *, const char *devname, uint16_t flags, - uint16_t port_no); +static int do_add_port(struct dp_netdev *, const char *devname, + const char *type, uint16_t port_no); static int do_del_port(struct dp_netdev *, uint16_t port_no); +static int dpif_netdev_open(const struct dpif_class *, const char *name, + bool create, struct dpif **); static int dp_netdev_output_control(struct dp_netdev *, const struct ofpbuf *, int queue_no, int port_no, uint32_t arg); static int dp_netdev_execute_actions(struct dp_netdev *, struct ofpbuf *, struct flow *, const union odp_action *, int n); +static struct dpif_class dpif_dummy_class; + static struct dpif_netdev * dpif_netdev_cast(const struct dpif *dpif) { - dpif_assert_class(dpif, &dpif_netdev_class); + assert(dpif->dpif_class->open == dpif_netdev_open); return CONTAINER_OF(dpif, struct dpif_netdev, dpif); } @@ -151,125 +156,80 @@ get_dp_netdev(const struct dpif *dpif) return dpif_netdev_cast(dpif)->dp; } -static int -name_to_dp_idx(const char *name) -{ - if (!strncmp(name, "dp", 2) && isdigit((unsigned char)name[2])) { - int dp_idx = atoi(name + 2); - if (dp_idx >= 0 && dp_idx < N_DP_NETDEVS) { - return dp_idx; - } - } - return -1; -} - -static struct dp_netdev * -find_dp_netdev(const char *name) -{ - int dp_idx; - size_t i; - - dp_idx = name_to_dp_idx(name); - if (dp_idx >= 0) { - return dp_netdevs[dp_idx]; - } - - for (i = 0; i < N_DP_NETDEVS; i++) { - struct dp_netdev *dp = dp_netdevs[i]; - if (dp) { - struct dp_netdev_port *port; - if (!get_port_by_name(dp, name, &port)) { - return dp; - } - } - } - return NULL; -} - static struct dpif * create_dpif_netdev(struct dp_netdev *dp) { + uint16_t netflow_id = hash_string(dp->name, 0); struct dpif_netdev *dpif; - char *dpname; dp->open_cnt++; - dpname = xasprintf("dp%d", dp->dp_idx); dpif = xmalloc(sizeof *dpif); - dpif_init(&dpif->dpif, &dpif_netdev_class, dpname, dp->dp_idx, dp->dp_idx); + dpif_init(&dpif->dpif, dp->class, dp->name, netflow_id >> 8, netflow_id); dpif->dp = dp; dpif->listen_mask = 0; dpif->dp_serial = dp->serial; - free(dpname); return &dpif->dpif; } static int -create_dp_netdev(const char *name, int dp_idx, struct dpif **dpifp) +create_dp_netdev(const char *name, const struct dpif_class *class, + struct dp_netdev **dpp) { struct dp_netdev *dp; int error; int i; - if (dp_netdevs[dp_idx]) { - return EBUSY; - } - - /* Create datapath. */ - dp_netdevs[dp_idx] = dp = xzalloc(sizeof *dp); - list_push_back(&dp_netdev_list, &dp->node); - dp->dp_idx = dp_idx; + dp = xzalloc(sizeof *dp); + dp->class = class; + dp->name = xstrdup(name); dp->open_cnt = 0; dp->drop_frags = false; for (i = 0; i < N_QUEUES; i++) { - queue_init(&dp->queues[i]); + list_init(&dp->queues[i]); } hmap_init(&dp->flow_table); list_init(&dp->port_list); - error = do_add_port(dp, name, ODP_PORT_INTERNAL, ODPP_LOCAL); + error = do_add_port(dp, name, "internal", ODPP_LOCAL); if (error) { dp_netdev_free(dp); - return ENODEV; + return error; } - *dpifp = create_dpif_netdev(dp); + shash_add(&dp_netdevs, name, dp); + + *dpp = dp; return 0; } static int -dpif_netdev_open(const char *name, const char *type OVS_UNUSED, bool create, - struct dpif **dpifp) +dpif_netdev_open(const struct dpif_class *class, const char *name, + bool create, struct dpif **dpifp) { - if (create) { - if (find_dp_netdev(name)) { - return EEXIST; - } else { - int dp_idx = name_to_dp_idx(name); - if (dp_idx >= 0) { - return create_dp_netdev(name, dp_idx, dpifp); - } else { - /* Scan for unused dp_idx number. */ - for (dp_idx = 0; dp_idx < N_DP_NETDEVS; dp_idx++) { - int error = create_dp_netdev(name, dp_idx, dpifp); - if (error != EBUSY) { - return error; - } - } + struct dp_netdev *dp; - /* All datapath numbers in use. */ - return ENOBUFS; + dp = shash_find_data(&dp_netdevs, name); + if (!dp) { + if (!create) { + return ENODEV; + } else { + int error = create_dp_netdev(name, class, &dp); + if (error) { + return error; } + assert(dp != NULL); } } else { - struct dp_netdev *dp = find_dp_netdev(name); - if (dp) { - *dpifp = create_dpif_netdev(dp); - return 0; - } else { - return ENODEV; + if (dp->class != class) { + return EINVAL; + } else if (create) { + return EEXIST; } } + + *dpifp = create_dpif_netdev(dp); + return 0; } static void @@ -284,11 +244,10 @@ dp_netdev_free(struct dp_netdev *dp) do_del_port(dp, port->port_no); } for (i = 0; i < N_QUEUES; i++) { - queue_destroy(&dp->queues[i]); + ofpbuf_list_delete(&dp->queues[i]); } hmap_destroy(&dp->flow_table); - dp_netdevs[dp->dp_idx] = NULL; - list_remove(&dp->node); + free(dp->name); free(dp); } @@ -298,6 +257,7 @@ dpif_netdev_close(struct dpif *dpif) struct dp_netdev *dp = get_dp_netdev(dpif); assert(dp->open_cnt > 0); if (--dp->open_cnt == 0 && dp->destroyed) { + shash_find_and_delete(&dp_netdevs, dp->name); dp_netdev_free(dp); } free(dpif); @@ -347,23 +307,33 @@ dpif_netdev_set_drop_frags(struct dpif *dpif, bool drop_frags) } static int -do_add_port(struct dp_netdev *dp, const char *devname, uint16_t flags, +do_add_port(struct dp_netdev *dp, const char *devname, const char *type, uint16_t port_no) { - bool internal = (flags & ODP_PORT_INTERNAL) != 0; struct dp_netdev_port *port; struct netdev_options netdev_options; struct netdev *netdev; + bool internal; int mtu; int error; /* XXX reject devices already in some dp_netdev. */ + if (type[0] == '\0' || !strcmp(type, "system")) { + internal = false; + } else if (!strcmp(type, "internal")) { + internal = true; + } else { + VLOG_WARN("%s: unsupported port type %s", devname, type); + return EINVAL; + } /* Open and validate network device. */ memset(&netdev_options, 0, sizeof netdev_options); netdev_options.name = devname; netdev_options.ethertype = NETDEV_ETH_TYPE_ANY; - if (internal) { + if (dp->class == &dpif_dummy_class) { + netdev_options.type = "dummy"; + } else if (internal) { netdev_options.type = "tap"; } @@ -399,7 +369,7 @@ do_add_port(struct dp_netdev *dp, const char *devname, uint16_t flags, } static int -dpif_netdev_port_add(struct dpif *dpif, const char *devname, uint16_t flags, +dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev, uint16_t *port_nop) { struct dp_netdev *dp = get_dp_netdev(dpif); @@ -408,7 +378,8 @@ dpif_netdev_port_add(struct dpif *dpif, const char *devname, uint16_t flags, for (port_no = 0; port_no < MAX_PORTS; port_no++) { if (!dp->ports[port_no]) { *port_nop = port_no; - return do_add_port(dp, devname, flags, port_no); + return do_add_port(dp, netdev_get_name(netdev), + netdev_get_type(netdev), port_no); } } return EFBIG; @@ -488,7 +459,7 @@ answer_port_query(const struct dp_netdev_port *port, struct odp_port *odp_port) ovs_strlcpy(odp_port->devname, netdev_get_name(port->netdev), sizeof odp_port->devname); odp_port->port = port->port_no; - odp_port->flags = port->internal ? ODP_PORT_INTERNAL : 0; + strcpy(odp_port->type, port->internal ? "internal" : "system"); } static int @@ -665,16 +636,9 @@ dpif_netdev_validate_actions(const union odp_action *actions, int n_actions, case ODPAT_CONTROLLER: break; - case ODPAT_SET_VLAN_VID: - *mutates = true; - if (a->vlan_vid.vlan_vid & htons(~VLAN_VID_MASK)) { - return EINVAL; - } - break; - - case ODPAT_SET_VLAN_PCP: + case ODPAT_SET_DL_TCI: *mutates = true; - if (a->vlan_pcp.vlan_pcp & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT)) { + if (a->dl_tci.tci & htons(VLAN_CFI)) { return EINVAL; } break; @@ -835,7 +799,7 @@ dpif_netdev_execute(struct dpif *dpif, struct dp_netdev *dp = get_dp_netdev(dpif); struct ofpbuf copy; bool mutates; - flow_t key; + struct flow key; int error; if (packet->size < ETH_HEADER_LEN || packet->size > UINT16_MAX) { @@ -851,7 +815,7 @@ dpif_netdev_execute(struct dpif *dpif, /* We need a deep copy of 'packet' since we're going to modify its * data. */ ofpbuf_init(©, DP_NETDEV_HEADROOM + packet->size); - copy.data = (char*)copy.base + DP_NETDEV_HEADROOM; + ofpbuf_reserve(©, DP_NETDEV_HEADROOM); ofpbuf_put(©, packet->data, packet->size); } else { /* We still need a shallow copy of 'packet', even though we won't @@ -888,7 +852,7 @@ dpif_netdev_recv_set_mask(struct dpif *dpif, int listen_mask) } } -static struct ovs_queue * +static int find_nonempty_queue(struct dpif *dpif) { struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif); @@ -897,20 +861,24 @@ find_nonempty_queue(struct dpif *dpif) int i; for (i = 0; i < N_QUEUES; i++) { - struct ovs_queue *q = &dp->queues[i]; - if (q->n && mask & (1u << i)) { - return q; + struct list *queue = &dp->queues[i]; + if (!list_is_empty(queue) && mask & (1u << i)) { + return i; } } - return NULL; + return -1; } static int dpif_netdev_recv(struct dpif *dpif, struct ofpbuf **bufp) { - struct ovs_queue *q = find_nonempty_queue(dpif); - if (q) { - *bufp = queue_pop_head(q); + int queue_idx = find_nonempty_queue(dpif); + if (queue_idx >= 0) { + struct dp_netdev *dp = get_dp_netdev(dpif); + + *bufp = ofpbuf_from_list(list_pop_front(&dp->queues[queue_idx])); + dp->queue_len[queue_idx]--; + return 0; } else { return EAGAIN; @@ -920,8 +888,7 @@ dpif_netdev_recv(struct dpif *dpif, struct ofpbuf **bufp) static void dpif_netdev_recv_wait(struct dpif *dpif) { - struct ovs_queue *q = find_nonempty_queue(dpif); - if (q) { + if (find_nonempty_queue(dpif) >= 0) { poll_immediate_wake(); } else { /* No messages ready to be received, and dp_wait() will ensure that we @@ -972,24 +939,25 @@ dp_netdev_port_input(struct dp_netdev *dp, struct dp_netdev_port *port, static void dp_netdev_run(void) { + struct shash_node *node; struct ofpbuf packet; - struct dp_netdev *dp; ofpbuf_init(&packet, DP_NETDEV_HEADROOM + max_mtu); - LIST_FOR_EACH (dp, node, &dp_netdev_list) { + SHASH_FOR_EACH (node, &dp_netdevs) { + struct dp_netdev *dp = node->data; struct dp_netdev_port *port; LIST_FOR_EACH (port, node, &dp->port_list) { int error; /* Reset packet contents. */ - packet.data = (char*)packet.base + DP_NETDEV_HEADROOM; - packet.size = 0; + ofpbuf_clear(&packet); + ofpbuf_reserve(&packet, DP_NETDEV_HEADROOM); error = netdev_recv(port->netdev, &packet); if (!error) { dp_netdev_port_input(dp, port, &packet); - } else if (error != EAGAIN) { + } else if (error != EAGAIN && error != EOPNOTSUPP) { struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); VLOG_ERR_RL(&rl, "error receiving data from %s: %s", netdev_get_name(port->netdev), strerror(error)); @@ -1002,10 +970,12 @@ dp_netdev_run(void) static void dp_netdev_wait(void) { - struct dp_netdev *dp; + struct shash_node *node; - LIST_FOR_EACH (dp, node, &dp_netdev_list) { + SHASH_FOR_EACH (node, &dp_netdevs) { + struct dp_netdev *dp = node->data; struct dp_netdev_port *port; + LIST_FOR_EACH (port, node, &dp->port_list) { netdev_recv_wait(port->netdev); } @@ -1013,16 +983,12 @@ dp_netdev_wait(void) } -/* Modify the TCI field of 'packet'. If a VLAN tag is not present, one - * is added with the TCI field set to 'tci'. If a VLAN tag is present, - * then 'mask' bits are cleared before 'tci' is logically OR'd into the - * TCI field. - * - * Note that the function does not ensure that 'tci' does not affect - * bits outside of 'mask'. +/* Modify the TCI field of 'packet'. If a VLAN tag is present, its TCI field + * is replaced by 'tci'. If a VLAN tag is not present, one is added with the + * TCI field set to 'tci'. */ static void -dp_netdev_modify_vlan_tci(struct ofpbuf *packet, uint16_t tci, uint16_t mask) +dp_netdev_set_dl_tci(struct ofpbuf *packet, uint16_t tci) { struct vlan_eth_header *veh; struct eth_header *eh; @@ -1030,17 +996,15 @@ dp_netdev_modify_vlan_tci(struct ofpbuf *packet, uint16_t tci, uint16_t mask) eh = packet->l2; if (packet->size >= sizeof(struct vlan_eth_header) && eh->eth_type == htons(ETH_TYPE_VLAN)) { - /* Clear 'mask' bits, but maintain other TCI bits. */ veh = packet->l2; - veh->veth_tci &= ~htons(mask); - veh->veth_tci |= htons(tci); + veh->veth_tci = tci; } else { /* Insert new 802.1Q header. */ struct vlan_eth_header tmp; memcpy(tmp.veth_dst, eh->eth_dst, ETH_ADDR_LEN); memcpy(tmp.veth_src, eh->eth_src, ETH_ADDR_LEN); tmp.veth_type = htons(ETH_TYPE_VLAN); - tmp.veth_tci = htons(tci); + tmp.veth_tci = tci; tmp.veth_next_type = eh->eth_type; veh = ofpbuf_push_uninit(packet, VLAN_HEADER_LEN); @@ -1061,8 +1025,7 @@ dp_netdev_strip_vlan(struct ofpbuf *packet) memcpy(tmp.eth_src, veh->veth_src, ETH_ADDR_LEN); tmp.eth_type = veh->veth_next_type; - packet->size -= VLAN_HEADER_LEN; - packet->data = (char*)packet->data + VLAN_HEADER_LEN; + ofpbuf_pull(packet, VLAN_HEADER_LEN); packet->l2 = (char*)packet->l2 + VLAN_HEADER_LEN; memcpy(packet->data, &tmp, sizeof tmp); } @@ -1167,12 +1130,11 @@ static int dp_netdev_output_control(struct dp_netdev *dp, const struct ofpbuf *packet, int queue_no, int port_no, uint32_t arg) { - struct ovs_queue *q = &dp->queues[queue_no]; struct odp_msg *header; struct ofpbuf *msg; size_t msg_size; - if (q->n >= MAX_QUEUE_LEN) { + if (dp->queue_len[queue_no] >= MAX_QUEUE_LEN) { dp->n_lost++; return ENOBUFS; } @@ -1185,7 +1147,8 @@ dp_netdev_output_control(struct dp_netdev *dp, const struct ofpbuf *packet, header->port = port_no; header->arg = arg; ofpbuf_put(msg, packet->data, packet->size); - queue_push_tail(q, msg); + list_push_back(&dp->queues[queue_no], &msg->list_node); + dp->queue_len[queue_no]++; return 0; } @@ -1237,15 +1200,8 @@ dp_netdev_execute_actions(struct dp_netdev *dp, key->in_port, a->controller.arg); break; - case ODPAT_SET_VLAN_VID: - dp_netdev_modify_vlan_tci(packet, ntohs(a->vlan_vid.vlan_vid), - VLAN_VID_MASK); - break; - - case ODPAT_SET_VLAN_PCP: - dp_netdev_modify_vlan_tci(packet, - a->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT, - VLAN_PCP_MASK); + case ODPAT_SET_DL_TCI: + dp_netdev_set_dl_tci(packet, a->dl_tci.tci); break; case ODPAT_STRIP_VLAN: @@ -1316,3 +1272,13 @@ const struct dpif_class dpif_netdev_class = { dpif_netdev_recv, dpif_netdev_recv_wait, }; + +void +dpif_dummy_register(void) +{ + if (!dpif_dummy_class.type) { + dpif_dummy_class = dpif_netdev_class; + dpif_dummy_class.type = "dummy"; + dp_register_provider(&dpif_dummy_class); + } +}