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