xref: /openbmc/linux/tools/testing/selftests/tc-testing/plugin-lib/buildebpfPlugin.py (revision 8dd06ef34b6e2f41b29fbf5fc1663780f2524285)
14c2d39bdSDavide Caratti'''
24c2d39bdSDavide Carattibuild ebpf program
34c2d39bdSDavide Caratti'''
44c2d39bdSDavide Caratti
54c2d39bdSDavide Carattiimport os
64c2d39bdSDavide Carattiimport signal
74c2d39bdSDavide Carattifrom string import Template
84c2d39bdSDavide Carattiimport subprocess
94c2d39bdSDavide Carattiimport time
104c2d39bdSDavide Carattifrom TdcPlugin import TdcPlugin
114c2d39bdSDavide Carattifrom tdc_config import *
124c2d39bdSDavide Caratti
134c2d39bdSDavide Caratticlass SubPlugin(TdcPlugin):
144c2d39bdSDavide Caratti    def __init__(self):
154c2d39bdSDavide Caratti        self.sub_class = 'buildebpf/SubPlugin'
164c2d39bdSDavide Caratti        self.tap = ''
174c2d39bdSDavide Caratti        super().__init__()
184c2d39bdSDavide Caratti
194c2d39bdSDavide Caratti    def pre_suite(self, testcount, testidlist):
204c2d39bdSDavide Caratti        super().pre_suite(testcount, testidlist)
214c2d39bdSDavide Caratti
224c2d39bdSDavide Caratti        if self.args.buildebpf:
234c2d39bdSDavide Caratti            self._ebpf_makeall()
244c2d39bdSDavide Caratti
254c2d39bdSDavide Caratti    def post_suite(self, index):
264c2d39bdSDavide Caratti        super().post_suite(index)
274c2d39bdSDavide Caratti
284c2d39bdSDavide Caratti        self._ebpf_makeclean()
294c2d39bdSDavide Caratti
304c2d39bdSDavide Caratti    def add_args(self, parser):
314c2d39bdSDavide Caratti        super().add_args(parser)
324c2d39bdSDavide Caratti
334c2d39bdSDavide Caratti        self.argparser_group = self.argparser.add_argument_group(
344c2d39bdSDavide Caratti            'buildebpf',
354c2d39bdSDavide Caratti            'options for buildebpfPlugin')
364c2d39bdSDavide Caratti        self.argparser_group.add_argument(
37489ce2f4SLucas Bates            '--nobuildebpf', action='store_false', default=True,
38489ce2f4SLucas Bates            dest='buildebpf',
39489ce2f4SLucas Bates            help='Don\'t build eBPF programs')
404c2d39bdSDavide Caratti
414c2d39bdSDavide Caratti        return self.argparser
424c2d39bdSDavide Caratti
434c2d39bdSDavide Caratti    def _ebpf_makeall(self):
444c2d39bdSDavide Caratti        if self.args.buildebpf:
454c2d39bdSDavide Caratti            self._make('all')
464c2d39bdSDavide Caratti
474c2d39bdSDavide Caratti    def _ebpf_makeclean(self):
484c2d39bdSDavide Caratti        if self.args.buildebpf:
494c2d39bdSDavide Caratti            self._make('clean')
504c2d39bdSDavide Caratti
514c2d39bdSDavide Caratti    def _make(self, target):
524c2d39bdSDavide Caratti        command = 'make -C {} {}'.format(self.args.NAMES['EBPFDIR'], target)
534c2d39bdSDavide Caratti        proc = subprocess.Popen(command,
544c2d39bdSDavide Caratti            shell=True,
554c2d39bdSDavide Caratti            stdout=subprocess.PIPE,
564c2d39bdSDavide Caratti            stderr=subprocess.PIPE,
57*7145fcffSDavide Caratti            env=os.environ.copy())
584c2d39bdSDavide Caratti        (rawout, serr) = proc.communicate()
594c2d39bdSDavide Caratti
604c2d39bdSDavide Caratti        if proc.returncode != 0 and len(serr) > 0:
614c2d39bdSDavide Caratti            foutput = serr.decode("utf-8")
624c2d39bdSDavide Caratti        else:
634c2d39bdSDavide Caratti            foutput = rawout.decode("utf-8")
644c2d39bdSDavide Caratti
654c2d39bdSDavide Caratti        proc.stdout.close()
664c2d39bdSDavide Caratti        proc.stderr.close()
674c2d39bdSDavide Caratti        return proc, foutput
68