Monday, January 25, 2016

Running serval in core-network simulator

For testing purposes it can be really helpful to run your software in a simulator with various different network setups. Here is a short introduction how to use serval with core-network.

Running serval wifi-mesh mixed with static wired nodes.

Installation


Installation of core-network in Ubuntu (here 14.04) is straight forward:
sudo apt-get install core-network
That's it!
For other platform consult the official installation instructions on the core homepage.

The next step is getting serval running as a core-network service. Therefore we need of course serval itself. In this tutorial I am logged in as user meshadmin and have compiled servald with "./configure --prefix=/home/meshadmin/serval-conf".

A set of sample configuration files can be found on github, detailed instructions are following here.

First we need to enable the loading of custom services in /etc/core/core.conf by adding the following line:
custom_services_dir = /home/meshadmin/.core/myservices
In this directory we need to put a small python script describing our service. The contents of this serval.py should look something like this:
''' serval service.
'''

import os

from core.service import CoreService, addservice
from core.misc.ipaddr import IPv4Prefix, IPv6Prefix

class ServalService(CoreService):
    ''' servald as a service.
    '''
    # a unique name is required, without spaces
    _name = "ServalService"
    # you can create your own group here
    _group = "Mesh"
    # list of other services this service depends on
    _depends = ()
    # per-node directories
    _dirs = ("/home/meshadmin/serval-conf/etc/serval","/home/meshadmin/serval-conf/var/log", "/home/meshadmin/serval-conf/var/log/serval", "/home/meshadmin/serval-conf/var/run/serval", "/home/meshadmin/serval-conf/var/cache/serval","/home/meshadmin/serval-conf/var/cache/serval/sqlite3tmp","/home/meshadmin/serval-conf/var/cache/serval/blob")
    # generated files (without a full path this file goes in the node's dir,
    #  e.g. /tmp/pycore.12345/n1.conf/)
    _configs = ('/home/meshadmin/serval-conf/etc/serval/serval.conf', "mesh-start.sh", )
    # this controls the starting order vs other enabled services
    _startindex = 50
    # list of startup commands, also may be generated during startup
    #_startup = ('/home/meshadmin/serval-dna/servald start',)
    _startup = ('bash mesh-start.sh',)
    # list of shutdown commands
    _shutdown = ('/home/meshadmin/serval-dna/servald stop')

    @classmethod
    def generateconfig(cls, node, filename, services):
        ''' Return a string that will be written to filename, or sent to the
            GUI for user customization.
        '''
    if filename == "/home/meshadmin/serval-conf/etc/serval/serval.conf":
            cfg = "debug.rhizome=true\n"
            cfg += "debug.verbose=true\n"
        cfg += "interfaces.0.match=eth*\n"
        cfg += "interfaces.0.socket_type=dgram\n"
        cfg += "interfaces.0.type=ethernet\n"
    elif filename == "mesh-start.sh":
        cfg ="#!/bin/sh\n"
        cfg +="/home/meshadmin/serval-dna/servald start\n"
        cfg +="sleep $[ ( $RANDOM % 10 )  + 1 ]s\n"
        cfg +="for i in `ifconfig | grep \"inet addr:10.\" | cut -d\":\" -f 2 | cut -d\".\" -f1,2,3`\n"
        cfg +="do\n"
        cfg +="/home/meshadmin/serval-dna/servald scan $i.255\n"
        cfg +="done\n"
    else:
        cfg = ""
        return cfg

# this line is required to add the above class to the list of available services
addservice(ServalService)
This service starts a new instance of servald, pre-configured to use any ethernet interface available and starts a discovery scan on all local interfaces after a random sleep. Each instance gets its own fresh copy of the instance directory! Core uses ethernet devices for all types of networks, even WiFi - there configuring servald for all ethernet interfaces is enough.
Note: You need to edit the paths according to your setup!

To use this service it needs to be added to the __init__.py file in the myservices directory:
__all__ = ["sample", "serval"]

That is basically it but to make life easier here are a few helpers I also use.
Adding to the standard node configurations is done by editing ~/.core/nodes.conf. This is where I added the following two entries:
7 { ServalNode pc.gif pc.gif {DefaultRoute ServalService}  netns {simple serval node} }
8 { ServalMesh mdr.gif mdr.gif {OLSRORG ServalService}  netns {simple serval mesh node} }
The first entry, ServalNode, creates a simple node only with our serval service and a default route enabled - classic laptop/pc icon. ServalMesh is a pre-configured node with serval and standard OLSR as a mesh routing protocol - using the standard router icon.
Note: You need olsr installed prior to using it here (sudo apt-get install olsrd) and change the IP config in core to use a /24 or something like that instead of /32 because otherwise serval won't find any peers despite the fact that olsr routing works and you can ping (icmp not mdp!) all addresses.

For live inspection defining custom widgets is also very helpful. This can be done by editing ~/.core/widgets.conf and adding a few lines like these:
15 { {serval peer count} {/home/meshadmin/serval-dna/servald peer count} }
16 { {serval id self} {/home/meshadmin/serval-dna/servald id self} }
17 { {serval rhizome list} /home/meshadmin/serval-tests/listfiles }
18 { {serval mesh scan} /home/meshadmin/serval-tests/mesh-scan.sh }
The first widget displays the number of peers currently known of the inspected serval instance while the second entry shows the SID of the selected node. Widgets 17 and 18 both use helper scripts that can be found here and display the number of files in rhizome store and trigger a new neighbour scan.
Note: Again change the paths to fit your machine.

The last and final step after adding all these files is to restart the core-network service.
sudo service core-daemon restart

Now you are ready to go!

UPDATE: Changed github repo links for core-serval and serval-tests to new location.

No comments:

Post a Comment