Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


add statistic structure to virtual ethernet devices
[palacios.git] / palacios / include / palacios / vmm_ethernet.h
1 /* 
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.  
5  *
6  * The V3VEE Project is a joint project between Northwestern University
7  * and the University of New Mexico.  You can find out more at 
8  * http://www.v3vee.org
9  *
10  * Copyright (c) 2011, Lei Xia <lxia@northwestern.edu> 
11  * Copyright (c) 2011, The V3VEE Project <http://www.v3vee.org> 
12  * All rights reserved.
13  *
14  * Author: Lei Xia <lxia@northwestern.edu>
15  *
16  * This is free software.  You are permitted to use,
17  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
18  */
19
20 #ifndef __ETHERNET_H__
21 #define __ETHERNET_H__
22
23 #define ETHERNET_HEADER_LEN 14
24 #define ETHERNET_MTU   1500
25 #define ETHERNET_PACKET_LEN (ETHERNET_HEADER_LEN + ETHERNET_MTU)
26 #define ETH_ALEN 6
27
28
29 #ifdef __V3VEE__
30
31 #include <palacios/vmm.h>
32
33
34 struct nic_statistics {
35     uint64_t tx_pkts;
36     uint64_t tx_bytes;
37     uint64_t tx_dropped;
38         
39     uint64_t rx_pkts;
40     uint64_t rx_bytes;
41     uint64_t rx_dropped;
42
43     uint32_t interrupts;
44 };
45     
46 static inline int is_multicast_ethaddr(const uint8_t * addr)
47 {
48     V3_ASSERT(ETH_ALEN == 6);
49         
50     return (0x01 & addr[0]);
51 }
52
53 static inline int is_broadcast_ethaddr(const uint8_t * addr)
54 {
55     V3_ASSERT(ETH_ALEN == 6);
56         
57     return (addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) == 0xff;
58 }
59
60
61 static inline int compare_ethaddr(const uint8_t * addr1, const uint8_t * addr2)
62 {
63     const uint16_t *a = (const uint16_t *) addr1;
64     const uint16_t *b = (const uint16_t *) addr2;
65
66     V3_ASSERT(ETH_ALEN == 6);
67     return ((a[0] ^ b[0]) | (a[1] ^ b[1]) | (a[2] ^ b[2])) != 0;
68 }
69
70
71 static inline int compare_ether_hdr(const uint8_t * hdr1, const uint8_t * hdr2)
72 {
73     uint32_t *a32 = (uint32_t *)(hdr1 + 2);
74     uint32_t *b32 = (uint32_t *)(hdr2 + 2);
75
76     V3_ASSERT(ETHERNET_HEADER_LEN == 14);
77
78     return (*(uint16_t *)hdr1 ^ *(uint16_t *)hdr2) | (a32[0] ^ b32[0]) |
79              (a32[1] ^ b32[1]) | (a32[2] ^ b32[2]);
80 }
81
82 /* AA:BB:CC:DD:EE:FF */
83 static inline int str2mac(char * macstr, uint8_t * mac){
84     char hex[2], *s = macstr;
85     int i = 0;
86
87     while(s){
88         memcpy(hex, s, 2);
89         mac[i++] = (char)atox(hex);     
90         if (i == ETH_ALEN) return 0;
91         s=strchr(s, ':');
92         if(s) s++;
93     }
94
95     return -1;
96 }
97
98
99 /* generate random ethernet address */
100 static inline void random_ethaddr(uint8_t * addr)
101 {
102     uint64_t val;
103
104     /* using current rdtsc as random number */
105     rdtscll(val);
106     *(uint64_t *)addr = val;
107         
108     addr [0] &= 0xfe;   /* clear multicast bit */
109     addr [0] |= 0x02;   /* set local assignment bit (IEEE802) */
110 }
111
112
113 #endif
114
115 #endif
116
117