Skip to content

Commit

Permalink
Merge pull request #3444 from anarkiwi/lru4
Browse files Browse the repository at this point in the history
Cache match creation and some MAC/IP matching.
  • Loading branch information
anarkiwi authored Feb 2, 2020
2 parents 0ec402e + 0d78839 commit 863b5a9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
3 changes: 3 additions & 0 deletions faucet/valve_flood.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import functools
from collections import defaultdict

from faucet import valve_of
Expand Down Expand Up @@ -85,6 +86,7 @@ def floods_to_root(self, _dp_obj):
"""Return True if the given dp floods (only) to root switch"""
return False

@functools.lru_cache(maxsize=1024)
def _mask_flood_priority(self, eth_dst_mask):
return self.flood_priority + valve_packet.mac_mask_bits(eth_dst_mask)

Expand Down Expand Up @@ -127,6 +129,7 @@ def _build_flood_rule(self, match, command, flood_acts, flood_priority):
inst=[valve_of.apply_actions(flood_acts)],
priority=flood_priority)

@functools.lru_cache(maxsize=1024)
def _vlan_flood_priority(self, eth_type, eth_dst_mask):
priority = self._mask_flood_priority(eth_dst_mask)
if eth_type:
Expand Down
5 changes: 5 additions & 0 deletions faucet/valve_of.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ def output_non_output_actions(flood_acts):
return (deduped_acts, output_ports, nonoutput_actions)


@functools.lru_cache()
def output_in_port():
"""Return OpenFlow action to output out input port.
Expand All @@ -546,6 +547,7 @@ def output_in_port():
return output_port(OFP_IN_PORT)


@functools.lru_cache()
def output_controller(max_len=MAX_PACKET_IN_BYTES):
"""Return OpenFlow action to packet in to the controller.
Expand Down Expand Up @@ -587,6 +589,7 @@ def packetout(port_num, data):
return packetouts([port_num], data)


@functools.lru_cache()
def barrier():
"""Return OpenFlow barrier request.
Expand All @@ -612,6 +615,7 @@ def match(match_fields):
return parser.OFPMatch(**match_fields)


@functools.lru_cache()
def valve_match_vid(value):
return to_match_vid(value, ofp.OFPVID_PRESENT)

Expand Down Expand Up @@ -681,6 +685,7 @@ def _match_ip_masked(ipa):
return (str(ipa.ip), str(ipa.netmask))


@functools.lru_cache(maxsize=1024)
def build_match_dict(in_port=None, vlan=None, eth_type=None, eth_src=None,
eth_dst=None, eth_dst_mask=None, icmpv6_type=None,
nw_proto=None, nw_dst=None, metadata=None,
Expand Down
13 changes: 13 additions & 0 deletions faucet/valve_packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,28 @@
MAC_MASK_BITMAP = {(2**EUI_BITS - 2**i): (EUI_BITS - i) for i in range(0, EUI_BITS + 1)}


@functools.lru_cache(maxsize=1024)
def mac_mask_bits(mac_mask):
"""Return number of bits in MAC mask or 0."""
if mac_mask is not None:
return MAC_MASK_BITMAP.get(EUI(mac_mask).value, 0)
return 0


@functools.lru_cache(maxsize=1024)
def int_from_mac(mac):
int_hi, int_lo = [int(i, 16) for i in mac.split(':')[-2:]]
return (int_hi << 8) + int_lo


@functools.lru_cache(maxsize=1024)
def int_in_mac(mac, to_int):
int_mac = mac.split(':')[:4] + [
'%x' % (to_int >> 8), '%x' % (to_int & 0xff)]
return ':'.join(int_mac)


@functools.lru_cache(maxsize=1024)
def ipv4_parseable(ip_header_data):
"""Return True if an IPv4 packet we could parse."""
# TODO: python library parsers are fragile
Expand Down Expand Up @@ -193,6 +197,7 @@ def parse_packet_in_pkt(data, max_len, eth_pkt=None, vlan_pkt=None):
return (pkt, eth_pkt, eth_type, vlan_pkt, vlan_vid)


@functools.lru_cache(maxsize=1024)
def mac_addr_all_zeros(mac_addr):
"""Returns True if mac_addr is all zeros.
Expand All @@ -205,6 +210,7 @@ def mac_addr_all_zeros(mac_addr):
return mac_bin == DONTCARE


@functools.lru_cache(maxsize=1024)
def mac_addr_is_unicast(mac_addr):
"""Returns True if mac_addr is a unicast Ethernet address.
Expand Down Expand Up @@ -372,6 +378,7 @@ def lacp_actor_up(lacp_pkt):
return 0


@functools.lru_cache(maxsize=1024)
def lacp_reqreply(eth_src,
actor_system, actor_key, actor_port,
actor_state_synchronization=0,
Expand Down Expand Up @@ -453,6 +460,7 @@ def lacp_reqreply(eth_src,
return pkt


@functools.lru_cache(maxsize=1024)
def arp_request(vid, eth_src, eth_dst, src_ip, dst_ip):
"""Return an ARP request packet.
Expand All @@ -475,6 +483,7 @@ def arp_request(vid, eth_src, eth_dst, src_ip, dst_ip):
return pkt


@functools.lru_cache(maxsize=1024)
def arp_reply(vid, eth_src, eth_dst, src_ip, dst_ip):
"""Return an ARP reply packet.
Expand Down Expand Up @@ -520,6 +529,7 @@ def echo_reply(vid, eth_src, eth_dst, src_ip, dst_ip, data):
return pkt


@functools.lru_cache(maxsize=1024)
def ipv6_link_eth_mcast(dst_ip):
"""Return an Ethernet multicast address from an IPv6 address.
Expand All @@ -535,6 +545,7 @@ def ipv6_link_eth_mcast(dst_ip):
return mcast_mac


@functools.lru_cache(maxsize=1024)
def ipv6_solicited_node_from_ucast(ucast):
"""Return IPv6 solicited node multicast address from IPv6 unicast address.
Expand All @@ -551,6 +562,7 @@ def ipv6_solicited_node_from_ucast(ucast):
return link_mcast


@functools.lru_cache(maxsize=1024)
def nd_request(vid, eth_src, eth_dst, src_ip, dst_ip):
"""Return IPv6 neighbor discovery request packet.
Expand Down Expand Up @@ -583,6 +595,7 @@ def nd_request(vid, eth_src, eth_dst, src_ip, dst_ip):
return pkt


@functools.lru_cache(maxsize=1024)
def nd_advert(vid, eth_src, eth_dst, src_ip, dst_ip):
"""Return IPv6 neighbor avertisement packet.
Expand Down
2 changes: 2 additions & 0 deletions faucet/valve_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import functools
import hashlib
import struct

Expand Down Expand Up @@ -92,6 +93,7 @@ def set_vlan_vid(self, vlan_vid):

# TODO: verify actions
@staticmethod
@functools.lru_cache(maxsize=1024)
def match(in_port=None, vlan=None, # pylint: disable=too-many-arguments,too-many-locals
eth_type=None, eth_src=None, eth_dst=None, eth_dst_mask=None,
icmpv6_type=None, nw_proto=None, nw_dst=None, metadata=None,
Expand Down

0 comments on commit 863b5a9

Please sign in to comment.