本文整理汇总了Python中pyroute2.IPRoute.tc方法的典型用法代码示例。如果您正苦于以下问题:Python IPRoute.tc方法的具体用法?Python IPRoute.tc怎么用?Python IPRoute.tc使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyroute2.IPRoute
的用法示例。
在下文中一共展示了IPRoute.tc方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: rv_manager
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import tc [as 别名]
class rv_manager(object):
def __init__(self):
self.ipr = IPRoute()
self.dataplane = BPF(src_file="core/rv_manager/rv_manager.c")
# Loading Tables from dp
self.next = self.dataplane.get_table("next_hop")
self.ifc2vi = self.dataplane.get_table("rvm_ifc2vi")
self.vi2ifc = self.dataplane.get_table("rvm_vi2ifc")
# Loading Functions from db
self.func_phy2virt = self.dataplane.load_func(
"rvm_function_p2v", BPF.SCHED_CLS)
self.func_virt2phy = self.dataplane.load_func(
"rvm_function_v2p", BPF.SCHED_CLS)
def set_next_hop(self, next_vnf):
self.next[self.next.Key(0)] = self.next.Leaf(next_vnf)
def get_fd(self):
return self.func_virt2phy.fd
def set_bpf_ingress(self, ifc_index, func):
self.ipr.tc("add", "ingress", ifc_index, "ffff:")
self.ipr.tc("add-filter", "bpf", ifc_index, ":1", fd=func.fd,
name=func.name, parent="ffff:", action="drop", classid=1)
def add_new_workload(self, phy_iface_index, virt_iface_index):
self.ifc2vi[self.ifc2vi.Key(phy_iface_index)] = self.ifc2vi.Leaf(
virt_iface_index)
self.vi2ifc[self.vi2ifc.Key(virt_iface_index)] = self.vi2ifc.Leaf(
phy_iface_index)
self.set_bpf_ingress(phy_iface_index, self.func_phy2virt)
示例2: setUp
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import tc [as 别名]
def setUp(self):
b = BPF(src_file=arg1, debug=0)
ether_fn = b.load_func("parse_ether", BPF.SCHED_CLS)
arp_fn = b.load_func("parse_arp", BPF.SCHED_CLS)
ip_fn = b.load_func("parse_ip", BPF.SCHED_CLS)
eop_fn = b.load_func("eop", BPF.SCHED_CLS)
ip = IPRoute()
ifindex = ip.link_lookup(ifname="eth0")[0]
ip.tc("add", "sfq", ifindex, "1:")
ip.tc("add-filter", "bpf", ifindex, ":1", fd=ether_fn.fd,
name=ether_fn.name, parent="1:", action="ok", classid=1)
self.jump = b.get_table("jump", c_int, c_int)
self.jump[c_int(S_ARP)] = c_int(arp_fn.fd)
self.jump[c_int(S_IP)] = c_int(ip_fn.fd)
self.jump[c_int(S_EOP)] = c_int(eop_fn.fd)
self.stats = b.get_table("stats", c_int, c_ulonglong)
示例3: setUp
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import tc [as 别名]
def setUp(self):
b = BPF(arg1, arg2, debug=0)
fn = b.load_func("on_packet", BPF.SCHED_CLS)
ip = IPRoute()
ifindex = ip.link_lookup(ifname="eth0")[0]
# set up a network to change the flow:
# outside | inside
# 172.16.1.1 - 172.16.1.2 | 192.168.1.1 - 192.16.1.2
ip.addr("del", index=ifindex, address="172.16.1.2", mask=24)
ip.addr("add", index=ifindex, address="192.168.1.2", mask=24)
# add an ingress and egress qdisc
ip.tc("add", "ingress", ifindex, "ffff:")
ip.tc("add", "sfq", ifindex, "1:")
# add same program to both ingress/egress, so pkt is translated in both directions
ip.tc("add-filter", "bpf", ifindex, ":1", fd=fn.fd, name=fn.name, parent="ffff:", action="ok", classid=1)
ip.tc("add-filter", "bpf", ifindex, ":2", fd=fn.fd, name=fn.name, parent="1:", action="ok", classid=1)
self.xlate = b.get_table("xlate")
示例4: setUp
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import tc [as 别名]
def setUp(self):
b = BPF(arg1, arg2, debug=0)
fn = b.load_func("on_packet", BPF.SCHED_ACT)
ip = IPRoute()
ifindex = ip.link_lookup(ifname="eth0")[0]
# set up a network to change the flow:
# outside | inside
# 172.16.1.1 - 172.16.1.2 | 192.168.1.1 - 192.16.1.2
ip.addr("del", index=ifindex, address="172.16.1.2", mask=24)
ip.addr("add", index=ifindex, address="192.168.1.2", mask=24)
# add an ingress and egress qdisc
ip.tc("add", "ingress", ifindex, "ffff:")
ip.tc("add", "sfq", ifindex, "1:")
# add same program to both ingress/egress, so pkt is translated in both directions
action = {"kind": "bpf", "fd": fn.fd, "name": fn.name, "action": "ok"}
ip.tc("add-filter", "u32", ifindex, ":1", parent="ffff:", action=[action],
protocol=protocols.ETH_P_ALL, classid=1, target=0x10002, keys=['0x0/0x0+0'])
ip.tc("add-filter", "u32", ifindex, ":2", parent="1:", action=[action],
protocol=protocols.ETH_P_ALL, classid=1, target=0x10002, keys=['0x0/0x0+0'])
self.xlate = b.get_table("xlate")
示例5: print
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import tc [as 别名]
print("Compiling and loading BPF program")
b = BPF(src_file="./bpf.c", debug=0)
fn = b.load_func("ebpf_filter", BPF.SCHED_CLS)
print("BPF program loaded")
print("Discovering tables")
routing_tbl = b.get_table("routing")
routing_miss_tbl = b.get_table("ebpf_routing_miss")
print("Hooking up BPF classifiers using TC")
eth1_idx = ipr.link_lookup(ifname="eth1")[0]
ipr.tc("add", "ingress", eth1_idx, "ffff:")
ipr.tc("add-filter", "bpf", eth1_idx, ":1", fd=fn.fd,
name=fn.name, parent="ffff:", action="ok", classid=1)
eth3_idx = ipr.link_lookup(ifname="eth3")[0]
ipr.tc("add", "ingress", eth3_idx, "ffff:")
ipr.tc("add-filter", "bpf", eth3_idx, ":1", fd=fn.fd,
name=fn.name, parent="ffff:", action="ok", classid=1)
class Forward(ctypes.Structure):
_fields_ = [("port", ctypes.c_ushort)]
class Nop(ctypes.Structure):
_fields_ = []
示例6: IPRoute
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import tc [as 别名]
# various linux ip operations
ipr = IPRoute()
ipdb = IPDB(nl=ipr)
# load the bpf program
bpf = BPF(src_file="traffic_counter.c", debug=0)
veth_tx_fn = bpf.load_func("veth_tx", BPF.SCHED_CLS)
veth_rx_fn = bpf.load_func("veth_rx", BPF.SCHED_CLS)
state = bpf.get_table("state")
ifc = ipdb.interfaces.eth1
ipr.tc("add", "ingress", ifc.index, "ffff:")
ipr.tc("add-filter", "bpf", ifc.index, ":1", fd=veth_rx_fn.fd,
name=veth_rx_fn.name, parent="ffff:", action="ok", classid=1)
ipr.tc("add", "sfq", ifc.index, "1:")
ipr.tc("add-filter", "bpf", ifc.index, ":1", fd=veth_tx_fn.fd,
name=veth_tx_fn.name, parent="1:", action="ok", classid=1)
state = bpf.get_table("state")
while True:
print ("dump: ")
for k, v in state.items():
out = "Count: " + "tx(%s, %s B), rx(%s, %s B)" % (v.tx_pkts, v.tx_bytes, v.rx_pkts, v.rx_bytes)
# print("Key %d IP: %s: " % k.id, str(IPAddress(k.src_ip)) )
sys.stdout.write( '%s' % out )
sys.stdout.flush()
示例7: IPRoute
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import tc [as 别名]
HANDLE_MAX = (2 ** 16) - 1
if __name__ == '__main__':
ipr = IPRoute()
tcMgr = TrafficControl()
wlan0 = tcMgr.getInterface('wlan0')
ifindex = wlan0.getIndex()
operation = "del"
if len(sys.argv) > 1:
operation = str(sys.argv[1])
if operation == "add":
wlan0.clean()
ipr.tc("add", "prio", ifindex, 0x10000, bands=5)
ipr.tc("add", "pfifo", ifindex, 0x20000, parent=0x10001, limit=135)
ipr.tc("add", "bfifo", ifindex, 0x30000, parent=0x10002, limit=150000)
ipr.tc("add", "tbf", ifindex, 0x40000, parent=0x10003, rate=10000, burst=20000, limit=1000)
mark = 10
extra_args = {}
ipr.tc("add-filter", 'fw', ifindex, mark, parent=0x10000, protocol=ETH_P_IP, prio=PRIO, \
classid=0x30000, **extra_args)
elif operation == "change":
flags = NLM_F_REQUEST | NLM_F_ACK
ipr.tc((RTM_NEWQDISC, flags), "tbf", ifindex, 0x40000, parent=0x10003, rate=60000, burst=50000, limit=5000)
#tc qdisc change dev eth0 handle 4: tbf rate 2048Kbit burst 20Kb limit 10000
else:
wlan0.clean()
示例8: bpf_trace_printk
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import tc [as 别名]
unsigned long long old_src_mac = ethernet->src;
unsigned long long old_dst_mac = ethernet->dst;
ethernet->src = old_dst_mac;
ethernet->dst = old_src_mac;
bpf_trace_printk("Replying to ICMP echo\\n");
int ret = bpf_clone_redirect(skb, skb->ifindex,0 /*For Egress */);
return 1;
}
"""
ipr = IPRoute()
ipdb = IPDB(nl=ipr)
ifc = ipdb.interfaces.eth0
b = BPF(text=prog)
pbr = b.load_func("ping_block_reply", BPF.SCHED_CLS)
ipr.tc("add", "ingress", ifc.index, "ffff:")
ipr.tc("add-filter", "bpf", ifc.index, ":1", fd=pbr.fd,
name=pbr.name, parent="ffff:", action="drop", classid=1)
try:
print "All Ready..."
b.trace_print()
except KeyboardInterrupt:
print "Ending Demo..."
finally:
ipr.tc("del","ingress",ifc.index,"ffff:")
ipdb.release()
示例9: bpf_redirect
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import tc [as 别名]
ethernet->dst = old_src_mac;
u64 ret = bpf_redirect(skb->ifindex, 0 /*For Egress */);
/*
This output to the kernel trace_pipe which can also be read by:
cat /sys/kernel/debug/tracing/trace_pipe
*/
bpf_trace_printk("ICMP_SEQ: %u\\n", icmp->seq);
return TC_ACT_REDIRECT;
}
"""
ipr = IPRoute()
ipdb = IPDB(nl=ipr)
ifc = ipdb.interfaces.eth0
b = BPF(text=prog)
pr = b.load_func("ping_reply", BPF.SCHED_ACT)
ipr.tc("add", "ingress", ifc.index, "ffff:")
action = {"kind": "bpf", "fd": pr.fd, "name": pr.name, "action": "ok"}
ipr.tc("add-filter", "u32", ifc.index, ":1", parent="ffff:", action=[action],
protocol=protocols.ETH_P_ALL, classid=1, target=0x10000, keys=['0x0/0x0+0'])
try:
print "All Ready..."
b.trace_print()
except KeyboardInterrupt:
print "Ending Demo..."
finally:
ipr.tc("del", "ingress", ifc.index, "ffff:")
ipdb.release()
示例10: AtcdLinuxShaper
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import tc [as 别名]
class AtcdLinuxShaper(AtcdThriftHandlerTask):
ID_MANAGER_ID_MIN = HANDLE_MIN
ID_MANAGER_ID_MAX = HANDLE_MAX
def initTask(self):
self.ipr = IPRoute()
super(AtcdLinuxShaper, self).initTask()
def stop(self):
self._release_ipr()
def _release_ipr(self):
self.ipr.close()
def _links_lookup(self):
try:
self.lan['id'] = self.ipr.link_lookup(ifname=self.lan_name)[0]
self.wan['id'] = self.ipr.link_lookup(ifname=self.wan_name)[0]
except IndexError:
self._release_ipr()
msg = 'One of the following interfaces does not exist:' \
' {0}, {1}'.format(self.lan_name, self.wan_name)
self.logger.critical(msg)
raise Exception(msg)
def initialize_shaping_system(self):
"""Initialize Iptables and TC subsystems
Only call once as this will FLUSH all current
shapings...
"""
self.logger.info("Calling initialize_shaping_system")
self._initialize_iptables()
self._initialize_tc()
def _initialize_iptables(self):
"""Initialize IPTables by flushing all rules in FORWARD chain
from mangle table.
"""
cmd = "{0} -t mangle -F FORWARD".format(self.iptables)
self.run_cmd(cmd)
def _initialize_tc_for_interface(self, eth):
"""Initialize TC on a given interface.
If an exception is thrown, it will be forwarded to the main loop
unless it can be ignored.
Args:
eth: the interface to flush TC on.
Raises:
NetlinkError: An error occured initializing TC subsystem.
Exception: Any other exception thrown during initialization.
"""
idx = 0x10000
eth_name = eth['name']
eth_id = eth['id']
try:
self.logger.info("deleting root QDisc on {0}".format(eth_name))
self.ipr.tc(RTM_DELQDISC, None, eth_id, 0, parent=TC_H_ROOT)
except Exception as e:
# a (2, 'No such file or directory') can be thrown if there is
# nothing to delete. Ignore such error, return the error otherwise
if isinstance(e, NetlinkError) and e.code == 2:
self.logger.warning(
"could not delete root QDisc. There might "
"have been nothing to delete")
else:
self.logger.exception(
'Initializing root Qdisc for {0}'.format(eth_name)
)
raise
try:
self.logger.info("setting root qdisc on {0}".format(eth_name))
self.ipr.tc(RTM_NEWQDISC, "htb", eth_id, idx, default=0)
except Exception as e:
self.logger.exception(
'Setting root Qdisc for {0}'.format(eth_name)
)
raise
return TrafficControlRc(code=ReturnCode.OK)
def _initialize_tc(self):
"""Initialize TC root qdisc on both LAN and WAN interface.
"""
for netif in [self.lan, self.wan]:
self._initialize_tc_for_interface(netif)
def _unset_htb_class(self, mark, eth):
"""Given a mark and an interface, unset the HTB class.
Args:
mark: The mark based on which we delete the class.
eth: The interface on which to delete that class id.
Returns:
A TrafficControlRc containing information on success/failure.
#.........这里部分代码省略.........