+ eth->eth_type = htons(ETH_TYPE_RARP);
+
+ arp = ofpbuf_put_uninit(b, sizeof *arp);
+ arp->ar_hrd = htons(ARP_HRD_ETHERNET);
+ arp->ar_pro = htons(ARP_PRO_IP);
+ arp->ar_hln = sizeof arp->ar_sha;
+ arp->ar_pln = sizeof arp->ar_spa;
+ arp->ar_op = htons(ARP_OP_RARP);
+ memcpy(arp->ar_sha, eth_src, ETH_ADDR_LEN);
+ arp->ar_spa = htonl(0);
+ memcpy(arp->ar_tha, eth_src, ETH_ADDR_LEN);
+ arp->ar_tpa = htonl(0);
+}
+
+/* Insert VLAN header according to given TCI. Packet passed must be Ethernet
+ * packet. Ignores the CFI bit of 'tci' using 0 instead.
+ *
+ * Also sets 'packet->l2' to point to the new Ethernet header. */
+void
+eth_push_vlan(struct ofpbuf *packet, ovs_be16 tci)
+{
+ struct eth_header *eh = packet->data;
+ struct vlan_eth_header *veh;
+
+ /* 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 = tci & htons(~VLAN_CFI);
+ tmp.veth_next_type = eh->eth_type;
+
+ veh = ofpbuf_push_uninit(packet, VLAN_HEADER_LEN);
+ memcpy(veh, &tmp, sizeof tmp);
+
+ packet->l2 = packet->data;
+}
+
+/* Removes outermost VLAN header (if any is present) from 'packet'.
+ *
+ * 'packet->l2' must initially point to 'packet''s Ethernet header. */
+void
+eth_pop_vlan(struct ofpbuf *packet)
+{
+ struct vlan_eth_header *veh = packet->l2;
+ if (packet->size >= sizeof *veh
+ && veh->veth_type == htons(ETH_TYPE_VLAN)) {
+ struct eth_header tmp;
+
+ memcpy(tmp.eth_dst, veh->veth_dst, ETH_ADDR_LEN);
+ memcpy(tmp.eth_src, veh->veth_src, ETH_ADDR_LEN);
+ tmp.eth_type = veh->veth_next_type;
+
+ ofpbuf_pull(packet, VLAN_HEADER_LEN);
+ packet->l2 = (char*)packet->l2 + VLAN_HEADER_LEN;
+ memcpy(packet->data, &tmp, sizeof tmp);
+ }
+}
+
+/* Converts hex digits in 'hex' to an Ethernet packet in '*packetp'. The
+ * caller must free '*packetp'. On success, returns NULL. On failure, returns
+ * an error message and stores NULL in '*packetp'. */
+const char *
+eth_from_hex(const char *hex, struct ofpbuf **packetp)
+{
+ struct ofpbuf *packet;
+
+ packet = *packetp = ofpbuf_new(strlen(hex) / 2);
+
+ if (ofpbuf_put_hex(packet, hex, NULL)[0] != '\0') {
+ ofpbuf_delete(packet);
+ *packetp = NULL;
+ return "Trailing garbage in packet data";
+ }
+
+ if (packet->size < ETH_HEADER_LEN) {
+ ofpbuf_delete(packet);
+ *packetp = NULL;
+ return "Packet data too short for Ethernet";
+ }
+
+ return NULL;
+}
+
+void
+eth_format_masked(const uint8_t eth[ETH_ADDR_LEN],
+ const uint8_t mask[ETH_ADDR_LEN], struct ds *s)
+{
+ ds_put_format(s, ETH_ADDR_FMT, ETH_ADDR_ARGS(eth));
+ if (mask && !eth_mask_is_exact(mask)) {
+ ds_put_format(s, "/"ETH_ADDR_FMT, ETH_ADDR_ARGS(mask));
+ }
+}
+
+void
+eth_addr_bitand(const uint8_t src[ETH_ADDR_LEN],
+ const uint8_t mask[ETH_ADDR_LEN],
+ uint8_t dst[ETH_ADDR_LEN])
+{
+ int i;
+
+ for (i = 0; i < ETH_ADDR_LEN; i++) {
+ dst[i] = src[i] & mask[i];
+ }
+}
+
+/* Given the IP netmask 'netmask', returns the number of bits of the IP address
+ * that it specifies, that is, the number of 1-bits in 'netmask'.
+ *
+ * If 'netmask' is not a CIDR netmask (see ip_is_cidr()), the return value will
+ * still be in the valid range but isn't otherwise meaningful. */
+int
+ip_count_cidr_bits(ovs_be32 netmask)
+{
+ return 32 - ctz(ntohl(netmask));
+}
+
+void
+ip_format_masked(ovs_be32 ip, ovs_be32 mask, struct ds *s)
+{
+ ds_put_format(s, IP_FMT, IP_ARGS(&ip));
+ if (mask != htonl(UINT32_MAX)) {
+ if (ip_is_cidr(mask)) {
+ ds_put_format(s, "/%d", ip_count_cidr_bits(mask));
+ } else {
+ ds_put_format(s, "/"IP_FMT, IP_ARGS(&mask));
+ }
+ }