diff options
author | Stefan Schmidt <stefan@datenfreihafen.org> | 2018-12-24 22:49:32 +0100 |
---|---|---|
committer | Stefan Schmidt <stefan@datenfreihafen.org> | 2019-04-24 20:38:58 +0200 |
commit | 11783a687d4900761e85788a5d815e81f16be5be (patch) | |
tree | 773b570af56596807447f4a6d11e12d7de2d3f50 | |
parent | 8bf662e1db1bd9015ef1b20315fb8cf73296553b (diff) | |
download | wpan-next-6lowpan-kselftest.tar.gz |
ieee802154 test progs6lowpan-kselftest
-rw-r--r-- | tools/testing/selftests/net/Makefile | 5 | ||||
-rwxr-xr-x | tools/testing/selftests/net/ieee802154.sh | 106 | ||||
-rw-r--r-- | tools/testing/selftests/net/ieee802154_rx.c | 87 | ||||
-rw-r--r-- | tools/testing/selftests/net/ieee802154_tx.c | 71 |
4 files changed, 267 insertions, 2 deletions
diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests/net/Makefile index 029d7e414d5d8..9020da18e5fff 100644 --- a/tools/testing/selftests/net/Makefile +++ b/tools/testing/selftests/net/Makefile @@ -10,8 +10,8 @@ TEST_PROGS += fib_tests.sh fib-onlink-tests.sh pmtu.sh udpgso.sh ip_defrag.sh TEST_PROGS += udpgso_bench.sh fib_rule_tests.sh msg_zerocopy.sh psock_snd.sh TEST_PROGS += udpgro_bench.sh udpgro.sh test_vxlan_under_vrf.sh reuseport_addr_any.sh TEST_PROGS += test_vxlan_fdb_changelink.sh -TEST_PROGS += ieee802154-6lowpan.sh -TEST_PROGS := ieee802154-6lowpan.sh +TEST_PROGS += ieee802154-6lowpan.sh ieee802154.sh +TEST_PROGS := ieee802154-6lowpan.sh ieee802154.sh TEST_PROGS_EXTENDED := in_netns.sh TEST_GEN_FILES = socket TEST_GEN_FILES += psock_fanout psock_tpacket msg_zerocopy reuseport_addr_any @@ -19,6 +19,7 @@ TEST_GEN_FILES += tcp_mmap tcp_inq psock_snd txring_overwrite TEST_GEN_FILES += udpgso udpgso_bench_tx udpgso_bench_rx ip_defrag TEST_GEN_PROGS = reuseport_bpf reuseport_bpf_cpu reuseport_bpf_numa TEST_GEN_PROGS += reuseport_dualstack reuseaddr_conflict tls +TEST_GEN_PROGS += ieee802154_tx ieee802154_rx KSFT_KHDR_INSTALL := 1 include ../lib.mk diff --git a/tools/testing/selftests/net/ieee802154.sh b/tools/testing/selftests/net/ieee802154.sh new file mode 100755 index 0000000000000..fbc5687330cbc --- /dev/null +++ b/tools/testing/selftests/net/ieee802154.sh @@ -0,0 +1,106 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 + +# Kselftest framework requirement - SKIP code is 4. +ksft_skip=4 + +CHANNEL=13 +PANID=0x4224 +COUNT=5 + +DEST_SHORT=0x0002 +DEST_LONG=DE:AD:BE:EF:BA:BE:00:02 + +test_setup_env () { + if [ $UID != 0 ]; then + echo "Please run ieee802154 test as root [SKIP]" + exit $ksft_skip + fi + + # Check if mac802154_hwsim is either compiled in or loaded, if not try + # to load the module + if [ ! -d /sys/module/mac802154_hwsim ]; then + if /sbin/modprobe -q mac802154_hwsim ; then + echo "Module mac802154_hwsim not found [SKIP]" + exit $ksft_skip + fi + fi + + + if [ ! hash iwpan 2>/dev/null ]; then + echo "Command iwpan not found [SKIP]" + echo "Please install the wpan-tools package for this test." + exit $ksft_skip + else + iwpan --version + fi + + if [ ! hash wpan-ping 2>/dev/null ]; then + echo "Command wpan-ping not found [SKIP]" + echo "Please install the wpan-tools package >= 0.9 for this test." + exit $ksft_skip + else + wpan-ping --version + fi +} + +test_cleanup_env () { + if /sbin/modprobe -q -r mac802154_hwsim; then + echo "Removed mac802154_hwsim module"; + fi +} + +# Setup a virtaul test bed with two ieee802154 radios (wpanX) +test_setup_hwsim () { + ip link set wpan0 down + iwpan dev wpan0 del + iwpan phy phy0 interface add wpan0 type node DE:AD:BE:EF:BA:BE:00:01 + iwpan phy phy0 set channel 0 $CHANNEL + iwpan dev wpan0 set ackreq_default 1 + iwpan dev wpan0 set pan_id $PANID + iwpan dev wpan0 set short_addr 0x0001 + ip link set wpan0 up + + ip link set wpan1 down + iwpan dev wpan1 del + iwpan phy phy1 interface add wpan1 type node DE:AD:BE:EF:BA:BE:00:02 + iwpan phy phy1 set channel 0 $CHANNEL + iwpan dev wpan1 set ackreq_default 1 + iwpan dev wpan1 set pan_id $PANID + iwpan dev wpan1 set short_addr 0x0002 + ip link set wpan1 up +} + +test_result_check () { + if [ "$?" = "0" ]; then + echo "$1 [PASS]"; + else + echo "$1 [FAILED]"; + fi +} + +test_wpan_ping () { + # Start daemon on interface wpan1 + wpan-ping -i wpan1 -d & + + echo "---- IEEE 802.15.4 packet size testing ----" + wpan-ping -a $DEST_SHORT -c $COUNT -s 5 -i wpan0 + test_result_check "ieee802154: wpan-ping: minimum wpan-ping size, short address:" + + wpan-ping -a $DEST_SHORT -c $COUNT -s 104 -i wpan0 + test_result_check "ieee802154: wpan-ping: maximum wpan-ping size, short address:" + + wpan-ping -e -a $DEST_LONG -c $COUNT -s 5 -i wpan0 + test_result_check "ieee802154: wpan-ping: minimum wpan-ping size, extended address:" + + wpan-ping -e -a $DEST_LONG -c $COUNT -s 104 -i wpan0 + test_result_check "ieee802154: wpan-ping: maximum wpan-ping size, extended address:" + killall wpan-ping +} + +#source ieee802154.local + +test_setup_env +test_setup_hwsim +#test_wpan_ping +test_cleanup_env diff --git a/tools/testing/selftests/net/ieee802154_rx.c b/tools/testing/selftests/net/ieee802154_rx.c new file mode 100644 index 0000000000000..418366e7bd88a --- /dev/null +++ b/tools/testing/selftests/net/ieee802154_rx.c @@ -0,0 +1,87 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* Copyright (C) 2018 Stefan Schmidt <stefan@datenfreihafen.org> */ + +#include <sys/types.h> +#include <sys/socket.h> +#include <stdio.h> +#include <stdint.h> +#include <unistd.h> +#include <string.h> + +#define IEEE802154_ADDR_LEN 8 +#define MAX_PACKET_LEN 127 +#define EXTENDED 1 + +enum { + IEEE802154_ADDR_NONE = 0x0, + IEEE802154_ADDR_SHORT = 0x2, + IEEE802154_ADDR_LONG = 0x3, +}; + +struct ieee802154_addr_sa { + int addr_type; + uint16_t pan_id; + union { + uint8_t hwaddr[IEEE802154_ADDR_LEN]; + uint16_t short_addr; + }; +}; + +struct sockaddr_ieee802154 { + sa_family_t family; + struct ieee802154_addr_sa addr; +}; + +int main(int argc, char *argv[]) { + int ret, sd; + struct sockaddr_ieee802154 src, dst; + unsigned char buf[MAX_PACKET_LEN + 1]; + socklen_t addrlen; + uint8_t long_addr[IEEE802154_ADDR_LEN] = {0xd6, 0x55, 0x2c, 0xd6, 0xe4, 0x1c, 0xeb, 0x57}; + + sd = socket(PF_IEEE802154, SOCK_DGRAM, 0); + if (sd < 0) { + perror("socket"); + return 1; + } + + memset(&src, 0, sizeof(src)); + src.family = AF_IEEE802154; + src.addr.pan_id = 0x4224; + +#if EXTENDED /* IEEE 802.15.4 extended address usage */ + src.addr.addr_type = IEEE802154_ADDR_LONG; + memcpy(&src.addr.hwaddr, &long_addr, IEEE802154_ADDR_LEN); +#else + src.addr.addr_type = IEEE802154_ADDR_SHORT; + src.addr.short_addr = 0x0002; +#endif + + ret = bind(sd, (struct sockaddr *)&src, sizeof(src)); + if (ret) { + perror("bind"); + close(sd); + return 1; + } + + addrlen = sizeof(dst); + + while (1) { + ret = recvfrom(sd, buf, MAX_PACKET_LEN, 0, (struct sockaddr *)&dst, &addrlen); + if (ret < 0) { + perror("recvfrom"); + continue; + } + buf[ret] = '\0'; +#if EXTENDED + printf("Received (from %s): %s\n", dst.addr.hwaddr, buf); +#else + printf("Received (from %x): %s\n", dst.addr.short_addr, buf); +#endif + } + + shutdown(sd, SHUT_RDWR); + close(sd); + return 0; +} diff --git a/tools/testing/selftests/net/ieee802154_tx.c b/tools/testing/selftests/net/ieee802154_tx.c new file mode 100644 index 0000000000000..01690a4ca9e58 --- /dev/null +++ b/tools/testing/selftests/net/ieee802154_tx.c @@ -0,0 +1,71 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* Copyright (C) 2018 Stefan Schmidt <stefan@datenfreihafen.org> */ + +#include <sys/types.h> +#include <sys/socket.h> +#include <stdio.h> +#include <stdint.h> +#include <unistd.h> +#include <string.h> + +#define IEEE802154_ADDR_LEN 8 +#define MAX_PACKET_LEN 127 + +enum { + IEEE802154_ADDR_NONE = 0x0, + IEEE802154_ADDR_SHORT = 0x2, + IEEE802154_ADDR_LONG = 0x3, +}; + +struct ieee802154_addr_sa { + int addr_type; + uint16_t pan_id; + union { + uint8_t hwaddr[IEEE802154_ADDR_LEN]; + uint16_t short_addr; + }; +}; + +struct sockaddr_ieee802154 { + sa_family_t family; + struct ieee802154_addr_sa addr; +}; + +int main(int argc, char *argv[]) { + int sd; + ssize_t len; + struct sockaddr_ieee802154 dst; + char buf[MAX_PACKET_LEN + 1]; + uint8_t long_addr[IEEE802154_ADDR_LEN] = {0xd6, 0x55, 0x2c, 0xd6, 0xe4, 0x1c, 0xeb, 0x57}; + + sd = socket(PF_IEEE802154, SOCK_DGRAM, 0); + if (sd < 0) { + perror("socket"); + return 1; + } + + memset(&dst, 0, sizeof(dst)); + dst.family = AF_IEEE802154; + dst.addr.pan_id = 0x4224; + + sprintf(buf, "Hello world from IEEE 802.15.4 socket example!"); + + dst.addr.addr_type = IEEE802154_ADDR_LONG; + memcpy(&dst.addr.hwaddr, long_addr, IEEE802154_ADDR_LEN); + len = sendto(sd, buf, strlen(buf), 0, (struct sockaddr *)&dst, sizeof(dst)); + if (len < 0) { + perror("sendto"); + } + + dst.addr.addr_type = IEEE802154_ADDR_SHORT; + dst.addr.short_addr = 0x0002; + len = sendto(sd, buf, strlen(buf), 0, (struct sockaddr *)&dst, sizeof(dst)); + if (len < 0) { + perror("sendto"); + } + + shutdown(sd, SHUT_RDWR); + close(sd); + return 0; +} |