1
- """
2
- Inception Cloud ARP module
3
- """
1
+ # -*- coding: utf-8 -*-
2
+
3
+ # vim: tabstop=4 shiftwidth=4 softtabstop=4
4
+
5
+ # Copyright (C) 2014 AT&T Labs All Rights Reserved.
6
+ # Copyright (C) 2014 University of Pennsylvania All Rights Reserved.
7
+ #
8
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may
9
+ # not use this file except in compliance with the License. You may obtain
10
+ # a copy of the License at
11
+ #
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing, software
15
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17
+ # License for the specific language governing permissions and limitations
18
+ # under the License.
4
19
5
20
import logging
6
21
import os
7
22
23
+ from ryu .app import inception_conf as i_conf
24
+ from ryu .app import inception_util as i_util
8
25
from ryu .lib .dpid import dpid_to_str
9
26
from ryu .lib .dpid import str_to_dpid
10
- from ryu .ofproto import ether
11
27
from ryu .lib .packet import arp
12
28
from ryu .lib .packet import ethernet
13
29
from ryu .lib .packet import packet
14
- from ryu .app .inception_util import zk_data_to_tuple
15
- from ryu .app .inception_conf import IP_TO_MAC
16
- from ryu .app .inception_conf import DPID_TO_CONNS
17
- from ryu .app .inception_conf import MAC_TO_DPID_PORT
30
+ from ryu .ofproto import ether
18
31
19
32
LOGGER = logging .getLogger (__name__ )
20
33
21
34
22
35
class InceptionArp (object ):
23
- """
24
- Inception Cloud ARP module for handling ARP packets
25
- """
36
+ """Inception Cloud ARP module for handling ARP packets."""
26
37
27
38
def __init__ (self , inception ):
28
39
self .inception = inception
29
40
30
- def handle (self , dpid , in_port , arp_header , transaction ):
41
+ def handle (self , dpid , in_port , arp_header , txn ):
31
42
LOGGER .info ("Handle ARP packet" )
32
43
33
44
# Do {ip => mac} learning
34
- self ._do_arp_learning (arp_header , transaction )
45
+ self ._do_arp_learning (arp_header , txn )
35
46
# Process arp request
36
47
if arp_header .opcode == arp .ARP_REQUEST :
37
- self ._handle_arp_request (dpid , in_port , arp_header , transaction )
48
+ self ._handle_arp_request (dpid , in_port , arp_header , txn )
38
49
# Process arp reply
39
50
elif arp_header .opcode == arp .ARP_REPLY :
40
- self ._handle_arp_reply (dpid , arp_header , transaction )
51
+ self ._handle_arp_reply (dpid , arp_header , txn )
41
52
42
- def _do_arp_learning (self , arp_header , transaction ):
43
- """
44
- Learn IP => MAC mapping from a received ARP packet, update
45
- ip_to_mac table
53
+ def _do_arp_learning (self , arp_header , txn ):
54
+ """Learn IP => MAC mapping from a received ARP packet, update
55
+ ip_to_mac table.
46
56
"""
47
57
src_ip = arp_header .src_ip
48
58
src_mac = arp_header .src_mac
49
59
50
- if src_ip not in self .inception .zk .get_children (IP_TO_MAC ):
51
- transaction .create (os .path .join (IP_TO_MAC , src_ip ), src_mac )
60
+ if src_ip not in self .inception .zk .get_children (i_conf . IP_TO_MAC ):
61
+ txn .create (os .path .join (i_conf . IP_TO_MAC , src_ip ), src_mac )
52
62
LOGGER .info ("Learn: (ip=%s) => (mac=%s)" , src_ip , src_mac )
53
63
54
- def _handle_arp_request (self , dpid , in_port , arp_header , transaction ):
55
- """
56
- Process ARP request packet
57
- """
64
+ def _handle_arp_request (self , dpid , in_port , arp_header , txn ):
65
+ """Process ARP request packet."""
58
66
src_ip = arp_header .src_ip
59
67
src_mac = arp_header .src_mac
60
68
dst_ip = arp_header .dst_ip
@@ -66,18 +74,18 @@ def _handle_arp_request(self, dpid, in_port, arp_header, transaction):
66
74
LOGGER .info ("ARP request: (ip=%s) query (ip=%s)" , src_ip , dst_ip )
67
75
# If entry not found, broadcast request
68
76
# TODO(Chen): Buffering request? Not needed in a friendly environment
69
- if dst_ip not in self .inception .zk .get_children (IP_TO_MAC ):
77
+ if dst_ip not in self .inception .zk .get_children (i_conf . IP_TO_MAC ):
70
78
LOGGER .info ("Entry for (ip=%s) not found, broadcast ARP request" ,
71
79
dst_ip )
72
80
73
81
arp_request = arp .arp (opcode = arp .ARP_REQUEST ,
74
- dst_mac = 'ff:ff:ff:ff:ff:ff' ,
75
- src_mac = src_mac ,
76
- dst_ip = dst_ip ,
77
- src_ip = src_ip )
82
+ dst_mac = 'ff:ff:ff:ff:ff:ff' ,
83
+ src_mac = src_mac ,
84
+ dst_ip = dst_ip ,
85
+ src_ip = src_ip )
78
86
eth_request = ethernet .ethernet (ethertype = ether .ETH_TYPE_ARP ,
79
- src = arp_header .src_mac ,
80
- dst = 'ff:ff:ff:ff:ff:ff' )
87
+ src = arp_header .src_mac ,
88
+ dst = 'ff:ff:ff:ff:ff:ff' )
81
89
packet_request = packet .Packet ()
82
90
packet_request .add_protocol (eth_request )
83
91
packet_request .add_protocol (arp_request )
@@ -90,7 +98,7 @@ def _handle_arp_request(self, dpid, in_port, arp_header, transaction):
90
98
ports = self .inception .dpset .get_ports (str_to_dpid (dpid ))
91
99
# Sift out ports connecting to hosts but vxlan peers
92
100
vxlan_ports = []
93
- zk_path = os .path .join (DPID_TO_CONNS , dpid )
101
+ zk_path = os .path .join (i_conf . DPID_TO_CONNS , dpid )
94
102
for child in self .inception .zk .get_children (zk_path ):
95
103
zk_path_child = os .path .join (zk_path , child )
96
104
port_no , _ = self .inception .zk .get (zk_path_child )
@@ -110,11 +118,9 @@ def _handle_arp_request(self, dpid, in_port, arp_header, transaction):
110
118
else :
111
119
# Setup data forwarding flows
112
120
result_dst_mac , _ = self .inception .zk .get (
113
- os .path .join (IP_TO_MAC , dst_ip ))
114
- self .inception .setup_switch_fwd_flows (src_mac ,
115
- dpid ,
116
- result_dst_mac ,
117
- transaction )
121
+ os .path .join (i_conf .IP_TO_MAC , dst_ip ))
122
+ self .inception .setup_switch_fwd_flows (src_mac , dpid ,
123
+ result_dst_mac , txn )
118
124
# Construct ARP reply packet and send it to the host
119
125
LOGGER .info ("Hit: (dst_ip=%s) <=> (dst_mac=%s)" ,
120
126
dst_ip , result_dst_mac )
@@ -144,26 +150,21 @@ def _handle_arp_request(self, dpid, in_port, arp_header, transaction):
144
150
arp_reply .dst_ip , arp_reply .dst_mac , in_port ,
145
151
arp_reply .src_ip , arp_reply .src_mac )
146
152
147
- def _handle_arp_reply (self , dpid , arp_header , transaction ):
148
- """
149
- Process ARP reply packet
150
- """
153
+ def _handle_arp_reply (self , dpid , arp_header , txn ):
154
+ """Process ARP reply packet."""
151
155
src_ip = arp_header .src_ip
152
156
src_mac = arp_header .src_mac
153
157
dst_ip = arp_header .dst_ip
154
158
dst_mac = arp_header .dst_mac
155
159
156
160
LOGGER .info ("ARP reply: (ip=%s) answer (ip=%s)" , src_ip , dst_ip )
157
- zk_path = os .path .join (MAC_TO_DPID_PORT , dst_mac )
161
+ zk_path = os .path .join (i_conf . MAC_TO_DPID_PORT , dst_mac )
158
162
if self .inception .zk .exists (zk_path ):
159
163
# If I know to whom to forward back this ARP reply
160
164
dst_dpid_port , _ = self .inception .zk .get (zk_path )
161
- dst_dpid , dst_port = zk_data_to_tuple (dst_dpid_port )
165
+ dst_dpid , dst_port = i_util . str_to_tuple (dst_dpid_port )
162
166
# Setup data forwarding flows
163
- self .inception .setup_switch_fwd_flows (src_mac ,
164
- dpid ,
165
- dst_mac ,
166
- transaction )
167
+ self .inception .setup_switch_fwd_flows (src_mac , dpid , dst_mac , txn )
167
168
# Forward ARP reply
168
169
arp_reply = arp .arp (opcode = arp .ARP_REPLY ,
169
170
dst_mac = dst_mac ,
0 commit comments