all the python scripts are for python2, and fedora31 requires to be specific
[vsys-scripts.git] / slice-context / src / vsys.py
1 #!/usr/bin/env python
2
3 # vsys.py: Python functions to wrap PlanetLab vsys API'
4 #   Alina Quereilhac - 02/09/2012
5 #
6 # Copyright (c) 202 INRIA
7 #
8
9 ''' This extension provides easy to use Python functions to 
10 interact with the PlanetLab vsys API.
11
12 The vsys API is presented in the following publication:
13
14 Vsys: A programmable sudo
15 S Bhatia, G Di Stasi, T Haddow, A Bavier, S Muir, L Peterson
16 In USENIX 2011
17
18 '''
19
20 IFF_TUN = 0x0001
21 IFF_TAP = 0x0002
22
23 def vif_up(if_name, ip, prefix, snat = False):
24     """ Configures a virtual interface with the values given by the user and 
25     sets its state UP 
26
27     Parameters:
28      if_name:  the name of the virtual interface 
29      ip:        the IP address to be assigned to the interface 
30      prefix:    the network prefix associated to the IP address
31      snat:      whether to enable SNAT on the virtual interface.
32
33     Return value:
34     On success, return 0. 
35     On error, a RuntimeError is raised."""
36     
37     import _vsys
38     (code, msg) = _vsys.vif_up(if_name, ip, str(prefix), snat)
39     
40     if code < 0:
41         raise RuntimeError(msg)
42
43
44 def vif_down(if_name):
45     """ Sets the state of a virtual interface DOWN 
46
47     Parameters:
48      if_name:  the name of the virtual interface 
49
50     Return value:
51     On success, return 0. 
52     On error, a RuntimeError is raised."""
53     
54     import _vsys
55     (code, msg) = _vsys.vif_down(if_name)
56     
57     if code < 0:
58         raise RuntimeError(msg)
59
60 def fd_tuntap(if_type, no_pi = False):
61     """Creates a TAP or TUN device in PlanetLab, and returns the device name and
62     the associated file descriptor.
63  
64     Parameters:
65         if_type:   the type of virtual device. Either IFF_TAP (0x0001) or
66                     IFF_TUN (0x0002)
67         no_pi:     set flag IFF_NO_PI
68
69     Return value:
70     On success, fd_tuntap returns a tuple containing the file descriptor 
71     associated to the device and the device name assigned by the PlanetLab 
72     vsys script.
73     On error, a RuntimeError is raised."""
74
75     import _vsys
76     (fd, if_name) = _vsys.fd_tuntap(if_type, no_pi)
77
78     if fd < 0:
79         raise RuntimeError(if_name)
80
81     return (fd, if_name)
82
83 def vroute(action, network, prefix, host, device):
84     """ Adds or removes routes on PlanetLab virtual interfaces (TAP/TUN).
85
86     Note that all networks and gateways must belong to the virtual 
87     network segment associated to the vsys_vnet tag for the slice.
88
89     Parameters:
90         action:     either 'add' or 'del'
91         network:    destintation network
92         prefix:     destination network prefix
93         host:       IP of gateway virtual interface
94         device:     name of the gateway virtual interface
95
96     Return value:
97     On success, vroute returns 0.
98     On error, a RuntimeError is raised."""
99
100     import _vsys
101     (code, msg) = _vsys.vroute(action, network, str(prefix), host, device)
102
103     if code < 0:
104         raise RuntimeError(msg)
105
106