Skip to content

Commit

Permalink
core: asyncio prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
svinota committed Jun 15, 2024
1 parent fd18ba0 commit 9e2e818
Show file tree
Hide file tree
Showing 9 changed files with 680 additions and 490 deletions.
38 changes: 38 additions & 0 deletions pyroute2/iproute/linux.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import json
import logging
import os
import time
Expand All @@ -25,6 +26,7 @@
NetlinkError,
SkipInode,
)
from pyroute2.netlink.nlsocket import IPCSocket
from pyroute2.netlink.rtnl import (
RTM_DELADDR,
RTM_DELLINK,
Expand Down Expand Up @@ -83,6 +85,7 @@
IPBatchSocket,
IPRSocket,
)
from pyroute2.netlink.rtnl.marshal import MarshalRtnl
from pyroute2.netlink.rtnl.ndtmsg import ndtmsg
from pyroute2.netlink.rtnl.nsidmsg import nsidmsg
from pyroute2.netlink.rtnl.nsinfmsg import nsinfmsg
Expand All @@ -91,6 +94,9 @@
from pyroute2.netlink.rtnl.rtmsg import rtmsg
from pyroute2.netlink.rtnl.tcmsg import plugins as tc_plugins
from pyroute2.netlink.rtnl.tcmsg import tcmsg
from pyroute2.netns import setns
from pyroute2.plan9 import Tcall
from pyroute2.plan9.server import Plan9Server
from pyroute2.requests.address import AddressFieldFilter, AddressIPRouteFilter
from pyroute2.requests.bridge import (
BridgeFieldFilter,
Expand Down Expand Up @@ -2597,6 +2603,38 @@ class IPRoute(LAB_API, RTNL_API, IPRSocket):
pass


def ipr_call(session, inode, req, response):
arg = json.loads(req['text'])
data = req['data']
if data:
arg['kwarg']['data'] = data
response['err'] = 0
ret = getattr(session.ipr, arg['call'])(*arg['argv'], **arg['kwarg'])
if isinstance(ret, bytes):
response['data'] = ret
response['text'] = ''
else:
response['data'] = b''
response['text'] = json.dumps(ret)
return response


class NetNS(RTNL_API, IPCSocket):

def __init__(self, netns):
super().__init__(target=netns)
self.marshal = MarshalRtnl()

def ipc_server(self):
setns(self.status['target'])
p9 = Plan9Server(use_socket=self.socket.server)
p9.filesystem.create('call').add_callback(Tcall, ipr_call)
p9.filesystem.create('log')
connection = p9.accept()
connection.session.ipr = IPRoute()
connection.serve()


class RawIPRoute(RTNL_API, RawIPRSocket):
'''
The same as `IPRoute`, but does not use the netlink proxy.
Expand Down
2 changes: 1 addition & 1 deletion pyroute2/netlink/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1754,7 +1754,7 @@ def ft_encode(self, offset):
else:
zs = 0
for name, fmt in self.fields:
default = self.defaults.get(name)
default = self.defaults.get(name, 0)
value = self[name] if name in self else default
if value is None:
continue
Expand Down

0 comments on commit 9e2e818

Please sign in to comment.