ConnectioHandler: set select timeout to 0

Setting the select timeout to 0 makes the call non-blocking which is
required if we want to use more than one connection handler. This will
be usefull for implementing network namespace support.

Signed-off-by: Ondrej Lichtner <olichtne@redhat.com>
Signed-off-by: Jiri Pirko <jiri@resnulli.us>
NetTestSlave: enable/disable NM on demand

This commit adds 3 new Slave methods:
enable_nm
disable_nm
restore_nm_option

These can be used to manipulate the value of the "use_nm" option, which
controlls whether the slave will use NM for interface configuration.

The patch also adds these methods to the controller class Machine.

This is usefull for network namespaces since NM doesn't support them and
probably never will.

Signed-off-by: Ondrej Lichtner <olichtne@redhat.com>
Signed-off-by: Jiri Pirko <jiri@resnulli.us>
InterfaceManager: change id to device mapping

This patch replaces the id to device mapping with id to if_index
mapping. This removes the need to update the mapping if a Device object
is removed and recreated for the same device. This can happen when
working with network namespaces - moving a device to a different
namespace deletes it in the current namespace.

Because of this we also need a new attribute - _tmp_mapping that maps id
to device for software devices that were not initialized yet and don't
have an if_index assigned to them.

Signed-off-by: Ondrej Lichtner <olichtne@redhat.com>
Signed-off-by: Jiri Pirko <jiri@resnulli.us>
slave: network namespace support

This patch adds network namespace support to lnst slaves.
NetTestSlave:
* New network namespaces are created using slave methods add_namespace
  and del_namespace. New network namespaces created by forking the
  lnst-slave process and using the unshare system call through libc. The
  namespaces are stored in the _net_namespaces dictionary in the
  SlaveMethods class. The process that manages the new namespaces
  communicates with the parent process through a Pipe object.
* To move an interface to a different network namespace we can use the
  set_if_netns and return_if_netns methods.
* The ServerHandler class now uses two ConnectionHandler objects - one
  for the controller and commands and the new one for communication with
  different network namespaces.
* The ServerHandler class is also capable of sending messages to these
  namespaces by the send_data_to_netns method.
* The Server class now support two new message types: "to_netns",
  "from_netns" that encapsulate a message that should be routed to a
  different process.
InterfaceManager:
* The Device class now has a new attribute _netns that is None if the
  device is in the current network namespace or contains the name of the
  namespace that was created by this process. This is used in order to
  not remove Device objects from the InterfaceManager when we move them
  to different network namespaces.
* I also implemented the reconnect_netlink method that creates a new
  netlink socket so that processes in the new namespace don't use the
  socket inherited from the parent process.
NmConfigDevice:
* Modified the is_nm_managed methods - when working with network
  namespaces NM should not be used for configuration.

Signed-off-by: Ondrej Lichtner <olichtne@redhat.com>
Signed-off-by: Jiri Pirko <jiri@resnulli.us>
NetConfigDevice: bonding use iproute2 when possible

This patch makes the NetConfigDeviceBond class use the ip command for
device creation/destruction and enslavement instead of sysfs. This is
because bond devices created through sysfs are always in the root
network namespace due to a kernel bug.

However on older systems bonding doesn't support netlink so I added an
exception to use sysfs for kernels older than 3.10. I chose 3.10 because
that's the oldest one I tested that worked - it's the current RHEL7
kernel.

Signed-off-by: Ondrej Lichtner <olichtne@redhat.com>
Signed-off-by: Jiri Pirko <jiri@resnulli.us>
XmlProcessing: don't transform None to string

If the XmlData dictionary contained a None value it would be transformed
to a string. This fixes that.

Signed-off-by: Ondrej Lichtner <olichtne@redhat.com>
Signed-off-by: Jiri Pirko <jiri@resnulli.us>
recipe: network namespace support

This patch extends the recipe XML files to support network namespaces.
This is a pretty simple change:
* all device elemenets (eth, bond, bridge, ...) now support the "netns"
  attribute which can be an arbitrary string
