Commit 4d15420a authored by 4ast's avatar 4ast

Merge pull request #71 from iovisor/bblanco_dev

Rework vlan example to be 1:1, no mac learning
parents 439e53ca 6bf723dc
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
# Copyright (c) PLUMgrid, Inc. # Copyright (c) PLUMgrid, Inc.
# Licensed under the Apache License, Version 2.0 (the "License") # Licensed under the Apache License, Version 2.0 (the "License")
# run in project directory with: # run in project examples directory with:
# sudo bash -c "PYTHONPATH=$PWD/src LD_LIBRARY_PATH=$PWD/build/src/cc examples/hello_world.py" # sudo ./hello_world.py"
from bpf import BPF from bpf import BPF
from subprocess import call from subprocess import call
......
...@@ -10,7 +10,7 @@ struct ifindex_leaf_t { ...@@ -10,7 +10,7 @@ struct ifindex_leaf_t {
}; };
// redirect based on mac -> out_ifindex (auto-learning) // redirect based on mac -> out_ifindex (auto-learning)
BPF_TABLE("hash", u64, struct ifindex_leaf_t, egress, 4096); BPF_TABLE("hash", int, struct ifindex_leaf_t, egress, 4096);
// redirect based on mac -> out_ifindex (config-driven) // redirect based on mac -> out_ifindex (config-driven)
BPF_TABLE("hash", u64, struct ifindex_leaf_t, ingress, 4096); BPF_TABLE("hash", u64, struct ifindex_leaf_t, ingress, 4096);
...@@ -24,8 +24,9 @@ int handle_phys2virt(struct __sk_buff *skb) { ...@@ -24,8 +24,9 @@ int handle_phys2virt(struct __sk_buff *skb) {
lock_xadd(&leaf->tx_pkts, 1); lock_xadd(&leaf->tx_pkts, 1);
lock_xadd(&leaf->tx_bytes, skb->len); lock_xadd(&leaf->tx_bytes, skb->len);
// auto-program reverse direction table // auto-program reverse direction table
int out_ifindex = leaf->out_ifindex;
struct ifindex_leaf_t zleaf = {0}; struct ifindex_leaf_t zleaf = {0};
struct ifindex_leaf_t *out_leaf = egress.lookup_or_init(&src_mac, &zleaf); struct ifindex_leaf_t *out_leaf = egress.lookup_or_init(&out_ifindex, &zleaf);
// relearn when mac moves ifindex // relearn when mac moves ifindex
if (out_leaf->out_ifindex != skb->ifindex) if (out_leaf->out_ifindex != skb->ifindex)
out_leaf->out_ifindex = skb->ifindex; out_leaf->out_ifindex = skb->ifindex;
...@@ -39,8 +40,8 @@ EOP: ...@@ -39,8 +40,8 @@ EOP:
int handle_virt2phys(struct __sk_buff *skb) { int handle_virt2phys(struct __sk_buff *skb) {
BEGIN(ethernet); BEGIN(ethernet);
PROTO(ethernet) { PROTO(ethernet) {
u64 dst_mac = ethernet->dst; int src_ifindex = skb->ifindex;
struct ifindex_leaf_t *leaf = egress.lookup(&dst_mac); struct ifindex_leaf_t *leaf = egress.lookup(&src_ifindex);
if (leaf) { if (leaf) {
lock_xadd(&leaf->tx_pkts, 1); lock_xadd(&leaf->tx_pkts, 1);
lock_xadd(&leaf->tx_bytes, skb->len); lock_xadd(&leaf->tx_bytes, skb->len);
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
# switch | subinterface | # switch | subinterface |
from bpf import BPF from bpf import BPF
from builtins import input
from ctypes import c_uint, c_int, c_ulonglong, Structure from ctypes import c_uint, c_int, c_ulonglong, Structure
from pyroute2 import IPRoute, NetNS, IPDB, NSPopen from pyroute2 import IPRoute, NetNS, IPDB, NSPopen
from random import shuffle from random import shuffle
...@@ -34,8 +35,7 @@ import sys ...@@ -34,8 +35,7 @@ import sys
ipr = IPRoute() ipr = IPRoute()
ipdb = IPDB(nl=ipr) ipdb = IPDB(nl=ipr)
num_workers = 3 num_clients = 3
num_clients = 9
num_vlans = 16 num_vlans = 16
# load the bpf program # load the bpf program
...@@ -52,7 +52,7 @@ class VlanSimulation(Simulation): ...@@ -52,7 +52,7 @@ class VlanSimulation(Simulation):
def start(self): def start(self):
# start identical workers each in a namespace # start identical workers each in a namespace
for i in range(0, num_workers): for i in range(0, num_clients):
httpmod = ("SimpleHTTPServer" if sys.version_info[0] < 3 httpmod = ("SimpleHTTPServer" if sys.version_info[0] < 3
else "http.server") else "http.server")
cmd = ["python", "-m", httpmod, "80"] cmd = ["python", "-m", httpmod, "80"]
...@@ -84,32 +84,27 @@ class VlanSimulation(Simulation): ...@@ -84,32 +84,27 @@ class VlanSimulation(Simulation):
# allocate vlans randomly # allocate vlans randomly
available_vlans = [i for i in range(2, 2 + num_vlans)] available_vlans = [i for i in range(2, 2 + num_vlans)]
shuffle(available_vlans) shuffle(available_vlans)
available_ips = [[i for i in range(100, 105)] for i in range(0, num_workers)] available_ips = [[i for i in range(100, 105)] for i in range(0, num_clients)]
# these are simulations of physical clients # these are simulations of physical clients
for i in range(0, num_clients): for i in range(0, num_clients):
worker_i = i % num_workers
ipaddr = "172.16.1.%d" % available_ips[worker_i].pop(0)
macaddr = ("02:00:00:%.2x:%.2x:%.2x" % macaddr = ("02:00:00:%.2x:%.2x:%.2x" %
((i >> 16) & 0xff, (i >> 8) & 0xff, i & 0xff)) ((i >> 16) & 0xff, (i >> 8) & 0xff, i & 0xff))
# program arp manually
p = NSPopen("worker%d" % worker_i, ["arp", "-s", ipaddr, macaddr])
p.communicate(); p.wait(); p.release()
# assign this client to the given worker # assign this client to the given worker
idx = self.ipdb.interfaces["worker%da" % worker_i]["index"] idx = self.ipdb.interfaces["worker%da" % i]["index"]
mac = int(macaddr.replace(":", ""), 16) mac = int(macaddr.replace(":", ""), 16)
ingress[ingress.Key(mac)] = ingress.Leaf(idx, 0, 0) ingress[ingress.Key(mac)] = ingress.Leaf(idx, 0, 0)
# test traffic with curl loop # test traffic with curl loop
cmd = ["bash", "-c", cmd = ["bash", "-c",
"for i in {1..8}; do curl 172.16.1.5 -o /dev/null; sleep 1; done"] "for i in {1..8}; do curl 172.16.1.5 -o /dev/null; sleep 1; done"]
br_ifc = self.ipdb.create(ifname="br100.%d" % i, kind="vlan", link=br100, br_ifc = self.ipdb.create(ifname="br100.%d" % i, kind="vlan",
link=br100,
vlan_id=available_vlans.pop(0)).commit() vlan_id=available_vlans.pop(0)).commit()
(out_ifc, in_ifc) = self._create_ns("client%d" % i, in_ifc=br_ifc, (out_ifc, in_ifc) = self._create_ns("client%d" % i, in_ifc=br_ifc,
ipaddr=ipaddr + "/24", macaddr=macaddr, ipaddr="172.16.1.100/24",
cmd=cmd)[1:3] macaddr=macaddr, cmd=cmd)[1:3]
try: try:
sim = VlanSimulation(ipdb) sim = VlanSimulation(ipdb)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment