Brenden Blanco | 09e77f7 | 2015-06-16 00:21:57 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (c) PLUMgrid, Inc. |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License") |
| 4 | |
| 5 | from bpf import BPF |
| 6 | from pyroute2 import IPRoute |
| 7 | |
Yonghong Song | e2a2c48 | 2015-08-10 09:40:11 -0700 | [diff] [blame^] | 8 | print("Simple program to test pyroute2 IPRoute tc interface") |
Brenden Blanco | 09e77f7 | 2015-06-16 00:21:57 -0700 | [diff] [blame] | 9 | ipr = IPRoute() |
| 10 | |
| 11 | text = """ |
| 12 | int hello(struct __sk_buff *skb) { |
| 13 | return 1; |
| 14 | } |
| 15 | """ |
| 16 | |
| 17 | try: |
| 18 | b = BPF(text=text, debug=0) |
| 19 | fn = b.load_func("hello", BPF.SCHED_CLS) |
| 20 | ipr.link_create(ifname="t1a", kind="veth", peer="t1b") |
| 21 | idx = ipr.link_lookup(ifname="t1a")[0] |
| 22 | |
| 23 | ipr.tc("add", "ingress", idx, "ffff:") |
| 24 | ipr.tc("add-filter", "bpf", idx, ":1", fd=fn.fd, |
| 25 | name=fn.name, parent="ffff:", action="ok", classid=1) |
| 26 | ipr.tc("add", "sfq", idx, "1:") |
| 27 | ipr.tc("add-filter", "bpf", idx, ":1", fd=fn.fd, |
| 28 | name=fn.name, parent="1:", action="ok", classid=1) |
| 29 | finally: |
| 30 | if "idx" in locals(): ipr.link_remove(idx) |