File: ofctl_nicira_ext.py

package info (click to toggle)
python-os-ken 3.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 21,280 kB
  • sloc: python: 100,620; erlang: 14,517; ansic: 594; sh: 338; makefile: 136
file content (159 lines) | stat: -rw-r--r-- 5,697 bytes parent folder | download | duplicates (2)
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
# Copyright (C) 2016 Rackspace US, Inc.
#
# 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 base64
import logging

from os_ken.ofproto import nicira_ext


LOG = logging.getLogger(__name__)


def action_to_str(act, ofctl_action_to_str):
    sub_type = act.subtype

    if sub_type == nicira_ext.NXAST_RESUBMIT:
        return 'NX_RESUBMIT: {port: %s, table: %s}' % (act.in_port,
                                                       act.table_id)

    elif sub_type == nicira_ext.NXAST_REG_MOVE:
        src_start = act.src_ofs
        dst_start = act.dst_ofs
        src_end = src_start + act.n_bits
        dst_end = dst_start + act.n_bits
        return 'NX_MOVE: {%s[%s..%s]: %s[%s..%s]}' % (act.dst_field, dst_start,
                                                      dst_end, act.src_field,
                                                      src_start, src_end)

    elif sub_type == nicira_ext.NXAST_REG_LOAD:
        return 'NX_LOAD: {%s%s: %x}' % (act.dst, nicira_ext.ofs_nbits_str(act.ofs_nbits), act.value)

    elif sub_type == nicira_ext.NXAST_LEARN:
        specs = []
        add_spec = specs.append

        for spec in act.specs:
            dst_type = spec._dst_type

            if dst_type == 0:  # match
                if isinstance(spec.src, (tuple, list)):
                    src = spec.src[0]
                    start = spec.src[1]
                    end = start + spec.n_bits
                    start_end = '%s..%s' % (start, end)

                else:
                    src = spec.src
                    start_end = '[]'

                add_spec('%s[%s]' % (src, start_end))

            elif dst_type == 1:  # load
                if isinstance(spec.src, (tuple, list)):
                    src = spec.src[0]
                    start = spec.src[1]
                    end = start + spec.n_bits
                    src_start_end = '[%s..%s]' % (start, end)

                else:
                    src = spec.src
                    src_start_end = '[]'

                if isinstance(spec.dst, (tuple, list)):
                    dst = spec.dst[0]
                    start = spec.dst[1]
                    end = start + spec.n_bits
                    dst_start_end = '[%s..%s]' % (start, end)

                else:
                    dst = spec.dst
                    dst_start_end = '[]'

                add_spec('NX_LOAD {%s%s: %s%s}' % (dst, dst_start_end,
                                                   src, src_start_end))

            elif dst_type == 2:  # output
                if isinstance(spec.src, (tuple, list)):
                    src = spec.src[0]
                    start = spec.src[1]
                    end = start + spec.n_bits
                    start_end = '%s..%s' % (start, end)

                else:
                    src = spec.src
                    start_end = '[]'

                add_spec('output:%s%s' % (src, start_end))

        return ('NX_LEARN: {idle_timeout: %s, '
                'hard_timeouts: %s, '
                'priority: %s, '
                'cookie: %s, '
                'flags: %s, '
                'table_id: %s, '
                'fin_idle_timeout: %s, '
                'fin_hard_timeout: %s, '
                'specs: %s}' %
                (act.idle_timeout,
                 act.hard_timeout,
                 act.priority,
                 act.cookie,
                 act.flags,
                 act.table_id,
                 act.fin_idle_timeout,
                 act.self.fin_hard_timeout,
                 specs))

    elif sub_type == nicira_ext.NXAST_CONJUNCTION:
        return ('NX_CONJUNCTION: {clause: %s, number_of_clauses: %s, id: %s}' %
                (act.clause, act.n_clauses, act.id))

    elif sub_type == nicira_ext.NXAST_CT:
        if act.zone_ofs_nbits != 0:
            start = act.zone_ofs_nbits
            end = start + 16
            zone = act.zone_src + ('[%s..%s]' % (start, end))

        else:
            zone = act.zone_src

        actions = [ofctl_action_to_str(action) for action in act.actions]

        return ('NX_CT: {flags: %s, '
                'zone: %s, '
                'table: %s, '
                'alg: %s, '
                'actions: %s}' % (act.flags, zone, act.recirc_table, act.alg,
                                  actions))

    elif sub_type == nicira_ext.NXAST_NAT:
        return ('NX_NAT: {flags: %s, '
                'range_ipv4_min: %s, '
                'range_ipv4_max: %s, '
                'range_ipv6_min: %s, '
                'range_ipv6_max: %s, '
                'range_proto_min: %s, '
                'range_proto_max: %s}' % (act.flags,
                                          act.range_ipv4_min,
                                          act.range_ipv4_max,
                                          act.range_ipv6_min,
                                          act.range_ipv6_max,
                                          act.range_proto_min,
                                          act.range_proto_max))

    data_str = base64.b64encode(act.data)
    return 'NX_UNKNOWN: {subtype: %s, data: %s}' % (sub_type,
                                                    data_str.decode('utf-8'))