DPDK logo

Elixir Cross Referencer

  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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
# SPDX-License-Identifier: BSD-3-Clause
# Copyright(c) 2018 Nippon Telegraph and Telephone Corporation

import bottle
import errno
import json
import logging
import netaddr
import re
import socket
import subprocess
import sys

import spp_proc

PORT_TYPES = ["phy", "vhost", "ring", "pcap", "nullpmd", "tap"]
VF_PORT_TYPES = ["phy", "vhost", "ring"]
# TODO(yasufum) consider PCAP_PORT_TYPES is required.

LOG = logging.getLogger(__name__)


class KeyRequired(bottle.HTTPError):

    def __init__(self, key):
        msg = "key(%s) required." % key
        super(KeyRequired, self).__init__(400, msg)


class KeyInvalid(bottle.HTTPError):

    def __init__(self, key, value):
        msg = "invalid key(%s): %s." % (key, value)
        super(KeyRequired, self).__init__(400, msg)


class BaseHandler(bottle.Bottle):
    """Define common methods for each handler."""

    def __init__(self, controller):
        super(BaseHandler, self).__init__()
        self.ctrl = controller

        self.default_error_handler = self._error_handler
        bottle.response.default_status = 404

    def _error_handler(self, res):
        # use "text/plain" as content_type rather than bottle's default
        # "html".
        res.content_type = "text/plain"
        return res.body

    def _validate_port(self, port):
        try:
            if_type, if_num = port.split(":")
            if if_type not in PORT_TYPES:
                raise
            int(if_num)
        except Exception:
            raise KeyInvalid('port', port)

    def log_url(self):
        LOG.info("%s %s called", bottle.request.method, bottle.request.path)

    def log_response(self):
        LOG.info("response: %s", bottle.response.status)

    # following three decorators do common works for each API.
    # each handler 'install' appropriate decorators.
    #
    def get_body(self, func):
        """Get body and set it to method argument.
        content-type is OK whether application/json or plain text.
        """
        def wrapper(*args, **kwargs):
            req = bottle.request
            if req.method in ["POST", "PUT"]:
                if req.get_header('Content-Type') == "application/json":
                    body = req.json
                else:
                    body = json.loads(req.body.read().decode())
                kwargs['body'] = body
                LOG.info("body: %s", body)
            return func(*args, **kwargs)
        return wrapper

    def check_sec_id(self, func):
        """Get and check proc and set it to method argument."""
        def wrapper(*args, **kwargs):
            sec_id = kwargs.pop('sec_id', None)
            if sec_id is not None:
                proc = self.ctrl.procs.get(sec_id)
                if proc is None or proc.type != self.type:
                    raise bottle.HTTPError(404,
                                           "sec_id %d not found." % sec_id)
                kwargs['proc'] = proc
            return func(*args, **kwargs)
        return wrapper

    def make_response(self, func):
        """Convert plain response to bottle.HTTPResponse."""
        def wrapper(*args, **kwargs):
            ret = func(*args, **kwargs)
            if ret is None:
                return bottle.HTTPResponse(status=204)
            else:
                r = bottle.HTTPResponse(status=200, body=json.dumps(ret))
                r.content_type = "application/json"
                return r
        return wrapper


class WebServer(BaseHandler):
    """Top level handler.

    handlers are hierarchized using 'mount' as follows:
    /          WebServer
    /v1          V1Handler
       /vfs        V1VFHandler
       /mirrors    V1MirrorHandler
       /nfvs       V1NFVHandler
       /primary    V1PrimaryHandler
       /pcaps      V1PcapHandler
    """

    def __init__(self, controller, host, api_port):
        super(WebServer, self).__init__(controller)
        self.host = host
        self.api_port = api_port

        self.mount("/v1", V1Handler(controller))

        # request and response logging.
        self.add_hook("before_request", self.log_url)
        self.add_hook("after_request", self.log_response)

    def start(self):
        self.run(server='eventlet', host=self.host, port=self.api_port,
                 quiet=True)


class V1Handler(BaseHandler):
    def __init__(self, controller):
        super(V1Handler, self).__init__(controller)

        self.set_route()

        self.mount("/vfs", V1VFHandler(controller))
        self.mount("/mirrors", V1MirrorHandler(controller))
        self.mount("/nfvs", V1NFVHandler(controller))
        self.mount("/primary", V1PrimaryHandler(controller))
        self.mount("/pcaps", V1PcapHandler(controller))

        self.install(self.make_response)

    def set_route(self):
        self.route('/processes', 'GET', callback=self.get_processes)
        self.route('/cpu_usage', 'GET', callback=self.get_cpu_usage)
        self.route('/cpu_layout', 'GET', callback=self.get_cpu_layout)

    def get_processes(self):
        LOG.info("get processes called.")
        return self.ctrl.get_processes()

    def get_cpu_usage(self):
        LOG.info("get cpu usage called.")
        return self.ctrl.get_cpu_usage()

    def get_cpu_layout(self):
        LOG.info("get cpu layout called.")
        return self.ctrl.get_cpu_layout()


