1 # Copyright (C) 2011 Nippon Telegraph and Telephone Corporation.
2 # Copyright (C) 2011 Isaku Yamahata <yamahata at valinux co jp>
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
19 from ryu.exception import MacAddressDuplicated
20 from ryu.lib.mac import haddr_to_str
22 LOG = logging.getLogger('ryu.controller.mac_to_network')
25 class MacToNetwork(object):
26 def __init__(self, nw):
27 super(MacToNetwork, self).__init__()
32 def get_network(self, mac, default=None):
33 return self.mac_to_net.get(mac, default)
35 def add_mac(self, mac, nw_id, nw_id_external=None):
36 _nw_id = self.mac_to_net.get(mac)
40 # allow changing from nw_id_external to known nw id
41 if _nw_id is None or _nw_id == nw_id_external:
42 self.mac_to_net[mac] = nw_id
43 LOG.debug('overwrite nw_id: mac %s nw old %s new %s',
44 haddr_to_str(mac), _nw_id, nw_id)
47 if nw_id == nw_id_external:
48 # this can happens when the packet traverses
49 # VM-> tap-> ovs-> ext-port-> wire-> ext-port-> ovs-> tap-> VM
52 LOG.warning('duplicated nw_id: mac %s nw old %s new %s',
53 haddr_to_str(mac), _nw_id, nw_id)
55 raise MacAddressDuplicated(mac=mac)
57 def del_mac(self, mac):
58 del self.mac_to_net[mac]