Last active
August 29, 2015 14:21
-
-
Save lukego/9e35183e0448c0e74750 to your computer and use it in GitHub Desktop.
Linux tap device example code based on ljsyscall
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
-- tap-test.lua -- test program based on ljsyscall test code | |
-- Reference example code for ljsyscall here: | |
-- https://github.com/justincormack/ljsyscall/blob/master/test/linux.lua | |
-- Run via Snabb Switch like this: | |
-- sudo snabb snsh tap-test.lua | |
local syscall = require("syscall") -- ljsyscall | |
-- Create a new tap device | |
local clonedev = "/dev/net/tun" | |
local fd = assert(syscall.open(clonedev, "rdwr")) | |
local ifr = syscall.t.ifreq() | |
ifr.flags = "tap" | |
assert(fd:ioctl("TUNSETIFF", ifr)) | |
-- Print out the details we get | |
local ifname = ifr.name | |
print("tunnel device: " .. ifname) | |
print() | |
print("netlink status:") | |
print(syscall.nl.interfaces()[ifname]) | |
-- Echo packets e.g. while playing around with ip/tcpdump/ping/etc | |
while true do | |
local data = fd:read() | |
print("Got packet:") | |
print(core.lib.hexdump(data)) | |
fd:write(data) | |
print("[Echoed packet back.]") | |
end |
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
$ sudo src/snabb snsh ~/tap-test.lua | |
tunnel device: tap0 | |
netlink status: | |
tap0 Link encap:Ethernet HWaddr 86:a4:7d:ce:70:f1 | |
ALL VOLATILE MULTICAST BROADCAST MTU: 1500 | |
RX packets:0 errors:0 dropped:0 | |
TX packets:0 errors:0 dropped:0 | |
Got packet: | |
00 00 08 00 01 00 5E 00 00 16 86 A4 7D CE 70 F1 08 00 46 C0 00 28 00 00 40 00 01 02 F9 EF 0A 09 00 01 E0 00 00 16 94 04 00 00 22 00 | |
F9 02 00 00 00 01 04 00 00 00 E0 00 00 FB | |
[Echoed packet back.] | |
Got packet: | |
00 00 86 DD 33 33 00 00 00 16 86 A4 7D CE 70 F1 86 DD 60 00 00 00 00 24 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 FF 02 | |
00 00 00 00 00 00 00 00 00 00 00 00 00 16 3A 00 05 02 00 00 01 00 8F 00 FD CA 00 00 00 01 04 00 00 00 FF 02 00 00 00 00 00 00 00 00 | |
00 01 FF CE 70 F1 | |
[Echoed packet back.] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment