Skip to content

Commit 79d2840

Browse files
committed
Replace nnpy with pynng.
Signed-off-by: fruffy <fruffy@nyu.edu>
1 parent 9d41838 commit 79d2840

12 files changed

Lines changed: 76 additions & 77 deletions

File tree

.github/workflows/build.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ jobs:
5353
python3 -m pip install --upgrade pip setuptools nose2 wheel
5454
python -m pip install scapy==${{ matrix.scapy_version }}
5555
56-
bash CI/install-nanomsg.sh
57-
sudo ldconfig
58-
bash CI/install-nnpy.sh
56+
bash CI/install-pynng.sh
5957
6058
- name: Install
6159
run: |
@@ -71,5 +69,5 @@ jobs:
7169
run: |
7270
source $HOME/ptf-test-venv/bin/activate
7371
pip --verbose list
74-
python3 CI/check-nnpy.py
72+
python3 CI/check-pynng.py
7573
./CI/run_tests.sh

CI/check-nnpy.py

Lines changed: 0 additions & 17 deletions
This file was deleted.

CI/check-pynng.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python
2+
3+
import sys
4+
import time
5+
6+
import pynng
7+
8+
address = "inproc://foo"
9+
10+
pub = pynng.Pub0(listen=address)
11+
sub = pynng.Sub0(dial=address, recv_timeout=500)
12+
sub.subscribe("")
13+
14+
ok = False
15+
for _ in range(5):
16+
pub.send(b"hello, world")
17+
try:
18+
recv = sub.recv()
19+
except pynng.Timeout:
20+
time.sleep(0.05)
21+
continue
22+
if recv == b"hello, world":
23+
ok = True
24+
break
25+
26+
pub.close()
27+
sub.close()
28+
29+
if not ok:
30+
sys.exit(1)

CI/install-nnpy.sh

Lines changed: 0 additions & 8 deletions
This file was deleted.

CI/install-pynng.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
set -e
3+
python3 -m pip install --upgrade pynng

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -315,12 +315,12 @@ NICs on the host running PTF, like so:
315315

316316
### `nn`
317317

