+static bool
+local_hook(struct relay *r)
+{
+ struct rconn *rc = r->halves[HALF_LOCAL].rconn;
+ struct buffer *msg = r->halves[HALF_LOCAL].rxbuf;
+ struct ofp_packet_in *opi;
+ struct ofp_header *oh;
+ size_t pkt_ofs, pkt_len;
+ struct buffer pkt, *b;
+ struct flow flow;
+ uint16_t in_port, out_port;
+
+ if (!local_port) {
+ return false;
+ }
+
+ oh = msg->data;
+ if (oh->type != OFPT_PACKET_IN) {
+ return false;
+ }
+ if (msg->size < offsetof (struct ofp_packet_in, data)) {
+ VLOG_WARN("packet too short (%zu bytes) for packet_in", msg->size);
+ return false;
+ }
+
+ /* Extract flow data from 'opi' into 'flow'. */
+ opi = msg->data;
+ in_port = ntohs(opi->in_port);
+ 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, in_port, &flow);
+
+ /* Deal with local stuff. */
+ if (!rconn_is_connected(r->halves[HALF_REMOTE].rconn)
+ && eth_addr_is_broadcast(flow.dl_dst)) {
+ out_port = OFPP_FLOOD;
+ } else if (in_port == OFPP_LOCAL) {
+ out_port = mac_learning_lookup(local_ml, flow.dl_dst);
+ } else if (eth_addr_equals(flow.dl_dst, local_mac)) {
+ out_port = OFPP_LOCAL;
+ if (mac_learning_learn(local_ml, flow.dl_src, in_port)) {
+ VLOG_DBG("learned that "ETH_ADDR_FMT" is on port %"PRIu16,
+ ETH_ADDR_ARGS(flow.dl_src), in_port);
+ }
+ } else {
+ return false;
+ }
+
+ /* Add new flow. */
+ if (out_port != OFPP_FLOOD) {
+ b = make_add_simple_flow(&flow, ntohl(opi->buffer_id), out_port);
+ if (rconn_force_send(rc, b)) {
+ buffer_delete(b);
+ }
+ }
+
+ /* If the switch didn't buffer the packet, we need to send a copy. */
+ if (out_port == OFPP_FLOOD || ntohl(opi->buffer_id) == UINT32_MAX) {
+ b = make_unbuffered_packet_out(&pkt, in_port, out_port);
+ if (rconn_force_send(rc, b)) {
+ buffer_delete(b);
+ }
+ }
+ return true;
+}
+