-
-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rtnl: Merge pull request #1175 from svinota/rtnl-probe
Add probe functionaly Bug-Url: #1175
- Loading branch information
Showing
13 changed files
with
368 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.. ndbprobes: | ||
Network probes | ||
============== | ||
|
||
.. automodule:: pyroute2.ndb.objects.probe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
ndb_interfaces | ||
ndb_addresses | ||
ndb_routes | ||
ndb_probes | ||
ndb_schema | ||
ndb_sources | ||
ndb_debug | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
''' | ||
Run a network probe | ||
=================== | ||
A successful network probe neither creates real network objects | ||
like interfaces or addresses, nor database records. The only | ||
important thing it does -- it raises no exception. | ||
On the contrary, an unsuccessful network probe raises a | ||
`NetlinkError` exception, cancelling the whole transaction. | ||
A network probe is always run from the corresponding netlink | ||
target: a local system, a remote system, a network namespace, | ||
a container. | ||
An example scenario: | ||
* target alpha, set up eth0 10.0.0.2/24 | ||
* target beta, set up eth0 10.0.0.4/24 | ||
* ping 10.0.0.4 (beta) from target alpha | ||
* ping 10.0.0.2 (alpha) from target beta | ||
The code below sets up the addresses and checks ICMP responses. If | ||
any step fails, the whole transaction will be rolled back automatically:: | ||
with NDB(log='debug') as ndb: | ||
ndb.sources.add(kind='remote', hostname='alpha', username='root') | ||
ndb.sources.add(kind='remote', hostname='beta', username='root') | ||
with ndb.begin() as trx: | ||
trx.push( | ||
(ndb | ||
.interfaces[{'target': 'alpha', 'ifname': 'eth0'}] | ||
.set(state='up') | ||
.add_ip(address='10.0.0.2', prefixlen=24) | ||
), | ||
(ndb | ||
.interfaces[{'target': 'beta', 'ifname': 'eth0'}] | ||
.set(state='up') | ||
.add_ip(address='10.0.0.4', prefixlen=24) | ||
), | ||
(ndb | ||
.probes | ||
.create(target='alpha', kind='ping', dst='10.0.0.4') | ||
), | ||
(ndb | ||
.probes | ||
.create(target='beta', kind='ping', dst='10.0.0.2') | ||
), | ||
) | ||
''' | ||
|
||
from pyroute2.netlink.rtnl.probe_msg import probe_msg | ||
|
||
from ..objects import RTNL_Object | ||
|
||
|
||
def load_probe_msg(schema, target, event): | ||
pass | ||
|
||
|
||
schema = probe_msg.sql_schema().unique_index() | ||
init = { | ||
'specs': [['probes', schema]], | ||
'classes': [['probes', probe_msg]], | ||
'event_map': {probe_msg: [load_probe_msg]}, | ||
} | ||
|
||
|
||
class Probe(RTNL_Object): | ||
|
||
table = 'probes' | ||
msg_class = probe_msg | ||
api = 'probe' | ||
|
||
def __init__(self, *argv, **kwarg): | ||
kwarg['iclass'] = probe_msg | ||
self.event_map = {probe_msg: 'load_probe_msg'} | ||
super().__init__(*argv, **kwarg) | ||
|
||
def check(self): | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import errno | ||
import shutil | ||
import subprocess | ||
|
||
from pyroute2.netlink import nlmsg | ||
from pyroute2.netlink.exceptions import NetlinkError | ||
|
||
|
||
class probe_msg(nlmsg): | ||
''' | ||
Fake message type to represent network probe info. | ||
This is a prototype, the NLA layout is subject to change without | ||
notification. | ||
''' | ||
|
||
__slots__ = () | ||
prefix = 'PROBE_' | ||
|
||
fields = (('family', 'B'), ('proto', 'B'), ('port', 'H'), ('dst_len', 'I')) | ||
|
||
nla_map = ( | ||
('PROBE_UNSPEC', 'none'), | ||
('PROBE_KIND', 'asciiz'), | ||
('PROBE_STDOUT', 'asciiz'), | ||
('PROBE_STDERR', 'asciiz'), | ||
('PROBE_SRC', 'target'), | ||
('PROBE_DST', 'target'), | ||
('PROBE_NUM', 'uint8'), | ||
('PROBE_TIMEOUT', 'uint8'), | ||
) | ||
|
||
|
||
def proxy_newprobe(msg, nl): | ||
num = msg.get('num') | ||
timeout = msg.get('timeout') | ||
dst = msg.get('dst') | ||
kind = msg.get('kind') | ||
|
||
if kind.endswith('ping'): | ||
args = [ | ||
shutil.which(kind), | ||
'-c', | ||
f'{num}', | ||
'-W', | ||
f'{timeout}', | ||
f'{dst}', | ||
] | ||
if args[0] is None: | ||
raise NetlinkError(errno.ENOENT, 'probe not found') | ||
|
||
process = subprocess.Popen( | ||
args, stdout=subprocess.PIPE, stderr=subprocess.PIPE | ||
) | ||
try: | ||
out, err = process.communicate(timeout=timeout) | ||
if out: | ||
msg['attrs'].append(['PROBE_STDOUT', out]) | ||
if err: | ||
msg['attrs'].append(['PROBE_STDERR', err]) | ||
except subprocess.TimeoutExpired: | ||
process.terminate() | ||
raise NetlinkError(errno.ETIMEDOUT, 'timeout expired') | ||
finally: | ||
process.stdout.close() | ||
process.stderr.close() | ||
return_code = process.wait() | ||
if return_code != 0: | ||
raise NetlinkError(errno.EHOSTUNREACH, 'probe failed') | ||
else: | ||
raise NetlinkError(errno.ENOTSUP, 'probe type not supported') | ||
msg.reset() | ||
msg.encode() | ||
return {'verdict': 'return', 'data': msg.data} |
Oops, something went wrong.