tests: Fix memory leaks in test programs.
[sliver-openvswitch.git] / xenserver / etc_xensource_scripts_vif
1 #!/bin/sh
2
3 # Copyright (C) 2008,2009 Citrix Systems, Inc.
4 # Copyright (C) 2009 Nicira Networks, Inc.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU Lesser General Public License as published
8 # by the Free Software Foundation; version 2.1 only. with the special
9 # exception on linking described in file LICENSE.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU Lesser General Public License for more details.
15
16 # CA-23900: Warning: when VIFs are added to windows guests with PV drivers the backend vif device is registered,
17 # unregistered and then registered again. This causes the udev event to fire twice and this script runs twice.
18 # Since the first invocation of the script races with the device unregistration, spurious errors are possible
19 # which will be logged but are safe to ignore since the second script invocation should complete the operation.
20 # Note that each script invocation is run synchronously from udev and so the scripts don't race with each other.
21
22 # Keep other-config/ keys in sync with device.ml:vif_udev_keys
23
24 vsctl="/usr/bin/ovs-vsctl"
25 dump_vif_details="/usr/share/vswitch/scripts/dump-vif-details"
26 service="/sbin/service"
27
28 TYPE=`echo ${XENBUS_PATH} | cut -f 2 -d '/'`
29 DOMID=`echo ${XENBUS_PATH} | cut -f 3 -d '/'`
30 DEVID=`echo ${XENBUS_PATH} | cut -f 4 -d '/'`
31
32 XAPI=/xapi/${DOMID}/hotplug/${TYPE}/${DEVID}
33 HOTPLUG=/xapi/${DOMID}/hotplug/${TYPE}/${DEVID}
34 PRIVATE=/xapi/${DOMID}/private/${TYPE}/${DEVID}
35 BRCTL=/usr/sbin/brctl
36 IP=/sbin/ip
37
38
39 handle_promiscuous()
40 {
41     local arg=$(xenstore-read "${PRIVATE}/other-config/promiscuous")
42     if [ $? -eq 0 -a -n "${arg}" ] ; then
43         case "${arg}" in 
44             true|on) logger -t script-vif "${vif}: Promiscuous ports are not supported via vSwitch." ;;
45             *) ;;
46         esac
47     fi
48 }
49
50 handle_ethtool()
51 {
52     local opt=$1
53     local arg=$(xenstore-read "${PRIVATE}/other-config/ethtool-${opt}")
54     if [ $? -eq 0 -a -n "${arg}" ] ; then
55         case "${arg}" in
56             true|on)   /sbin/ethtool -K "${vif}" "${opt}" on ;;
57             false|off) /sbin/ethtool -K "${vif}" "${opt}" off ;;
58             *) logger -t scripts-vif "Unknown ethtool argument ${opt}=${arg} on ${vif}/${VIFUUID}" ;;
59         esac
60     fi
61 }
62
63 handle_mtu()
64 {
65     local mtu=$(xenstore-read "${PRIVATE}/MTU")
66     if [ $? -eq 0 -a -n "${mtu}" ]; then
67         echo "${mtu}" > /sys/class/net/${vif}/mtu
68     fi
69 }
70
71 add_to_bridge()
72 {
73     local address=$(xenstore-read "${PRIVATE}/bridge-MAC")
74     if [ $? -ne 0 -o -z "${address}" ]; then
75         logger -t scripts-vif "Failed to read ${PRIVATE}/bridge-MAC from xenstore"
76     fi
77     local bridge=$(xenstore-read "${PRIVATE}/bridge")
78     if [ $? -ne 0 -o -z "${bridge}" ]; then
79         logger -t scripts-vif "Failed to read ${PRIVATE}/bridge from xenstore"
80     fi
81     logger -t scripts-vif "Adding ${vif} to ${bridge} with address ${address}"
82
83     ${IP} link set "${vif}" down                        || logger -t scripts-vif "Failed to ip link set ${vif} down"
84     ${IP} link set "${vif}" arp off                     || logger -t scripts-vif "Failed to ip link set ${vif} arp off"
85     ${IP} link set "${vif}" multicast off               || logger -t scripts-vif "Failed to ip link set ${vif} multicast off"
86     ${IP} link set "${vif}" address "${address}"        || logger -t scripts-vif "Failed to ip link set ${vif} address ${address}"
87     ${IP} addr flush "${vif}"                           || logger -t scripts-vif "Failed to ip addr flush ${vif}"
88
89     local vif_details=$($dump_vif_details $DOMID $DEVID)
90     if [ $? -ne 0 -o -z "${vif_details}" ]; then
91             logger -t scripts-vif "Failed to retrieve vif details for vswitch"
92     fi
93
94     $vsctl add-port $bridge $vif $vif_details
95
96     ${IP} link set "${vif}" up                          || logger -t scripts-vif "Failed to ip link set ${vif} up"
97 }
98
99 echo Called as "$@" "$TYPE" "$DOMID" "$DEVID" | logger -t scripts-vif
100 case "$1" in
101 online)
102         handle_ethtool rx
103         handle_ethtool tx
104         handle_ethtool sg
105         handle_ethtool tso
106         handle_ethtool ufo
107         handle_ethtool gso
108
109         handle_mtu
110         add_to_bridge
111         handle_promiscuous
112
113         xenstore-write "${HOTPLUG}/vif" "${vif}"
114         xenstore-write "${HOTPLUG}/hotplug" "online"
115
116         # xs-xen.pq.hq:91e986b8e49f netback-wait-for-hotplug
117         xenstore-write "/local/domain/0/backend/vif/${DOMID}/${DEVID}/hotplug-status" "connected"
118
119         ;;
120 remove)
121         xenstore-rm "${HOTPLUG}/hotplug"
122         vif=vif${DOMID}.${DEVID}
123         logger -t scripts-vif "${vif} has been removed"
124         $vsctl del-port $bridge $vif
125         ;;
126 esac