aboutsummaryrefslogtreecommitdiffstats
path: root/vicn/resource/vpp/vpp_host.py
blob: 60fdfd47ac9cdc0fb043b745f112472bd8c60f5f (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2017 Cisco and/or its affiliates.
#
# 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.
#

import asyncio

from netmodel.model.type            import String
from vicn.core.attribute            import Attribute, Multiplicity
from vicn.core.exception            import ResourceNotFound
from vicn.core.requirement          import Requirement
from vicn.core.task                 import BashTask, task, EmptyTask
from vicn.core.task                 import inherit_parent
from vicn.resource.linux.application import LinuxApplication
from vicn.resource.linux.file       import TextFile
from vicn.resource.node             import Node
from vicn.resource.vpp.scripts      import FN_APPARMOR_DPDK_SCRIPT
from vicn.resource.vpp.scripts      import TPL_APPARMOR_DPDK_SCRIPT
from vicn.resource.vpp.scripts      import FN_VPP_DPDK_SCRIPT
from vicn.resource.vpp.scripts      import TPL_VPP_DPDK_DAEMON_SCRIPT
from vicn.resource.vpp.vpp_commands import CMD_VPP_DISABLE
from vicn.resource.vpp.vpp_commands import CMD_VPP_STOP_SERVICE

CMD_INSERT_MODULES = 'modprobe uio && modprobe igb_uio'
CMD_APP_ARMOR_RELOAD = '''
# Force apparmor to reload profiles to include the new profile
/etc/init.d/apparmor reload
'''
CMD_SYSCTL_HUGEPAGES = 'sysctl -w vm.nr_hugepages={nb_hp}'
DEFAULT_NB_HUGEPAGES = 1024
CMD_GREP_UIO_DEV = 'ls /dev | grep uio'
CMD_CREATE_UIO_DEVICES = "dpdk-devbind --bind=igb_uio {pci_address}"

class VPPHost(LinuxApplication):
    """
    Resource: VPPHost

    Only used for container deployment

    Packages required on the host
     - vpp : sysctl configuration
     - vpp-dpdk-dkms : kernel modules

    Host must be configured to let vpp to work into container:
     - install new apparmor profile (to let the container to read
       hugepages info in /sys/kernel/mm/hugepages)
     - set hugepages into the host
    """

    node =  Attribute(Node,
            description = 'Node on which the application is installed',
            mandatory = True,
            multiplicity = Multiplicity.OneToOne,
            reverse_name = 'vpp_host',
            reverse_description = 'Setup for hosting vpp containers',
            reverse_auto = True,
            requirements = [Requirement('numa_mgr')])
    uio_devices = Attribute(String,
            description = 'uio devices on the node',
            multiplicity = Multiplicity.OneToMany,
            ro = True)
    dpdk_devices = Attribute(String,
            description = 'Dpdk devices on the node',
            multiplicity = Multiplicity.OneToMany)

    __package_names__ = ['vpp-dpdk-dkms', 'vpp-dpdk-dev']

    #--------------------------------------------------------------------------
    # Constructor and Accessors
    #--------------------------------------------------------------------------

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.vppstart_lock = asyncio.Lock()

    #--------------------------------------------------------------------------
    # Resource lifecycle
    #--------------------------------------------------------------------------

    @inherit_parent
    def __subresources__(self):
        app_armor_file = TextFile(node = self.node,
                filename = FN_APPARMOR_DPDK_SCRIPT,
                content = TPL_APPARMOR_DPDK_SCRIPT,
                overwrite = True)
        return app_armor_file

    @inherit_parent
    @task
    def __get__(self):
        """
        This method always assumes the resource does not exist, since it is not
        an issue to perform the modprobe call everytime.
        """
        raise ResourceNotFound

    @inherit_parent
    def __create__(self):
        modules = BashTask(self.node, CMD_INSERT_MODULES)
        app_armor_reload = BashTask(self.node, CMD_APP_ARMOR_RELOAD)
        sysctl_hugepages = BashTask(self.node, CMD_SYSCTL_HUGEPAGES,
                {'nb_hp': DEFAULT_NB_HUGEPAGES})

        create_uio = EmptyTask()
        for device in self.dpdk_devices:
            create_uio = create_uio > BashTask(self.node,
                    CMD_CREATE_UIO_DEVICES, {'pci_address' : device})

        return ((modules | app_armor_reload) | sysctl_hugepages) > \
            create_uio

    #--------------------------------------------------------------------------
    # Attributes
    #--------------------------------------------------------------------------

    def _get_uio_devices(self):
        def parse(rv):
            return rv.stdout.splitlines()
        return BashTask(self.node, CMD_GREP_UIO_DEV, parse = parse)