Commit 09e77f70 authored by Brenden Blanco's avatar Brenden Blanco

Add simple examples for using pyroute2+tc

* simple_tc shows using just IPRoute
* simple_ipdb shows using IPDB as well, slightly more complicated
 - If the exception handling is done as shown, hangs may result
 - If only IPRoute is used, this shouldn't be the case
Signed-off-by: default avatarBrenden Blanco <bblanco@plumgrid.com>
parent 00b8a403
#!/usr/bin/env python
# Copyright (c) PLUMgrid, Inc.
# Licensed under the Apache License, Version 2.0 (the "License")
# This example shows how to use pyroute2 to attach a BPF program to an
# interface. Pyroute2 does contain some quirks with regard to program
# termination, especially take care when using IPDB and leaving exceptions
# uncaught (catch them).
from bpf import BPF
from pyroute2 import IPRoute, IPDB
ipr = IPRoute()
ipdb = IPDB(nl=ipr)
text = """
int hello(struct __sk_buff *skb) {
return 1;
}
"""
try:
b = BPF(text=text, debug=0)
fn = b.load_func("hello", BPF.SCHED_CLS)
ifc = ipdb.create(ifname="t1a", kind="veth", peer="t1b").commit()
ipr.tc("add", "ingress", ifc["index"], "ffff:")
ipr.tc("add-filter", "bpf", ifc["index"], ":1", fd=fn.fd,
name=fn.name, parent="ffff:", action="ok", classid=1)
ipr.tc("add", "sfq", ifc["index"], "1:")
ipr.tc("add-filter", "bpf", ifc["index"], ":1", fd=fn.fd,
name=fn.name, parent="1:", action="ok", classid=1)
finally:
if "ifc" in locals(): ifc.remove().commit()
ipdb.release()
#!/usr/bin/env python
# Copyright (c) PLUMgrid, Inc.
# Licensed under the Apache License, Version 2.0 (the "License")
from bpf import BPF
from pyroute2 import IPRoute
ipr = IPRoute()
text = """
int hello(struct __sk_buff *skb) {
return 1;
}
"""
try:
b = BPF(text=text, debug=0)
fn = b.load_func("hello", BPF.SCHED_CLS)
ipr.link_create(ifname="t1a", kind="veth", peer="t1b")
idx = ipr.link_lookup(ifname="t1a")[0]
ipr.tc("add", "ingress", idx, "ffff:")
ipr.tc("add-filter", "bpf", idx, ":1", fd=fn.fd,
name=fn.name, parent="ffff:", action="ok", classid=1)
ipr.tc("add", "sfq", idx, "1:")
ipr.tc("add-filter", "bpf", idx, ":1", fd=fn.fd,
name=fn.name, parent="1:", action="ok", classid=1)
finally:
if "idx" in locals(): ipr.link_remove(idx)
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