class V1VFCommon(object):
    """Define common methods for vf and mirror handler."""

    def convert_info(self, data):
        info = data["info"]
        vf = {}
        vf["client-id"] = info["client-id"]
        vf["ports"] = []
        for key in VF_PORT_TYPES:
            for idx in info[key]:
                vf["ports"].append(key + ":" + str(idx))
        vf["master-lcore"] = info["master-lcore"]
        vf["components"] = info["core"]
        if "classifier_table" in info:
            vf["classifier_table"] = info["classifier_table"]

        return vf

    def validate_comp_start(self, body, types):
        for key in ['name', 'core', 'type']:
            if key not in body:
                raise KeyRequired(key)
        if not isinstance(body['name'], str):
            raise KeyInvalid('name', body['name'])
        if not isinstance(body['core'], int):
            raise KeyInvalid('core', body['core'])
        if body['type'] not in types:
            raise KeyInvalid('type', body['type'])

    def validate_comp_port(self, body):
        for key in ['action', 'port', 'dir']:
            if key not in body:
                raise KeyRequired(key)
        if body['action'] not in ["attach", "detach"]:
            raise KeyInvalid('action', body['action'])
        if body['dir'] not in ["rx", "tx"]:
            raise KeyInvalid('dir', body['dir'])
        self._validate_port(body['port'])

    def vf_exit(self, proc):
        self.ctrl.do_exit(proc.type, proc.id)
        proc.do_exit()


class V1VFHandler(BaseHandler, V1VFCommon):

    def __init__(self, controller):
        super(V1VFHandler, self).__init__(controller)
        self.type = spp_proc.TYPE_VF

        self.set_route()

        self.install(self.check_sec_id)
        self.install(self.get_body)
        self.install(self.make_response)

    def set_route(self):
        self.route('/<sec_id:int>', 'GET', callback=self.vf_get)
        self.route('/<sec_id:int>', 'DELETE', callback=self.vf_exit)
        self.route('/<sec_id:int>/components', 'POST',
                   callback=self.vf_comp_start)
        self.route('/<sec_id:int>/components/<name>', 'DELETE',
                   callback=self.vf_comp_stop)
        self.route('/<sec_id:int>/components/<name>/ports', 'PUT',
                   callback=self.vf_comp_port)
        self.route('/<sec_id:int>/classifier_table', 'PUT',
                   callback=self.vf_classifier)

    def vf_get(self, proc):
        return self.convert_info(proc.get_status())

    def vf_comp_start(self, proc, body):
        self.validate_comp_start(body, ["forward", "merge", "classifier"])
        proc.start_component(body['name'], body['core'], body['type'])

    def vf_comp_stop(self, proc, name):
        proc.stop_component(name)

    def _validate_vf_comp_port(self, body):
        self.validate_comp_port(body)
        if body['action'] == "attach":
            vlan = body.get('vlan')
            if vlan:
                try:
                    if vlan['operation'] not in ["none", "add", "del"]:
                        raise
                    if vlan['operation'] == "add":
                        int(vlan['id'])
                        int(vlan['pcp'])
                except Exception:
                    raise KeyInvalid('vlan', vlan)

    def vf_comp_port(self, proc, name, body):
        self._validate_vf_comp_port(body)

        if body['action'] == "attach":
            op = "none"
            vlan_id = 0
            pcp = 0
            vlan = body.get('vlan')
            if vlan:
                if vlan['operation'] == "add":
                    op = "add_vlantag"
                    vlan_id = vlan['id']
                    pcp = vlan['pcp']
                elif vlan['operation'] == "del":
                    op = "del_vlantag"
            proc.port_add(body['port'], body['dir'],
                          name, op, vlan_id, pcp)
        else:
            proc.port_del(body['port'], body['dir'], name)

    def _validate_mac(self, mac_address):
        try:
            netaddr.EUI(mac_address)
        except Exception:
            raise KeyInvalid('mac_address', mac_address)

    def _validate_vf_classifier(self, body):
        for key in ['action', 'type', 'port', 'mac_address']:
            if key not in body:
                raise KeyRequired(key)
        if body['action'] not in ["add", "del"]:
            raise KeyInvalid('action', body['action'])
        if body['type'] not in ["mac", "vlan"]:
            raise KeyInvalid('type', body['type'])
        self._validate_port(body['port'])

        if not body['mac_address'] == 'default':
            self._validate_mac(body['mac_address'])

        if body['type'] == "vlan":
            try:
                int(body['vlan'])
            except Exception:
                raise KeyInvalid('vlan', body.get('vlan'))

    def vf_classifier(self, proc, body):
        self._validate_vf_classifier(body)

        port = body['port']
        mac_address = body['mac_address']

        if body['action'] == "add":
            if body['type'] == "mac":
                proc.set_classifier_table(mac_address, port)
            else:
                proc.set_classifier_table_with_vlan(
                    mac_address, port, body['vlan'])
        else:
            if body['type'] == "mac":
                proc.clear_classifier_table(mac_address, port)
            else:
                proc.clear_classifier_table_with_vlan(
                    mac_address, port, body['vlan'])


