本文整理汇总了Python中pyroute2.IPRoute.nlm_request方法的典型用法代码示例。如果您正苦于以下问题:Python IPRoute.nlm_request方法的具体用法?Python IPRoute.nlm_request怎么用?Python IPRoute.nlm_request使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyroute2.IPRoute
的用法示例。
在下文中一共展示了IPRoute.nlm_request方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _manage_plug_via_netlink
# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import nlm_request [as 别名]
def _manage_plug_via_netlink(interface_name, action='unplug'):
""" Manipulates the plug qdisc via netlink
FIXME: Once we have a modern userpace, replace this with appropriate
calls to nl-qdisc-add
"""
ip = IPRoute()
index = ip.link_lookup(ifname=interface_name)[0]
# See the linux source at include/uapi/linux/pkt_sched.h
# #define TCQ_PLUG_BUFFER 0
# #define TCQ_PLUG_RELEASE_ONE 1
# #define TCQ_PLUG_RELEASE_INDEFINITE 2
# #define TCQ_PLUG_LIMIT 3
action = {'unplug': 2, 'plug': 0}[action]
packet_limit = 10000
handle = transform_handle(PLUG_QDISC)
parent = transform_handle(PLUG_CLASS)
flags = NLM_F_REQUEST | NLM_F_ACK
command = pyroute2.netlink.rtnl.RTM_NEWQDISC
# This is a bit of magic sauce, inspired by xen's remus project
opts = struct.pack('iI', action, packet_limit)
msg = tcmsg()
msg['index'] = index
msg['handle'] = handle
msg['parent'] = parent
msg['attrs'] = [['TCA_KIND', 'plug']]
msg['attrs'].append(['TCA_OPTIONS', opts])
try:
nlm_response = ip.nlm_request(msg, msg_type=command, msg_flags=flags)
except pyroute2.netlink.NetlinkError as nle:
if nle.code == 22:
# This is an old kernel and we're talking to a qfifo, chill
log.warn('Detected a non plug qdisc, likely due to an old kernel. '
'If you wish to have zero downtime haproxy restarts, '
'upgrade your kernel. '
'Doing nothing to the SYN traffic lane...')
return
else:
raise
# As per the netlink manpage (man 7 netlink), we expect an
# acknowledgment as a NLMSG_ERROR packet with the error field being 0,
# which it looks like pyroute2 treats as None. Really we want it to be
# non negative.
if not(len(nlm_response) > 0 and
nlm_response[0]['event'] == 'NLMSG_ERROR' and
nlm_response[0]['header']['error'] is None):
raise RuntimeError(
'Had an error while communicating with netlink: {0}'.format(
nlm_response))