- time_t now = time(0);
- if (now >= this->last_features_request + 1) {
- struct buffer *b;
- struct ofp_header *ofr;
- struct ofp_switch_config *osc;
-
- /* Send OFPT_SET_CONFIG. */
- b = buffer_new(0);
- osc = buffer_put_uninit(b, sizeof *osc);
- memset(osc, 0, sizeof *osc);
- osc->header.type = OFPT_SET_CONFIG;
- osc->header.version = OFP_VERSION;
- osc->header.length = htons(sizeof *osc);
- osc->flags = htons(OFPC_SEND_FLOW_EXP);
- osc->miss_send_len = htons(OFP_DEFAULT_MISS_SEND_LEN);
- queue_tx(this, b);
-
- /* Send OFPT_FEATURES_REQUEST. */
- b = buffer_new(0);
- ofr = buffer_put_uninit(b, sizeof *ofr);
- memset(ofr, 0, sizeof *ofr);
- ofr->type = OFPT_FEATURES_REQUEST;
- ofr->version = OFP_VERSION;
- ofr->length = htons(sizeof *ofr);
- queue_tx(this, b);
-
- this->last_features_request = now;
- }
-}
-
-static void
-queue_tx(struct switch_ *this, struct buffer *b)
-{
- queue_push_tail(&this->txq, b);
-}
-
-static void
-process_packet(struct switch_ *sw, struct buffer *msg)
-{
- static const size_t min_size[UINT8_MAX + 1] = {
- [0 ... UINT8_MAX] = sizeof (struct ofp_header),
- [OFPT_FEATURES_REPLY] = sizeof (struct ofp_switch_features),
- [OFPT_PACKET_IN] = offsetof (struct ofp_packet_in, data),
- };
- struct ofp_header *oh;
-
- oh = msg->data;
- if (msg->size < min_size[oh->type]) {
- VLOG_WARN("%s: too short (%zu bytes) for type %"PRIu8" (min %zu)",
- sw->name, msg->size, oh->type, min_size[oh->type]);
- return;
- }
-
- if (oh->type == OFPT_FEATURES_REPLY) {
- struct ofp_switch_features *osf = msg->data;
- sw->datapath_id = osf->datapath_id;
- } else if (sw->datapath_id == 0) {
- send_features_request(sw);
- } else if (oh->type == OFPT_PACKET_IN) {
- struct ofp_packet_in *opi = msg->data;
- if (sw->txq.n >= MAX_TXQ) {
- /* FIXME: ratelimit. */
- VLOG_WARN("%s: tx queue overflow", sw->name);
- } else if (noflow) {
- process_noflow(sw, opi);
- } else if (hub) {
- process_hub(sw, opi);
- } else {
- process_switch(sw, opi);
- }
- } else {
- ofp_print(stdout, msg->data, msg->size, 2);
- }
-}
-
-static void
-process_hub(struct switch_ *sw, struct ofp_packet_in *opi)
-{
- size_t pkt_ofs, pkt_len;
- struct buffer pkt;
- struct flow flow;
-
- /* Extract flow data from 'opi' into 'flow'. */
- pkt_ofs = offsetof(struct ofp_packet_in, data);
- pkt_len = ntohs(opi->header.length) - pkt_ofs;
- pkt.data = opi->data;
- pkt.size = pkt_len;
- flow_extract(&pkt, ntohs(opi->in_port), &flow);
-
- /* Add new flow. */
- queue_tx(sw, make_add_simple_flow(&flow, ntohl(opi->buffer_id),
- OFPP_FLOOD));
-
- /* If the switch didn't buffer the packet, we need to send a copy. */
- if (ntohl(opi->buffer_id) == UINT32_MAX) {
- queue_tx(sw, make_unbuffered_packet_out(&pkt, ntohs(flow.in_port),
- OFPP_FLOOD));
- }
-}
-
-static void
-process_noflow(struct switch_ *sw, struct ofp_packet_in *opi)
-{
- /* If the switch didn't buffer the packet, we need to send a copy. */
- if (ntohl(opi->buffer_id) == UINT32_MAX) {
- size_t pkt_ofs, pkt_len;
- struct buffer pkt;
-
- /* Extract flow data from 'opi' into 'flow'. */
- pkt_ofs = offsetof(struct ofp_packet_in, data);
- pkt_len = ntohs(opi->header.length) - pkt_ofs;
- pkt.data = opi->data;
- pkt.size = pkt_len;
-
- queue_tx(sw, make_unbuffered_packet_out(&pkt, ntohs(opi->in_port),
- OFPP_FLOOD));
- } else {
- queue_tx(sw, make_buffered_packet_out(ntohl(opi->buffer_id),
- ntohs(opi->in_port), OFPP_FLOOD));
- }
-}
-
-
-#define MAC_HASH_BITS 10
-#define MAC_HASH_MASK (MAC_HASH_SIZE - 1)
-#define MAC_HASH_SIZE (1u << MAC_HASH_BITS)
-
-#define MAC_MAX 1024
-
-struct mac_source {
- struct list hash_list;
- struct list lru_list;
- uint64_t datapath_id;
- uint8_t mac[ETH_ADDR_LEN];
- uint16_t port;
-};
-
-static struct list mac_table[MAC_HASH_SIZE];
-static struct list lrus;
-static size_t mac_count;
-
-static void
-switch_init(void)
-{
- int i;
-
- list_init(&lrus);
- for (i = 0; i < MAC_HASH_SIZE; i++) {
- list_init(&mac_table[i]);
- }