#!/bin/bash
set -ex

lock_file=/var/lock/openshift-sdn.lock

action=$1
pod_namespace=$2
pod_name=$3
net_container=$4
tenant_id=$5

lockwrap() {
    (
    flock 200
    "$@"
    ) 200>${lock_file}
}

# Retrieve the name of the host-local member of the veth pair that
# connects the container (identified by pid) to the docker bridge.
get_veth_host() {
    local pid=$1

    local veth_ifindex=$(nsenter -n -t $pid -- ethtool -S eth0 | sed -n -e 's/.*peer_ifindex: //p')
    # Strip a suffix starting with '@' from the interface name.
    # The suffixed interface name won't be recognized by brctl or ovs-*
    ip link show | sed -ne "s/^$veth_ifindex: \([^:@]*\).*/\1/p"
}

Init() {
    true
}

Setup() {
    source /etc/openshift-sdn/config.env
    cluster_subnet=${OPENSHIFT_CLUSTER_SUBNET}

    pid=$(docker inspect --format "{{.State.Pid}}" ${net_container})
    network_mode=$(docker inspect --format "{{.HostConfig.NetworkMode}}" ${net_container})
    if [ "${network_mode}" == "host" ]; then
      # quit, nothing for the SDN here
      exit 0
    fi
    ipaddr=$(docker inspect --format "{{.NetworkSettings.IPAddress}}" ${net_container})
    veth_host=$(get_veth_host $pid)

    # If the caller didn't pass a tenant ID, we make one up based on
    # the first two letters of the pod name.
    # FIXME: remove this when it's no longer needed for testing
    if [ -z "$tenant_id" ]; then
       tenant_id=$(echo $pod_name | perl -ne 'print unpack("S", $_)')
    fi

    brctl delif lbr0 $veth_host
    ovs-vsctl add-port br0 ${veth_host} 
    ovs_port=$(ovs-ofctl -O OpenFlow13 dump-ports-desc br0  | grep ${veth_host} | cut -d "(" -f 1 | tr -d ' ')

    ovs-ofctl -O OpenFlow13 add-flow br0 "table=3,cookie=0x${ovs_port},priority=100,in_port=${ovs_port},ip,nw_src=${ipaddr},actions=load:${tenant_id}->NXM_NX_REG0[],goto_table:4"
    if [ "${tenant_id}" == "0" ]; then
      ovs-ofctl -O OpenFlow13 add-flow br0 "table=5,cookie=0x${ovs_port},priority=150,ip,nw_dst=${ipaddr},actions=output:${ovs_port}"
    else
      ovs-ofctl -O OpenFlow13 add-flow br0 "table=5,cookie=0x${ovs_port},priority=100,ip,nw_dst=${ipaddr},reg0=${tenant_id},actions=output:${ovs_port}"
    fi

    add_subnet_route="ip route add ${cluster_subnet} dev eth0 proto kernel scope link src $ipaddr"
    nsenter -n -t $pid -- $add_subnet_route
}

Teardown() {
    source /etc/openshift-sdn/config.env

    pid=$(docker inspect --format "{{.State.Pid}}" ${net_container})
    network_mode=$(docker inspect --format "{{.HostConfig.NetworkMode}}" ${net_container})
    if [ "${network_mode}" == "host" ]; then
      # quit, nothing for the SDN here
      exit 0
    fi
    veth_host=$(get_veth_host $pid)
    ovs_port=$(ovs-ofctl -O OpenFlow13 dump-ports-desc br0  | grep ${veth_host} | cut -d "(" -f 1 | tr -d ' ')
    ovs-vsctl del-port $veth_host
    ovs-ofctl -O OpenFlow13 del-flows br0 "table=3,cookie=0x${ovs_port}/0xffffffff"
    ovs-ofctl -O OpenFlow13 del-flows br0 "table=5,cookie=0x${ovs_port}/0xffffffff"
}

case "$action" in
    init)
	lockwrap Init
	;;
    setup)
	lockwrap Setup
	;;
    teardown)
	lockwrap Teardown
	;;
    *)
        echo "Bad input: $@"
        exit 1
esac