class V1MirrorHandler(BaseHandler, V1VFCommon):

    def __init__(self, controller):
        super(V1MirrorHandler, self).__init__(controller)
        self.type = spp_proc.TYPE_MIRROR

        self.set_route()

        self.install(self.check_sec_id)
        self.install(self.get_body)
        self.install(self.make_response)

    def set_route(self):
        self.route('/<sec_id:int>', 'GET', callback=self.mirror_get)
        self.route('/<sec_id:int>', 'DELETE', callback=self.vf_exit)
        self.route('/<sec_id:int>/components', 'POST',
                   callback=self.mirror_comp_start)
        self.route('/<sec_id:int>/components/<name>', 'DELETE',
                   callback=self.mirror_comp_stop)
        self.route('/<sec_id:int>/components/<name>/ports', 'PUT',
                   callback=self.mirror_comp_port)

    def mirror_get(self, proc):
        return self.convert_info(proc.get_status())

    def mirror_comp_start(self, proc, body):
        self.validate_comp_start(body, ["mirror"])
        proc.start_component(body['name'], body['core'], body['type'])

    def mirror_comp_stop(self, proc, name):
        proc.stop_component(name)

    def mirror_comp_port(self, proc, name, body):
        self.validate_comp_port(body)
        if body['action'] == "attach":
            proc.port_add(body['port'], body['dir'], name)
        else:
            proc.port_del(body['port'], body['dir'], name)


class V1NFVHandler(BaseHandler):

    def __init__(self, controller):
        super(V1NFVHandler, self).__init__(controller)
        self.type = spp_proc.TYPE_NFV

        self.set_route()

        self.install(self.check_sec_id)
        self.install(self.get_body)
        self.install(self.make_response)

    def set_route(self):
        self.route('/<sec_id:int>', 'GET', callback=self.nfv_get)
        self.route('/<sec_id:int>', 'DELETE', callback=self.nfv_exit)
        self.route('/<sec_id:int>/forward', 'PUT',
                   callback=self.nfv_forward)
        self.route('/<sec_id:int>/ports', 'PUT',
                   callback=self.nfv_port)
        self.route('/<sec_id:int>/patches', 'PUT',
                   callback=self.nfv_patch_add)
        self.route('/<sec_id:int>/patches', 'DELETE',
                   callback=self.nfv_patch_del)

    def nfv_get(self, proc):
        return proc.get_status()

    def _validate_nfv_forward(self, body):
        if 'action' not in body:
            raise KeyRequired('action')
        if body['action'] not in ["start", "stop"]:
            raise KeyInvalid('action', body['action'])

    def nfv_forward(self, proc, body):
        if body['action'] == "start":
            proc.forward()
        else:
            proc.stop()

    def _validate_nfv_port(self, body):
        for key in ['action', 'port']:
            if key not in body:
                raise KeyRequired(key)
        if body['action'] not in ["add", "del"]:
            raise KeyInvalid('action', body['action'])
        self._validate_port(body['port'])

    def nfv_port(self, proc, body):
        self._validate_nfv_port(body)

        if body['action'] == "add":
            proc.port_add(body['port'])
        else:
            proc.port_del(body['port'])

    def _validate_nfv_patch(self, body):
        for key in ['src', 'dst']:
            if key not in body:
                raise KeyRequired(key)
        self._validate_port(body['src'])
        self._validate_port(body['dst'])

    def nfv_patch_add(self, proc, body):
        self._validate_nfv_patch(body)
        proc.patch_add(body['src'], body['dst'])

    def nfv_patch_del(self, proc):
        proc.patch_reset()

    def nfv_exit(self, proc):
        self.ctrl.do_exit(proc.type, proc.id)
        proc.do_exit()


