aboutsummaryrefslogtreecommitdiffstats
path: root/netmodel/network/flow_table.py
blob: 2da40d1c03430816308e4a773fd9298a42b1df5a (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
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
#!/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 copy
from collections            import defaultdict, Counter

from netmodel.model.query   import Query, ACTION_SUBSCRIBE, ACTION_UNSUBSCRIBE

# Per-interface flow table
class SubFlowTable:
    def __init__(self, interface):
        # Interface to which this flow table is associated
        self._interface = interface

        # Flow -> ingress interface
        self._flows = defaultdict(set)

    def add(self, packet, ingress_interface):
        flow = packet.get_flow()
        self._flows[flow].add(ingress_interface)

    def match(self, packet):
        flow = packet.get_flow()
        return self._flows.get(flow.get_reverse())

    def _on_interface_delete(self, interface):
        """
        Returns:
            False is the flow table is empty.
        """
        to_remove = set()
        for flow, ingress_interfaces in self._flows.items():
            ingress_interfaces.discard(interface)
            if not ingress_interfaces:
                to_remove.add(flow)
        for flow in to_remove:
            del self._flows[flow]

        return len(self._flows) > 0

class Subscription:
    def __init__(self, packet, ingress_set, egress_set):
        """
        Args:
            packet : subscription packet
            ingress_list (List[Interface]) : list of ingress interface
            egress_list (List[Interface]) : list of egress interface
        """
        self._packet = packet
        #self._ingress_set = ingress_set
        #self._egress_set = egress_set

    def get_tuple(self):
        return (self._packet,)

    def __eq__(self, other):
        return self.get_tuple() == other.get_tuple()

    def __hash__(self):
        return hash(self.get_tuple())

    def __repr__(self):
        return '<Subscription {}>'.format(self._packet.to_query())

    __str__ = __repr__

class FlowTable:
    """
    The flow table managed flow association between packets, as well as the
    subscription state for each flow.

    Event management
    ================

    The flow table has to be notified from netmodel.network.interface becomes
    up, and when it is deleted. This is handled through the following events:

    _on_interface_up
        . resend any pending subscription on the interface
    _on_interface_deleted
        . delete any subscription
    """

    def __init__(self):
        # Per interface (sub) flow tables
        #  flow are added when forwarded using FIB
        #  matched upon returning
        self._sub_flow_tables = dict()

        # The Flow Table also maintains a list of subscriptions doubly indexed
        # by both subscriptors, and egress interfaces

        # ingress_interface -> bag of subscriptions
        self._ingress_subscriptions = defaultdict(Counter)

        # egress_interface -> bag of subscriptions
        self._egress_subscriptions = defaultdict(Counter)

    def dump(self, msg=''):
        print("="*80)
        print("FLOW TABLE {}".format(msg))
        print("-" * 80)
        print("SubFlowTables")
        for interface, flow_table in self._sub_flow_tables.items():
            for k, v in flow_table._flows.items():
                print(interface, "\t", k, "\t", v)
        print("-" * 80)
        print("Ingress subscriptions")
        for interface, subscriptions in self._ingress_subscriptions.items():
            print(interface, "\t", subscriptions)
        print("-" * 80)
        print("Egress subscriptions")
        for interface, subscriptions in self._egress_subscriptions.items():
            print(interface, "\t", subscriptions)
        print("=" * 80)
        print("")

    def match(self, packet, interface):
        """
        Check whether the packet arriving on interface is a reply.

        Returns:
            interface that originally requested the packet
            None if not found
        """
        sub_flow_table = self._sub_flow_tables.get(interface)
        if not sub_flow_table:
            return None
        return sub_flow_table.match(packet)

    def add(self, packet, ingress_interface, interfaces):
        for interface in interfaces:
            sub_flow_table = self._sub_flow_tables.get(interface)
            if not sub_flow_table:
                sub_flow_table = SubFlowTable(interface)
                self._sub_flow_tables[interface] = sub_flow_table
            sub_flow_table.add(packet, ingress_interface)

        # If the flow is a subscription, we need to associate it to the list
        query = packet.to_query()
        if query.action == ACTION_SUBSCRIBE:
            # XXX we currently don't merge subscriptions, and assume a single
            # next hop interface
            s = Subscription(packet, set([ingress_interface]), interfaces)

            if ingress_interface:
                self._ingress_subscriptions[ingress_interface] += Counter([s])
            for interface in interfaces:
                self._egress_subscriptions[interface] += Counter([s])

        elif query.action == ACTION_UNSUBSCRIBE:
            raise NotImplementedError

    # Events

    def _on_interface_up(self, interface):
        """
        Callback: an interface gets back up after downtime.

        Resend all pending subscriptions when an interface comes back up.
        """
        subscriptions = self._egress_subscriptions.get(interface)
        if not subscriptions:
            return
        for s in subscriptions:
            interface.send(s._packet)

    def _on_interface_delete(self, interface):
        """
        Callback: an interface has been deleted

        Cancel all subscriptions that have been issues from
            netmodel.network.interface.
        Remove all pointers to subscriptions pending on this interface
        """

        if interface in self._ingress_subscriptions:
            # If the interface we delete at the origin of the subscription,
            # let's also remove corresponding egress subscriptions
            subs = self._ingress_subscriptions[interface]
            if not subs:
                return

            to_remove = set()
            for _interface, subscriptions in self._egress_subscriptions.items():

                removed = subs & subscriptions
                if removed:
                    for s in removed:
                        # We found a subscription of this interface on an other
                        # interface; send unsubscribe...
                        action, params = s._packet.payload
                        p = copy.deepcopy(s._packet)
                        p.payload = (ACTION_UNSUBSCRIBE, params)
                        _interface.send(p)
                    # ... and remove them
                    subscriptions -= removed

                    # if the interface has no more subscription remove it.
                    if not subscriptions:
                        to_remove.add(_interface)

            for i in to_remove:
                del self._egress_subscriptions[i]

            del self._ingress_subscriptions[interface]

        # Remove interface from flow table destination
        to_remove = set()
        for _interface, sub_flow_table in self._sub_flow_tables.items():
            remove = sub_flow_table._on_interface_delete(interface)
            if not remove:
                to_remove.add(_interface)
        for _interface in to_remove:
            del self._sub_flow_tables[_interface]