318-
We introduce a new platform, `nn`, which uses [nanomsg] (http://nanomsg.org/) to
319-
send and receive packet to the switch. We support IPC and TCP nanomsg
320-
sockets. When using this platform, you need to make sure that the Python package
321-
[nnpy] (https://github.com/nanomsg/nnpy) is installed. With `nn`, do not use
318+
We introduce a new platform, `nn`, which uses [NNG] (https://nng.nanomsg.org/)
319+
to send and receive packet to the switch. We support IPC and TCP sockets. When
320+
using this platform, you need to make sure that the Python package
321+
[pynng] (https://github.com/codypiersall/pynng) is installed. With `nn`, do not use
322322
`--interface`, instead use `--device-socket`. For each device, you need to
323-
provide a list of enabled ports and a nanomsg socket address. For example:
323+
provide a list of enabled ports and a socket address. For example:
324324

325325
--device-socket 0-{1,2,5-8}@ipc:///tmp/bmv2_packets_1.ipc
326326

ptf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -824,9 +824,9 @@ logging.info("Importing platform: " + platform_name)
824824
# TODO(antonin): put this check in platforms/nn.py ?
825825
if platform_name == "nn":
826826
try:
827-
import nnpy
827+
import pynng
828828
except:
829-
die("Cannot use 'nn' platform if nnpy package is not installed")
829+
die("Cannot use 'nn' platform if pynng package is not installed")
830830

831831
platform_mod = None
832832
try:

ptf_nn/README.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,17 @@ the host running PTF).
1010

1111
## Dependencies
1212

13-
We rely on [nanomsg](http://nanomsg.org/) (a messaging library) to forward
13+
We rely on [NNG](https://nng.nanomsg.org/) (a messaging library and modern
14+
successor to nanomsg) to forward
1415
packets between the PTF agent and the PTF test runner. You will therefore need
1516
to install the following:
1617

17-
- [nanomsg](https://github.com/nanomsg/nanomsg/releases): we recommend
18-
installing the `1.0.0` production release.
19-
- [nnpy](https://github.com/nanomsg/nnpy): these are the Python bindings for
20-
nanomsg. You may use the provided (install-nnpy.sh)[install-nnpy.sh] script
21-
to install nnpy. It will install a version of nnpy that we have tested.
18+
- [pynng](https://github.com/codypiersall/pynng): these are the Python
19+
bindings for NNG. You may use the provided
20+
[install-pynng.sh](../CI/install-pynng.sh) script to install pynng.
2221

23-
We provide a [check-nnpy.py](check-nnpy.py) script that you can run to check
24-
that nanomsg and nnpy are running properly.
22+
We provide a [check-pynng.py](../CI/check-pynng.py) script that you can run to
23+
check that pynng is running properly.
2524

2625
## Overview
2726

@@ -34,7 +33,7 @@ tester. Packets received on an interface (from the switch) will be tagged with
3433
the port number and forwarded to the PTF tester. Packets received from the PTF
3534
tester will be forwarded to the switch using the appropriate
3635
interface. Communications between the PTF tester and each agent are done over
37-
TCP using the nanomsg messaging library.
36+
TCP using the NNG messaging library.
3837

3938
## Demo
4039

@@ -64,7 +63,7 @@ runs on the PTF host. The second PTF agent runs on the remote host. For each
6463
host we need to use a separate device id (0 for the PTF host, 1 for the remote
6564
host). The "switch" is connected to the PTF host through veth0-veth1 and to the
6665
remote host through veth2-veth3. When running `ptf`, we need to use the `nn`
67-
platform and provide the nanomsg TCP address for each of the 2 devices.
66+
platform and provide the NNG TCP address for each of the 2 devices.
6867

6968
In our test, we send a packet to port 1 of device 0 and receive the exact same
7069
packet on port 1 of device 1, as expected.

ptf_nn/ptf_nn_agent.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
import socket
2929

3030
try:
31-
import nnpy
31+
import pynng
3232
except ImportError:
33-
print("Cannot find nnpy package, please install")
33+
print("Cannot find pynng package, please install")
3434
sys.exit(1)
3535
import threading
3636
import os
@@ -369,20 +369,17 @@ def __init__(self, dev, socket_addr, nn_rcv_buf=0, nn_snd_buf=0):
369369
self.daemon = True
370370
self.dev = dev
371371
self.socket_addr = socket_addr
372-
self.socket = nnpy.Socket(nnpy.AF_SP, nnpy.PAIR)
372+
self.socket = pynng.Pair0()
373373
if nn_rcv_buf != 0:
374-
self.socket.setsockopt(nnpy.SOL_SOCKET, nnpy.RCVBUF, nn_rcv_buf)
374+
self.socket.recv_buffer_size = nn_rcv_buf
375375
if nn_snd_buf != 0:
376-
self.socket.setsockopt(nnpy.SOL_SOCKET, nnpy.SNDBUF, nn_snd_buf)
377-
self.socket.bind(socket_addr)
376+
self.socket.send_buffer_size = nn_snd_buf
377+
self.socket.listen(socket_addr)
378378

379379
def forward(self, p, port):
380380
msg = struct.pack(
381381
"<iii{}s".format(len(p)), self.MSG_TYPE_PACKET_OUT, port, len(p), p
382382
)
383-
# because nnpy expects unicode when using str
384-
msg = bytearray(msg)
385-
386383
self.socket.send(msg)
387384

388385
def handle_info_req(self, port_number, info_id, msg):

ptf_nn/ptf_nn_test_nn.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#
2121
#
2222

23-
import nnpy
23+
import pynng
2424
import struct
2525
import argparse
2626

@@ -49,16 +49,14 @@ def receive(socket):
4949

5050

5151
def main():
52-
socket = nnpy.Socket(nnpy.AF_SP, nnpy.PAIR)
53-
socket.connect(args.socket)
52+
socket = pynng.Pair0()
53+
socket.dial(args.socket)
5454
if args.receive:
5555
receive(socket)
5656
else: # send one
57-
p = "ab" * 20
57+
p = b"ab" * 20
5858
port = 1
5959
msg = struct.pack("<iii{}s".format(len(p)), MSG_TYPE_PACKET_IN, port, len(p), p)
60-
# because nnpy expects unicode when using str
61-
msg = list(msg)
6260
socket.send(msg)
6361

6462

0 commit comments

Comments
 (0)