Skip to content

Commit 97e4d95

Browse files
committed
Add a new open vswitch plugin
This plugin allows to add, delete and dump openflow rules for a given bridge. Signed-off-by: Guillaume <[email protected]>
1 parent 7b661ff commit 97e4d95

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

Diff for: README.md

+71
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,77 @@ Restart a service only if it is already running.
338338
$ xe host-call-plugin host-uuid<uuid> plugin=service.py fn=try_restart_service args:service=<service>
339339
```
340340

341+
342+
### `open-vswitch`
343+
344+
Add, delete and dump openflow rules.
345+
346+
#### Examples
347+
348+
##### Add flow
349+
350+
```
351+
$ xe host-call-plugin host-uuid<uuid> plugin=open-vswitch.py \
352+
fn=add-flow \
353+
args:bridge=xenbr0 \
354+
args:rule="ip,in_port=1,dl_src=5e:f3:5b:e6:7b:f3,nw_dst=10.10.10.0/24 actions=drop"
355+
```
356+
357+
##### Delete flows
358+
```
359+
$ xe host-call-plugin host-uuid<uuid> plugin=open-vswitch.py \
360+
fn=del-flows \
361+
args:bridge=xenbr0 \
362+
args:rule="ip,in_port=1,dl_src=5e:f3:5b:e6:7b:f3,nw_dst=10.10.10.0/24"
363+
```
364+
365+
##### Dump flows
366+
367+
- This command will return all flows entries in the bridge passed as a parameter.
368+
- That means a bridge is required as parameter:
369+
```
370+
$ xe host-call-plugin host-uuid=<uuid> plugin=open-vswitch.py fn=dump-flows args:bridge=xenbr0 | jq .
371+
{
372+
"returncode": 0,
373+
"command": [
374+
"ovs-ofctl",
375+
"dump-flows",
376+
"xenbr0"
377+
],
378+
"stderr": "",
379+
"stdout": "NXST_FLOW reply (xid=0x4):\n cookie=0x0, duration=248977.339s, table=0, n_packets=24591786, n_bytes=3278442075, idle_age=0, hard_age=65534, priority=0 actions=NORMAL\n"
380+
}
381+
```
382+
383+
- This error is raised when the bridge parameter is missing:
384+
```
385+
$ xe host-call-plugin host-uuid=<uuid> plugin=open-vswitch.py fn=dump-flows | jq .
386+
{
387+
"returncode": 1,
388+
"command": [
389+
"ovs-ofctl",
390+
"dump-flows"
391+
],
392+
"stderr": "bridge parameter is missing",
393+
"stdout": ""
394+
}
395+
```
396+
397+
- If the bridge is unknown, the following error will occur:
398+
```
399+
$ xe host-call-plugin host-uuid=<uuid> plugin=open-vswitch.py args:bridge=xenbr10 fn=dump-flows | jq .
400+
{
401+
"returncode": 1,
402+
"command": [
403+
"ovs-ofctl",
404+
"dump-flows",
405+
"xenbr10"
406+
],
407+
"stderr": "ovs-ofctl: xenbr10 is not a bridge or a socket\n",
408+
"stdout": ""
409+
}
410+
```
411+
341412
## Tests
342413

343414
To run the plugins' unit tests you'll need to install `pytest`, `pyfakefs` and `mock`.

Diff for: SOURCES/etc/xapi.d/plugins/open-vswitch.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
import json
5+
6+
import XenAPIPlugin
7+
8+
from xcpngutils import configure_logging, error_wrapped, run_command
9+
10+
11+
@error_wrapped
12+
def add_flow(_session, args):
13+
_LOGGER.info("Calling add_flow with args {}".format(args))
14+
return json.dumps(True)
15+
16+
17+
@error_wrapped
18+
def del_flows(_session, args):
19+
_LOGGER.info("Calling del_flows with args {}".format(args))
20+
return json.dumps(True)
21+
22+
23+
@error_wrapped
24+
def dump_flows(_session, args):
25+
_LOGGER.info("Calling check with args {}".format(args))
26+
ofctl_cmd = ["ovs-ofctl", "dump-flows"]
27+
bridge = args.get("bridge")
28+
29+
if bridge is None:
30+
return json.dumps(
31+
{
32+
"returncode": 1,
33+
"command": ofctl_cmd,
34+
"stderr": "bridge parameter is missing",
35+
"stdout": "",
36+
}
37+
)
38+
ofctl_cmd.append(bridge)
39+
cmd = run_command(ofctl_cmd, check=False)
40+
return json.dumps(cmd)
41+
42+
43+
_LOGGER = configure_logging("open-vswitch")
44+
if __name__ == "__main__":
45+
XenAPIPlugin.dispatch(
46+
{
47+
"add-flow": add_flow,
48+
"del-flows": del_flows,
49+
"dump-flows": dump_flows,
50+
}
51+
)

0 commit comments

Comments
 (0)