class V1PrimaryHandler(BaseHandler):

    def __init__(self, controller):
        super(V1PrimaryHandler, self).__init__(controller)

        self.set_route()

        self.install(self.make_response)
        self.install(self.get_body)

    def set_route(self):
        self.route('/status', 'GET', callback=self.get_status)
        self.route('/status', 'DELETE', callback=self.clear_status)
        self.route('/forward', 'PUT', callback=self.nfv_forward)
        self.route('/ports', 'PUT', callback=self.primary_port)
        self.route('/patches', 'PUT', callback=self.nfv_patch_add)
        self.route('/patches', 'DELETE', callback=self.nfv_patch_del)
        self.route('/launch', 'PUT', callback=self.launch_sec_proc)
        self.route('/', 'DELETE', callback=self.pri_exit)

    def _get_proc(self):
        """Get Primary object for requesting.

        This method is call everytime received request via REST API
        to confirm primary is alive.
        """

        proc = self.ctrl.procs.get(spp_proc.ID_PRIMARY)
        if proc is None:
            raise bottle.HTTPError(404, "primary not found.")
        return proc

    def convert_status(self, data):
        stat = {}
        try:
            stat = json.loads(data)
        except json.JSONDecodeError as e:
            print("%s" % e)
        return stat

    def get_status(self):
        proc = self._get_proc()
        return proc.get_status()

    def clear_status(self):
        proc = self._get_proc()
        proc.clear()

    # TODO(yasufum) change name `nfv` and make it to shared method
    def _validate_nfv_forward(self, body):
        if 'action' not in body:
            raise KeyRequired('action')
        if body['action'] not in ["start", "stop"]:
            raise KeyInvalid('action', body['action'])

    # TODO(yasufum) change name `nfv` and make it to shared method
    def nfv_forward(self, body):
        proc = self._get_proc()
        if body['action'] == "start":
            proc.forward()
        else:
            proc.stop()

    # TODO(yasufum) change name `nfv` and make it to shared method
    def _validate_nfv_port(self, body):
        for key in ['action', 'port']:
            if key not in body:
                raise KeyRequired(key)
        if body['action'] not in ["add", "del"]:
            raise KeyInvalid('action', body['action'])
        self._validate_port(body['port'])

    def primary_port(self, body):
        self._validate_nfv_port(body)
        proc = self._get_proc()

        if body['action'] == "add":
            proc.port_add(body['port'])
        else:
            proc.port_del(body['port'])

    # TODO(yasufum) change name `nfv` and make it to shared method
    def _validate_nfv_patch(self, body):
        for key in ['src', 'dst']:
            if key not in body:
                raise KeyRequired(key)
        self._validate_port(body['src'])
        self._validate_port(body['dst'])

    # TODO(yasufum) change name `nfv` and make it to shared method
    def nfv_patch_add(self, body):
        proc = self._get_proc()
        self._validate_nfv_patch(body)
        proc.patch_add(body['src'], body['dst'])

    # TODO(yasufum) change name `nfv` and make it to shared method
    def nfv_patch_del(self):
        proc = self._get_proc()
        proc.patch_reset()

    def launch_sec_proc(self, body):  # the arg should be "body"
        for key in ['client_id', 'proc_name', 'eal', 'app']:
            if key not in body:
                raise KeyRequired(key)

        proc = self._get_proc()
        proc.do_launch_sec_proc(body)

    def pri_exit(self):
        proc = self._get_proc()
        self.ctrl.do_exit(proc.type, proc.id)
        proc.do_exit()


class V1PcapHandler(BaseHandler):

    def __init__(self, controller):
        super(V1PcapHandler, self).__init__(controller)
        self.type = spp_proc.TYPE_PCAP

        self.set_route()

        self.install(self.check_sec_id)
        self.install(self.get_body)
        self.install(self.make_response)

    def set_route(self):
        self.route('/<sec_id:int>', 'GET', callback=self.pcap_get)
        self.route('/<sec_id:int>', 'DELETE', callback=self.pcap_exit)
        self.route('/<sec_id:int>/capture', 'PUT', callback=self.pcap_action)

    def pcap_get(self, proc):
        return proc.get_status()["info"]

    def _validate_pcap_action(self, body):
        if 'action' not in body:
            raise KeyRequired('action')
        if body['action'] not in ["start", "stop"]:
            raise KeyInvalid('action', body['action'])

    def pcap_action(self, proc, body):
        self._validate_pcap_action(body)
        if body['action'] == "start":
            proc.start()
        else:
            proc.stop()

    def pcap_exit(self, proc):
        self.ctrl.do_exit(proc.type, proc.id)
        proc.do_exit()