summaryrefslogtreecommitdiffstats
path: root/core/vswitch_controller_pvp.py
blob: 0c98cc7f1cbc54747b5cacdd2704839c8d199796 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# Copyright 2015 Intel Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""VSwitch controller for Physical to VM to Physical deployment
"""

import logging

from core.vswitch_controller import IVswitchController
from vswitches.utils import add_ports_to_flow
from conf import settings

_FLOW_TEMPLATE = {
    'idle_timeout': '0'
}

class VswitchControllerPVP(IVswitchController):
    """VSwitch controller for PVP deployment scenario.

    Attributes:
        _vswitch_class: The vSwitch class to be used.
        _vswitch: The vSwitch object controlled by this controller
        _deployment_scenario: A string describing the scenario to set-up in the
            constructor.
    """
    def __init__(self, vswitch_class, traffic):
        """Initializes up the prerequisites for the PVP deployment scenario.

        :vswitch_class: the vSwitch class to be used.
        """
        self._logger = logging.getLogger(__name__)
        self._vswitch_class = vswitch_class
        self._vswitch = vswitch_class()
        self._deployment_scenario = "PVP"
        self._traffic = traffic.copy()
        self._logger.debug('Creation using ' + str(self._vswitch_class))

    def setup(self):
        """ Sets up the switch for pvp
        """
        self._logger.debug('Setup using ' + str(self._vswitch_class))

        try:
            self._vswitch.start()

            bridge = settings.getValue('VSWITCH_BRIDGE_NAME')
            self._vswitch.add_switch(bridge)

            (_, phy1_number) = self._vswitch.add_phy_port(bridge)
            (_, phy2_number) = self._vswitch.add_phy_port(bridge)
            (_, vport1_number) = self._vswitch.add_vport(bridge)
            (_, vport2_number) = self._vswitch.add_vport(bridge)

            self._vswitch.del_flow(bridge)

            # configure flows according to the TC definition
            flow_template = _FLOW_TEMPLATE.copy()
            if self._traffic['flow_type'] == 'IP':
                flow_template.update({'dl_type':'0x0800', 'nw_src':self._traffic['l3']['srcip'],
                                      'nw_dst':self._traffic['l3']['dstip']})

            flow1 = add_ports_to_flow(flow_template, phy1_number,
                                      vport1_number)
            flow2 = add_ports_to_flow(flow_template, vport2_number,
                                      phy2_number)
            self._vswitch.add_flow(bridge, flow1)
            self._vswitch.add_flow(bridge, flow2)

            if self._traffic['bidir']:
                flow3 = add_ports_to_flow(flow_template, phy2_number,
                                          vport2_number)
                flow4 = add_ports_to_flow(flow_template, vport1_number,
                                          phy1_number)
                self._vswitch.add_flow(bridge, flow3)
                self._vswitch.add_flow(bridge, flow4)

        except:
            self._vswitch.stop()
            raise

    def stop(self):
        """Tears down the switch created in setup().
        """
        self._logger.debug('Stop using ' + str(self._vswitch_class))
        self._vswitch.stop()

    def __enter__(self):
        self.setup()

    def __exit__(self, type_, value, traceback):
        self.stop()

    def get_vswitch(self):
        """See IVswitchController for description
        """
        return self._vswitch

    def get_ports_info(self):
        """See IVswitchController for description
        """
        self._logger.debug('get_ports_info  using ' + str(self._vswitch_class))
        return self._vswitch.get_ports(settings.getValue('VSWITCH_BRIDGE_NAME'))

    def dump_vswitch_flows(self):
        """See IVswitchController for description
        """
        self._vswitch.dump_flows(settings.getValue('VSWITCH_BRIDGE_NAME'))
pan> *dev, struct device_attribute *attr, char *buf) { struct tle62x0_state *st = dev_get_drvdata(dev); char *bp = buf; unsigned char *buff = st->rx_buff; unsigned long fault = 0; int ptr; int ret; mutex_lock(&st->lock); ret = tle62x0_read(st); dev_dbg(dev, "tle62x0_read() returned %d\n", ret); if (ret < 0) { mutex_unlock(&st->lock); return ret; } for (ptr = 0; ptr < (st->nr_gpio * 2)/8; ptr += 1) { fault <<= 8; fault |= ((unsigned long)buff[ptr]); dev_dbg(dev, "byte %d is %02x\n", ptr, buff[ptr]); } for (ptr = 0; ptr < st->nr_gpio; ptr++) { bp += sprintf(bp, "%s ", decode_fault(fault >> (ptr * 2))); } *bp++ = '\n'; mutex_unlock(&st->lock); return bp - buf; } static DEVICE_ATTR(status_show, S_IRUGO, tle62x0_status_show, NULL); static ssize_t tle62x0_gpio_show(struct device *dev, struct device_attribute *attr, char *buf) { struct tle62x0_state *st = dev_get_drvdata(dev); int gpio_num = to_gpio_num(attr); int value; mutex_lock(&st->lock); value = (st->gpio_state >> gpio_num) & 1; mutex_unlock(&st->lock); return snprintf(buf, PAGE_SIZE, "%d", value); } static ssize_t tle62x0_gpio_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t len) { struct tle62x0_state *st = dev_get_drvdata(dev); int gpio_num = to_gpio_num(attr); unsigned long val; char *endp; val = simple_strtoul(buf, &endp, 0); if (buf == endp) return -EINVAL; dev_dbg(dev, "setting gpio %d to %ld\n", gpio_num, val); mutex_lock(&st->lock); if (val) st->gpio_state |= 1 << gpio_num; else st->gpio_state &= ~(1 << gpio_num); tle62x0_write(st); mutex_unlock(&st->lock); return len; } static DEVICE_ATTR(gpio1, S_IWUSR|S_IRUGO, tle62x0_gpio_show, tle62x0_gpio_store); static DEVICE_ATTR(gpio2, S_IWUSR|S_IRUGO, tle62x0_gpio_show, tle62x0_gpio_store); static DEVICE_ATTR(gpio3, S_IWUSR|S_IRUGO, tle62x0_gpio_show, tle62x0_gpio_store); static DEVICE_ATTR(gpio4, S_IWUSR|S_IRUGO, tle62x0_gpio_show, tle62x0_gpio_store); static DEVICE_ATTR(gpio5, S_IWUSR|S_IRUGO, tle62x0_gpio_show, tle62x0_gpio_store); static DEVICE_ATTR(gpio6, S_IWUSR|S_IRUGO, tle62x0_gpio_show, tle62x0_gpio_store); static DEVICE_ATTR(gpio7, S_IWUSR|S_IRUGO, tle62x0_gpio_show, tle62x0_gpio_store); static DEVICE_ATTR(gpio8, S_IWUSR|S_IRUGO, tle62x0_gpio_show, tle62x0_gpio_store); static DEVICE_ATTR(gpio9, S_IWUSR|S_IRUGO, tle62x0_gpio_show, tle62x0_gpio_store); static DEVICE_ATTR(gpio10, S_IWUSR|S_IRUGO, tle62x0_gpio_show, tle62x0_gpio_store); static DEVICE_ATTR(gpio11, S_IWUSR|S_IRUGO, tle62x0_gpio_show, tle62x0_gpio_store); static DEVICE_ATTR(gpio12, S_IWUSR|S_IRUGO, tle62x0_gpio_show, tle62x0_gpio_store); static DEVICE_ATTR(gpio13, S_IWUSR|S_IRUGO, tle62x0_gpio_show, tle62x0_gpio_store); static DEVICE_ATTR(gpio14, S_IWUSR|S_IRUGO, tle62x0_gpio_show, tle62x0_gpio_store); static DEVICE_ATTR(gpio15, S_IWUSR|S_IRUGO, tle62x0_gpio_show, tle62x0_gpio_store); static DEVICE_ATTR(gpio16, S_IWUSR|S_IRUGO, tle62x0_gpio_show, tle62x0_gpio_store); static struct device_attribute *gpio_attrs[] = { [0] = &dev_attr_gpio1, [1] = &dev_attr_gpio2, [2] = &dev_attr_gpio3, [3] = &dev_attr_gpio4, [4] = &dev_attr_gpio5, [5] = &dev_attr_gpio6, [6] = &dev_attr_gpio7, [7] = &dev_attr_gpio8, [8] = &dev_attr_gpio9, [9] = &dev_attr_gpio10, [10] = &dev_attr_gpio11, [11] = &dev_attr_gpio12, [12] = &dev_attr_gpio13, [13] = &dev_attr_gpio14, [14] = &dev_attr_gpio15, [15] = &dev_attr_gpio16 }; static int to_gpio_num(struct device_attribute *attr) { int ptr; for (ptr = 0; ptr < ARRAY_SIZE(gpio_attrs); ptr++) { if (gpio_attrs[ptr] == attr) return ptr; } return -1; } static int tle62x0_probe(struct spi_device *spi) { struct tle62x0_state *st; struct tle62x0_pdata *pdata; int ptr; int ret; pdata = dev_get_platdata(&spi->dev); if (pdata == NULL) { dev_err(&spi->dev, "no device data specified\n"); return -EINVAL; } st = kzalloc(sizeof(struct tle62x0_state), GFP_KERNEL); if (st == NULL) return -ENOMEM; st->us = spi; st->nr_gpio = pdata->gpio_count; st->gpio_state = pdata->init_state; mutex_init(&st->lock); ret = device_create_file(&spi->dev, &dev_attr_status_show); if (ret) { dev_err(&spi->dev, "cannot create status attribute\n"); goto err_status; } for (ptr = 0; ptr < pdata->gpio_count; ptr++) { ret = device_create_file(&spi->dev, gpio_attrs[ptr]); if (ret) { dev_err(&spi->dev, "cannot create gpio attribute\n"); goto err_gpios; } } /* tle62x0_write(st); */ spi_set_drvdata(spi, st); return 0; err_gpios: while (--ptr >= 0) device_remove_file(&spi->dev, gpio_attrs[ptr]); device_remove_file(&spi->dev, &dev_attr_status_show); err_status: kfree(st); return ret; } static int tle62x0_remove(struct spi_device *spi) { struct tle62x0_state *st = spi_get_drvdata(spi); int ptr; for (ptr = 0; ptr < st->nr_gpio; ptr++) device_remove_file(&spi->dev, gpio_attrs[ptr]); device_remove_file(&spi->dev, &dev_attr_status_show); kfree(st); return 0; } static struct spi_driver tle62x0_driver = { .driver = { .name = "tle62x0", .owner = THIS_MODULE, }, .probe = tle62x0_probe, .remove = tle62x0_remove, }; module_spi_driver(tle62x0_driver); MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>"); MODULE_DESCRIPTION("TLE62x0 SPI driver"); MODULE_LICENSE("GPL v2"); MODULE_ALIAS("spi:tle62x0");