We're sorry but this page doesn't work properly without JavaScript enabled. Please enable it to continue.
Feedback

A high level language for low level code

00:00

Formal Metadata

Title
A high level language for low level code
Alternative Title
A high level language for low level code
Using Lua to script the Linux kernel ABI
Title of Series
Number of Parts
90
Author
License
CC Attribution 2.0 Belgium:
You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal purpose as long as the work is attributed to the author in the manner specified by the author or licensor.
Identifiers
Publisher
Release Date
Language

Content Metadata

Subject Area
Genre
Abstract
Controlling Linux from a scripting language offers many advantages, in order to learn about how the operating system works, to build embedded systems and test environments and other uses. The ljsyscall project is a project that is working towards this aim using the Lua programming language to implement the Linux kernel ABI. Linux distributions do a very good job of "just working" out of the box, and hiding what actually goes on, in a complex set of largely compiled code. The original use case to develop ljsyscall came from trying to build small Linux container, virtualised or embedded systems that only needed to do one small thing, where a whole Linux distribution was overkill. It is also easy to understand what is happening with a scripting language as you can explore it in a REPL, see all the source, and run code easily with no recompile cycle. The code is also easier to understand than C for many people, with simpler string handling, garbage collection, and the provision of higher level data types as abstractions, eg for network interfaces. This makes it a good learning tool for what operating systems actually do. The code uses the LuaJIT foreign function interface (ffi) which makes it easy to understand as the Lua to C interface is very simple, as well as being very fast as it is just-in-time compiled giving similar performance to native C code. There are extensive tests, and the code works on x86, AMD64 and ARM processors so far. The code works and already exposes a lot of functionality, including system calls, signals, processes, containers, network interfaces and routing. It is like the busybox application in a scripting language. There is more to implement, such as support for more parts of the netlink protocol. It is being used in some projects now, but a wider audience could help contribute and improve it. You can use it like busybox, or for simple applications, or exploring the system. All code is MIT licensed.
Computer animationLecture/Conference
MultiplicationBootingArmData structureInheritance (object-oriented programming)Scripting languageJust-in-Time-CompilerQuicksortPhysical systemSocket-SchnittstelleBitData structureGastropod shellSystem callTerm (mathematics)Interface (computing)Kernel (computing)Logical constantInfinityPoint (geometry)Electronic mailing listBootingProgramming languageSource codeComputer programmingSoftwareMetropolitan area networkProcess (computing)1 (number)Goodness of fitMultiplication signNumberImplementationFunctional programmingProjective planeWeb pageStrut
ArmSource codeInterface (computing)High-level programming languageSoftwareMacro (computer science)Computer architectureIP addressDifferent (Kate Ryan album)1 (number)Goodness of fitLink (knot theory)Software bugLogical constantRoutingWeightBitPowerPCLecture/Conference
Physical systemSet (mathematics)Interface (computing)MIDIMach's principleObject-oriented programmingAddress spaceConfiguration spaceQuicksortScripting languageInformationSocket-SchnittstelleProgrammer (hardware)IP addressLetterpress printingLink (knot theory)Set (mathematics)FlagSoftware testingInterface (computing)Computer fileNetwork socket32-bitDifferent (Kate Ryan album)BitEvent horizonKeyboard shortcutString (computer science)Positional notationLibrary (computing)RootkitNP-hardSoftwareKernel (computing)Escape characterLipschitz-StetigkeitBefehlsprozessorRoutingRevision controlXML
Interface (computing)Computer programmingProgrammer (hardware)Graph (mathematics)System callMultiplication signSoftware testingStatisticsFunctional programmingLevel (video gaming)Software repositoryBitSampling (statistics)Broadcast programmingSoftwareComputer fileHigh-level programming languageIP addressString (computer science)Suite (music)Link (knot theory)Complete metric spaceIntegrated development environmentServer (computing)CodeProgramming languageSocket-SchnittstelleWeb 2.0Linear programmingPhysical systemCASE <Informatik>Operator (mathematics)Position operatorKernel (computing)Point (geometry)Revision controlFitness functionState of matterTable (information)Data structureRadon transformType theoryPrototypeReal number
Host Identity ProtocolMereologyPhysical systemBitProgramming languageTable (information)Link (knot theory)Medical imagingKernel (computing)Functional programmingInformation securityWeightProjective planeInterface (computing)Coma BerenicesXML
Repository (publishing)Programming languageInterface (computing)Lecture/Conference
Programming languageCodeSoftware repositoryCodeTwitterEmailJSONXMLUML
CodeComputer architectureProjective planeProcess (computing)MereologyQuicksortProgramming languageNeuroinformatikDifferent (Kate Ryan album)Interface (computing)BitLevel (video gaming)Type theoryCASE <Informatik>Physical systemSystem callRun time (program lifecycle phase)Execution unitLecture/Conference
Transcript: English(auto-generated)
Hi, thanks very much. It's not yeah, it's not exactly saying that was a really fascinating talk. I did go to that one when it was on and and That works carrying on it's quite interesting, but this is actually a stuff in user space, but for doing kind of interesting things, so When I started using Linux a very long time ago
I started poking around to find out how it worked because that was the great thing about Linux was that you it was All there and open you could find out how things worked and I just started with in it tab and how the system booted and started as a sort of starting point and tried to follow everything through and You could kind of do it there was some shell scripts and stuff like that and as long as you get the source code
of things and look at it and try and understand it, but that's how I started learning about Linux all those years ago and Recently I've been using Linux containers to do lots of things and for running sort of little processes that are isolated and doing stuff And there's this sort of assumption with the containers stuff that you want to run a whole Linux distro in each container Which gets really complicated and messy and I thought was there some sort of simpler way
I could just you know make something really simple just to run one program in a container or with a bit of networking and so on So I had this kind of dumb idea that you know Why not just make some simple scripts that are just enough to boot a simple system that can do some bit of networking
Basically start a few programs and basic stuff. I mean how much work could it be to make a little script to do this? I wouldn't have much idea so I thought I'd give it a go Obviously it turned out to be a bit more work than I thought and it's not finished, but I picked lure which is a program language. I'm quite fond of it's really small and simple
It's about a tenth of the size of Ruby in terms of the binary and It's kind of like JavaScript in the prototypal inheritance quite nice to use and I picked Lua JIT in particular, which is a JIT compiled implementation that is
Very fast and has really really nice really easy to use interfaces to see which is good because the kernel interfaces are all C basically And this foreign function interface FFI Works at native C speed pretty much it gets compiled in so it's really fast You can run system calls and stuff basically pretty much as fast as you can and see
And it's being used for several other low-level projects. There's an interesting one here, which guys working on building an ethernet switch With ethernet drivers in it, so it's it's good fun, and it's really quick to develop in So I've been building this over the last couple of years really
Start up with system calls which are mostly done you can call most of the system calls few left to do There's all the data structures that come with them all the structs that socket addresses lots and lots of these things Then there's a lot of constants every system called you look at the man page, and there's a list of 20 constants
There's iocutl which I've done some of but there are Infinite numbers of them, and they're all different and inconsistent, so I've only done a ones as I've used them I've got net link support working net link is the new interface to talk to the networking subsystem, so you can
Create network interfaces give them IP addresses create routes and those things that was Quite fun to implement really in a high-level language because it's designed to be used with C macros really but it and it's not terribly well documented other than looking at the source code and trying things out and
And so on so that was quite fun and Then there's support for different architectures. I tested stuff on our harmony and 64 and 32-bit and Recently power PC. I was doing MIPS as well Which was good for finding bugs and finding the weird things are different all the constants
Mostly have the same values in different architectures except the ones that don't There are whole lots of different weird things that you find a different Which you never knew about it's good learning exercise doing all this stuff by the way So here's a simple example of basically Creating a socket setting a socket address
binding to a port Listening on it, and then I'm feeding it into an e-poll so you can listen for events on it later So as you can see you can use You can use Strings for your flag so I Mean understands which flags are supposed to be in which place so these are the sea flags that you'd use
So and you can just use commas instead of having to do or bitwise or on the flags And then there are many sockets have methods for bind and listen. It's kind of That's a the colon is the lower object notation so it and
Things like file descriptors a garbage collected if they go out of scope so which will then close the socket so it's kind of Easy to use if you're you know even if you are not a C programmer and don't really understand How currently stuff works you can start learning learning it
this is net link so this is creating a Dummy net interface This is from the test scripts because I do a lot of that you can set a Mac exchange the MAC address Set an IP address on it great put it up and down delete the interface And you say you can create vlan interface all sorts of all the things that you can do in that link
there's a couple of different Syntaxes, this is just this is an object with the interfaces and you can print it And it'll print out something looks pretty much like if config with all the information about the interface So it's kind of quite easy to work with network interfaces
Much easier than right just shelling out and calling if config or IP And you can do the same thing with roots and so on so it's kind of I find it quiet It's quite satisfying that it's kind of quite easy to use the hard bits Libc and the kernel don't provide exactly the same IPI there's the libc
Does all sorts of compatibility things and it varies depending which I use run this with different C libraries for compatibility testing The worst thing is 64-bit files on 32-bit CPUs, which are just horrible Muscle just doesn't support just always gives you 64-bit, which is nice and clean, but if you basically call
Stat or Something like that you don't actually know what it's going to be so graph And I just call the the system the syscall interface directly if for compatibility reasons I spent a lot of time on testing so there's a complete test suite that calls all the system calls and
Checks that it's created the the network interface It's supposed to create and it's got the IP addresses on it and checks say through using Sys interface as well as through netlink and so on so it And it You know does some seeks it more than 64 bits in a file and checks that it's actually got the position expect so on
What's the point of this now? I've made it Prototyping Networking code I find it quite. It's quite quick You know you've got a high level language with string operations Which is you know if you just want to knock together a quick interface?
It's fast and you can always convert it to C later It kind of reads very much like the C, but you can just knock something together quite quickly General network programs you can do asynchronous stuff sockets, and that's it's fast You can do 20,000 requests a second on a kind of dummy things because it's basically C type speed
Learning you can just try stuff out You Will less likely get segfault so you can do it if you try, but you you know you you can You don't have to you can just give it high-level lure types with tables instead of creating all your M All your kernel structures, so you're much less likely to make miss stupid mistakes
The there's some example code mainly the tests, but they and I've got a real pair of Examples but some better documentation would be helpful for people who want to learn about Linux But don't are not C programmers and want something high-level. I mean I'm a C programmer, but I
still like programming in Kind of high-level languages sometimes you know it's a it's fun Simple Linux containers that was my original use case which is Kind of getting again. I've got a sample, and there's a sample repo That's got a really basic thing that brings up a container with a network interface
It's bridged across and you can run a little program in it. It's needs more functionality, but it's kind of There and the my aim is just build a whole You know whole environment that runs a web server or a program that you can just build the whole thing It just builds the whole environment. You can test everything and it just then you can just run it
It's quite quick and fast and simple because I like simple things where you can understand the whole system And there's less really to go wrong The Roadmap for things I want to do Once I use luajit because it was quick and easy to do I'd like to pull it straight lure partly
To do the MIPS support because there's no luajit support for First off like MIPS because The open wrt that kind of thing that uses lure as a conflict language And I think this might be a good fit for some of the stuff They're doing so I'd kind of like to get in state where it's possible to try that
Documentation Really needs documentation More API is particularly on IP tables set com se Linux that kind of thing so that you've got a bit of security support around your system images If you don't like lure you could port this to another language The only language I've ever I looked when I started this and the only kind of language that seems to have a really comprehensive
Linux kernel interfaces go There's little bits in Ruby There's a half functional net link project, but it seems a bit not doesn't be seen well. I couldn't find much else It's it's a fair amount of work, but it's I think it's worthwhile for a scripting language to have this kind of interface
Everything's on github under this is the main repository, but I've got there's a few example repositories This talks in a repo on github Lurgits legit org I'm on Twitter or email or raise a github issue send some code pocket
That's it really um questions
Well, yeah, the nice thing actually is you don't have to it's legit everything is in alert You don't have to build it at all. You simply you Simply I can show you Sorry, this isn't my computer so I can't But basically there is no build process the entire thing
Basically is written in Lua. There is no C code at all in the project. So Because of the FFI interface does all the C interfacing so there there is no build you just do require Cisco and that's it and then you can start using it So and all the bits of the different architectures are just it just detects your architecture and
Includes that at runtime. So so there is no build process at all Which makes the build really simple Yeah, well, I mean I look to go but the sorry the thing
You know you you repeat it
He's brought the microphone Sorry, yeah Looking back knows that you've seen Things like go and what it has for all of the stuff if you were to do it again Would you consider something like go or another language when it comes to a simple in its system?
I the reason I thought about using go because it was you know, it was around when I started this I Like the fact that it's interpreted and so you can just write it and there's no sort of compile stage at all so because part of the thing was this educational thing that you can just look at the code that's running and change it and
you know just type stuff in and try it out and That that really quite appealed to me. I think I mean I'd you know, I'd use go for lots of things I it's all my kind of languages I'd like to use but I think that that is sort of immediacy where you can Run stuff is really quite valuable as well. And and so it's a different kind of use case