2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2011, Lei Xia <lxia@northwestern.edu>
11 * Copyright (c) 2011, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Lei Xia <lxia@northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
20 #ifndef __ETHERNET_H__
21 #define __ETHERNET_H__
23 #define ETHERNET_HEADER_LEN 14
24 #define ETHERNET_MTU 1500
25 #define ETHERNET_PACKET_LEN (ETHERNET_HEADER_LEN + ETHERNET_MTU)
31 #include <palacios/vmm.h>
34 struct nic_statistics {
46 static inline int is_multicast_ethaddr(const uint8_t * addr)
48 V3_ASSERT(ETH_ALEN == 6);
50 return (0x01 & addr[0]);
53 static inline int is_broadcast_ethaddr(const uint8_t * addr)
55 V3_ASSERT(ETH_ALEN == 6);
57 return (addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) == 0xff;
61 static inline int compare_ethaddr(const uint8_t * addr1, const uint8_t * addr2)
63 const uint16_t *a = (const uint16_t *) addr1;
64 const uint16_t *b = (const uint16_t *) addr2;
66 V3_ASSERT(ETH_ALEN == 6);
67 return ((a[0] ^ b[0]) | (a[1] ^ b[1]) | (a[2] ^ b[2])) != 0;
71 static inline int compare_ether_hdr(const uint8_t * hdr1, const uint8_t * hdr2)
73 uint32_t *a32 = (uint32_t *)(hdr1 + 2);
74 uint32_t *b32 = (uint32_t *)(hdr2 + 2);
76 V3_ASSERT(ETHERNET_HEADER_LEN == 14);
78 return (*(uint16_t *)hdr1 ^ *(uint16_t *)hdr2) | (a32[0] ^ b32[0]) |
79 (a32[1] ^ b32[1]) | (a32[2] ^ b32[2]);
82 /* AA:BB:CC:DD:EE:FF */
83 static inline int str2mac(char * macstr, uint8_t * mac){
84 char hex[2], *s = macstr;
89 mac[i++] = (char)atox(hex);
90 if (i == ETH_ALEN) return 0;
99 /* generate random ethernet address */
100 static inline void random_ethaddr(uint8_t * addr)
104 /* using current rdtsc as random number */
106 *(uint64_t *)addr = val;
108 addr [0] &= 0xfe; /* clear multicast bit */
109 addr [0] |= 0x02; /* set local assignment bit (IEEE802) */