Skip to content

Commit

Permalink
use 64bit ulonglong to represent mac address
Browse files Browse the repository at this point in the history
Signed-off-by: Yonghong Song <[email protected]>
  • Loading branch information
yonghong-song committed Jun 10, 2015
1 parent 77b26e1 commit 5604fc8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
13 changes: 6 additions & 7 deletions tests/cc/test_brb.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ typedef struct bpf_dest {
u32 port_id;
} bpf_dest_t;

// use u64 to represent eth_addr.
// maintain the structure though to indicate the semantics
typedef struct eth_addr {
u8 addr[6];
u64 addr;
} eth_addr_t;

// Program table definitions for tail calls
Expand Down Expand Up @@ -115,7 +117,6 @@ static int br_common(struct __sk_buff *skb, int which_br) {
u32 tx_port_id;
bpf_dest_t *dest_p;
u32 index, *rtrif_p;
u64 mac_addr;

if (skb->tc_index == 0) {
skb->tc_index = 1;
Expand All @@ -128,11 +129,10 @@ static int br_common(struct __sk_buff *skb, int which_br) {

BEGIN(ethernet);
PROTO(ethernet) {
mac_addr = bpf_htonll(ethernet->dst);
_memcpy(dmac.addr, (u8 *)&mac_addr + 2, 6);
dmac.addr = ethernet->dst;
if (meta.prog_id != 0) {
/* send to the router */
if (dmac.addr[0] & dmac.addr[1] & dmac.addr[2] & dmac.addr[3] & dmac.addr[4] & dmac.addr[5] == 0xff) {
if (dmac.addr == 0xffffffffffffULL) {
index = 0;
if (which_br == 1)
rtrif_p = br1_rtr.lookup(&index);
Expand Down Expand Up @@ -181,8 +181,7 @@ static int br_common(struct __sk_buff *skb, int which_br) {
__u32 ifindex = *rtrif_p;
eth_addr_t smac;

mac_addr = bpf_htonll(ethernet->src);
_memcpy(smac.addr, (u8 *)&mac_addr + 2, 6);
smac.addr = ethernet->src;
if (which_br == 1)
br1_mac_ifindex.update(&smac, &ifindex);
else
Expand Down
14 changes: 3 additions & 11 deletions tests/cc/test_brb.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
from unittest import main, TestCase
import subprocess
import commands
import struct

arg1 = sys.argv.pop(1)

Expand All @@ -82,12 +83,7 @@ class Bpf_Dest(Structure):
("port_id", c_uint)]

class Eth_Addr(Structure):
_fields_ = [("addr0", c_ubyte),
("addr1", c_ubyte),
("addr2", c_ubyte),
("addr3", c_ubyte),
("addr4", c_ubyte),
("addr5", c_ubyte)]
_fields_ = [("addr", c_ulonglong)]

class TestBPFSocket(TestCase):
def setup_vm_ns(self, ns, veth_in, veth_out):
Expand Down Expand Up @@ -150,12 +146,10 @@ def get_table(self, b):
self.br1_dest = b.get_table("br1_dest", c_uint, Bpf_Dest)
self.br1_mac = b.get_table("br1_mac", Eth_Addr, c_uint)
self.br1_rtr = b.get_table("br1_rtr", c_uint, c_uint)
self.br1_mac_ifindex = b.get_table("br1_mac_ifindex", Eth_Addr, c_uint)

self.br2_dest = b.get_table("br2_dest", c_uint, Bpf_Dest)
self.br2_mac = b.get_table("br2_mac", Eth_Addr, c_uint)
self.br2_rtr = b.get_table("br2_rtr", c_uint, c_uint)
self.br2_mac_ifindex = b.get_table("br2_mac_ifindex", Eth_Addr, c_uint)

def connect_ports(self, prog_id_pem, prog_id_br, curr_pem_pid, curr_br_pid,
ip, br_dest_map, br_mac_map,
Expand All @@ -167,9 +161,7 @@ def connect_ports(self, prog_id_pem, prog_id_br, curr_pem_pid, curr_br_pid,
ifindex = ip.link_lookup(ifname=ns_eth_out)[0]
self.pem_port.update(c_uint(curr_pem_pid), c_uint(ifindex))
self.pem_ifindex.update(c_uint(ifindex), c_uint(curr_pem_pid))
mac1 = vm_mac.split(':')
mac_addr = Eth_Addr(int(mac1[0], 16), int(mac1[1], 16), int(mac1[2], 16),
int(mac1[3], 16), int(mac1[4], 16), int(mac1[5], 16))
mac_addr = Eth_Addr(struct.unpack('!Q', "\0\0" + vm_mac.replace(':', '').decode('hex'))[0])
br_mac_map.update(mac_addr, c_uint(curr_br_pid))

def attach_filter(self, ip, ifname, fd, name):
Expand Down

0 comments on commit 5604fc8

Please sign in to comment.