* command elements config, run, kill, intr and wait now also support the
  "netns" attribute

At the moment there are no checks whether or not the value of the
attribute makes sense. The controller will create the namespaces based
on the device configuration so if a command uses a different namespace
it will fail. The same goes for background commands - the corresponding
signal command needs to be run in the same namespace or it will fail.
This probably can be improved in the future.

Signed-off-by: Ondrej Lichtner <olichtne@redhat.com>
Signed-off-by: Jiri Pirko <jiri@resnulli.us>
controller: network namespace support

This patch adds the rest of the controller support for network
namespaces.
Machine:
* The most important addition is the _rpc_call_to_netns method of the
  Machine class. This method ensures that whent the slave recieves the
  message it will route it's contents to the specified network namespace.
* Packet capture was modified so that we also capture on devices that
  are in different namespaces.
* To add/remove network namespaces we use the add_netns and del_netns
  methods of the Machine class.
Interface:
* If the interface is supposed to be in a non-root network namespace the
  configure method first moves the interface to the correct namespace
  and then configures the interface in the new interface.
  Deconfiguration is the inverse process - first we deconfigure the
  interface and then we return the interface to the previous namespace.
* SoftInterface configuration creates the interfaces in the target
  namespace since it might be impossible to move them to a different
  namespace once they're created.
NetTestController:
* The _prepare_network method finds all required network namespaces
  (based on interface configuration) and takes care of creating these
  devices before configuring the interfaces. It also disables the use of
  network manager on the slave machine since NM doesn't support network
  namespaces.
* In addition to that it takes care of extracting the recipe information
  about network namespaces and storing it in the Interface object, and
  command dictionary.

Signed-off-by: Ondrej Lichtner <olichtne@redhat.com>
Signed-off-by: Jiri Pirko <jiri@resnulli.us>
network namespace support for LNST

The following set of patches introduces network namespace support to LNST. This
required some changes to the way the slave server works. Currently there will
be a single "master" process that acts as the main server and manages the root
network namespace (or the namespace that lnst-slave was launched in). This is
the process that the controller connects to and communicates with.

When the controller requests the creation of a new network namespace the server
forks itself and using the unshare syscall creates a new namespace. This new
process will now act as the lnst-slave server that manages a different
namespace (configuration, running commands and cleanup). The new process only
communicates with it's parent through a pipe object, and as far as network
communication is concerned this parent is it's connected controller. At this
point the parent process starts acting like a router resending messages from
the network namespaces to the controller and vice versa.

The configuration itself works in this order:
1. namespace creation
2. move interfaces to their target namespaces
3. configure interfaces (create soft interfaces in their target namespace)

When it comes to recipe XML format, the only thing that changed is that you can
now use the attribute "netns" for devices, and for some commands (where it
makes sense).

Next it will probably be a good idea to add support for veth type devices,
signal commands probably shouldn't require to use the netns attribute, netns
support for python taks.

To finish off I'll add an example recipe:
<lnstrecipe>
    <network>
        <host id="1">
            <interfaces>
                <eth id="t1" netns="xyz" label="ttnet"/>
                <eth id="t2" netns="xyz" label="ttnet"/>
                <bond id="t3" netns="xyz">
                    <addresses>
                        <address value="192.168.100.240/24"/>
                    </addresses>
                    <slaves>
                        <slave id="t1"/>
                        <slave id="t2"/>
                    </slaves>
                </bond>
            </interfaces>
        </host>
        <host id="2">
            <interfaces>
                <eth id="t1" label="ttnet">
                    <addresses>
                        <address value="192.168.100.215/24"/>
                    </addresses>
                </eth>
            </interfaces>
        </host>
    </network>

    <task>
        <run host="1" command="ip a"/>
        <run host="1" command="ip a" netns="xyz"/>
        <run host="1" module="IcmpPing" netns="xyz">
            <options>
                <option name="addr" value="{ip(2,t1)}"/>
                <option name="count" value="40"/>
                <option name="interval" value="1"/>
            </options>
        </run>
    </task>
</lnstrecipe>