Skip to content

Instantly share code, notes, and snippets.

@ishidawataru
Last active December 22, 2016 08:58
Show Gist options
  • Save ishidawataru/1f08ffe12bb2dbc2eef77cb77ef920cd to your computer and use it in GitHub Desktop.
Save ishidawataru/1f08ffe12bb2dbc2eef77cb77ef920cd to your computer and use it in GitHub Desktop.
of-dpa vpws
#! /usr/bin/env python
from OFDPA_python import *
import sys
def addL2UnfilteredInterfaceGroup(port):
group_id = new_uint32_tp()
uint32_tp_assign(group_id, 0)
ofdpaGroupTypeSet(group_id, OFDPA_GROUP_ENTRY_TYPE_L2_UNFILTERED_INTERFACE)
ofdpaGroupPortIdSet(group_id, port)
group = ofdpaGroupEntry_t()
group.groupId = uint32_tp_value(group_id)
rc = ofdpaGroupAdd(group)
if rc != OFDPA_E_NONE:
raise Exception("Unable to add L2 interface group. rc = %d " % (rc))
bucket = ofdpaGroupBucketEntry_t()
bucket.groupId = group.groupId
bucket.bucketIndex = 0
bucket.bucketData.l2UnfilteredInterface.outputPort = port
bucket.bucketData.l2UnfilteredInterface.allowVlanTranslation = 1
rc = ofdpaGroupBucketEntryAdd(bucket)
if rc != OFDPA_E_NONE:
raise Exception("Unable to add L2 interface group bucket. rc = %d " % (rc))
delete_uint32_tp(group_id)
return group.groupId
def addL2InterfaceGroup(vlanID, out_port):
group_id = new_uint32_tp()
uint32_tp_assign(group_id, 0)
ofdpaGroupTypeSet(group_id, OFDPA_GROUP_ENTRY_TYPE_L2_INTERFACE)
ofdpaGroupVlanSet(group_id, vlanID)
ofdpaGroupPortIdSet(group_id, out_port)
group = ofdpaGroupEntry_t()
group.groupId = uint32_tp_value(group_id)
rc = ofdpaGroupAdd(group)
if rc != OFDPA_E_NONE:
raise Exception("Unable to add L2 interface group. rc = %d " % (rc))
bucket = ofdpaGroupBucketEntry_t()
bucket.groupId = group.groupId
bucket.bucketIndex = 0
bucket.bucketData.l2Interface.outputPort = out_port
bucket.bucketData.l2Interface.popVlanTag = 1
rc = ofdpaGroupBucketEntryAdd(bucket)
if rc != OFDPA_E_NONE:
raise Exception("Unable to add L2 interface group bucket. rc = %d " % (rc))
delete_uint32_tp(group_id)
return group.groupId
def addMPLSInterfaceGroup(vlanID, srcMAC, dstMAC, l2GroupID):
group_id = new_uint32_tp()
uint32_tp_assign(group_id, 0)
ofdpaGroupTypeSet(group_id, OFDPA_GROUP_ENTRY_TYPE_MPLS_LABEL)
ofdpaGroupMplsSubTypeSet(group_id, OFDPA_MPLS_INTERFACE)
ofdpaGroupIndexSet(group_id, 1)
group = ofdpaGroupEntry_t()
group.groupId = uint32_tp_value(group_id)
rc = ofdpaGroupAdd(group)
if rc != OFDPA_E_NONE:
raise Exception("Unable to add MPLS interface group. rc = %d " % (rc))
bucket = ofdpaGroupBucketEntry_t()
bucket.groupId = group.groupId
bucket.bucketIndex = 0
bucket.bucketData.mplsInterface.vlanId = (vlanID | OFDPA_VID_PRESENT) # must be tagged
MACAddress_set(bucket.bucketData.mplsInterface.srcMac, srcMAC)
MACAddress_set(bucket.bucketData.mplsInterface.dstMac, dstMAC)
bucket.referenceGroupId = l2GroupID
rc = ofdpaGroupBucketEntryAdd(bucket)
if rc != OFDPA_E_NONE:
raise Exception("Unable to add MPLS interface group bucket. rc = %d " % (rc))
delete_uint32_tp(group_id)
return group.groupId
def addMPLSL2VPNGroup(mplsLabel, mplsTunnelGroupID):
group_id = new_uint32_tp()
uint32_tp_assign(group_id, 0)
ofdpaGroupTypeSet(group_id, OFDPA_GROUP_ENTRY_TYPE_MPLS_LABEL)
ofdpaGroupMplsSubTypeSet(group_id, OFDPA_MPLS_L2_VPN_LABEL)
ofdpaGroupIndexSet(group_id, 0)
group = ofdpaGroupEntry_t()
group.groupId = uint32_tp_value(group_id)
rc = ofdpaGroupAdd(group)
if rc != OFDPA_E_NONE:
raise Exception("Unable to add MPLS L2 VPN group. rc = %d " % (rc))
bucket = ofdpaGroupBucketEntry_t()
bucket.groupId = group.groupId
bucket.bucketIndex = 0
bucket.referenceGroupId = mplsTunnelGroupID
bucket.bucketData.mplsLabel.mplsLabel = mplsLabel
bucket.bucketData.mplsLabel.mplsBOS = 1
bucket.bucketData.mplsLabel.pushMplsHdr = 1
bucket.bucketData.mplsLabel.pushL2Hdr = 1
bucket.bucketData.mplsLabel.pushCW = 1
rc = ofdpaGroupBucketEntryAdd(bucket)
if rc != OFDPA_E_NONE:
raise Exception("Unable to add MPLS L2 VPN group bucket. rc = %d " % (rc))
delete_uint32_tp(group_id)
return group.groupId
def addVLANFlowEntry(port, vid=0, untagged=True):
entry = ofdpaFlowEntry_t()
ofdpaFlowEntryInit(OFDPA_FLOW_TABLE_ID_VLAN, entry)
if vid > 0:
entry.flowData.vlanFlowEntry.gotoTableId = OFDPA_FLOW_TABLE_ID_TERMINATION_MAC
entry.flowData.vlanFlowEntry.match_criteria.inPort = port
entry.flowData.vlanFlowEntry.match_criteria.vlanId = (OFDPA_VID_PRESENT | vid)
entry.flowData.vlanFlowEntry.match_criteria.vlanIdMask = (OFDPA_VID_PRESENT | OFDPA_VID_EXACT_MASK)
else:
entry.flowData.vlanFlowEntry.gotoTableId = OFDPA_FLOW_TABLE_ID_MPLS_L2_PORT
entry.flowData.vlanFlowEntry.match_criteria.inPort = port
entry.flowData.vlanFlowEntry.mplsL2PortAction = 1
entry.flowData.vlanFlowEntry.mplsL2Port = 0x00000010
entry.flowData.vlanFlowEntry.tunnelIdAction = 1
entry.flowData.vlanFlowEntry.tunnelId = 0x00010001
entry.flowData.vlanFlowEntry.mplsTypeAction = 1
entry.flowData.vlanFlowEntry.mplsType = 1
rc = ofdpaFlowAdd(entry)
if rc != OFDPA_E_NONE:
raise Exception("Unable to add VLAN Flow entry. rc = %d " % rc)
if vid > 0 and untagged:
entry.flowData.vlanFlowEntry.match_criteria.vlanId = 0
entry.flowData.vlanFlowEntry.setVlanIdAction = 1
entry.flowData.vlanFlowEntry.newVlanId = (OFDPA_VID_PRESENT | vid)
rc = ofdpaFlowAdd(entry)
if rc != OFDPA_E_NONE:
raise Exception("failed to add VLAN untagged flow entry")
def addMPLSL2PortFlow(l2vpnGroupId):
entry = ofdpaFlowEntry_t()
ofdpaFlowEntryInit(OFDPA_FLOW_TABLE_ID_MPLS_L2_PORT, entry)
entry.flowData.mplsL2PortFlowEntry.match_criteria.mplsL2Port = 0x00000010
entry.flowData.mplsL2PortFlowEntry.match_criteria.mplsL2PortMask = OFDPA_MPLS_L2_PORT_EXACT_MASK
entry.flowData.mplsL2PortFlowEntry.match_criteria.tunnelId = 0x00010001
entry.flowData.mplsL2PortFlowEntry.gotoTableId = OFDPA_FLOW_TABLE_ID_ACL_POLICY
entry.flowData.mplsL2PortFlowEntry.groupId = l2vpnGroupId
rc = ofdpaFlowAdd(entry)
if rc != OFDPA_E_NONE:
raise Exception("Unable to add MPLS L2 Port Flow entry. rc = %d " % rc)
def addTerminationMACFlowEntry(ether_type=0x0800, vid=None, mac=None, port=0):
entry = ofdpaFlowEntry_t()
ofdpaFlowEntryInit(OFDPA_FLOW_TABLE_ID_TERMINATION_MAC, entry)
entry.flowData.terminationMacFlowEntry.match_criteria.etherType = ether_type
if port > 0:
entry.flowData.terminationMacFlowEntry.match_criteria.inPort = port
entry.flowData.terminationMacFlowEntry.match_criteria.inPortMask = OFDPA_INPORT_EXACT_MASK
if vid != None:
entry.flowData.terminationMacFlowEntry.match_criteria.vlanId = (OFDPA_VID_PRESENT | vid)
entry.flowData.terminationMacFlowEntry.match_criteria.vlanIdMask = (OFDPA_VID_PRESENT | OFDPA_VID_EXACT_MASK)
if mac != None:
MACAddress_set(entry.flowData.terminationMacFlowEntry.match_criteria.destMac, mac)
MACAddress_set(entry.flowData.terminationMacFlowEntry.match_criteria.destMacMask, "ff:ff:ff:ff:ff:ff")
if ether_type == 0x0800:
entry.flowData.terminationMacFlowEntry.gotoTableId = OFDPA_FLOW_TABLE_ID_UNICAST_ROUTING
elif ether_type == 0x8847:
entry.flowData.terminationMacFlowEntry.gotoTableId = OFDPA_FLOW_TABLE_ID_MPLS_1
rc = ofdpaFlowAdd(entry)
if rc != OFDPA_E_NONE:
raise Exception("failed to add termination mac flow entry: %d" % rc)
def addMPLS1FlowEntry(gid, label):
entry = ofdpaFlowEntry_t()
ofdpaFlowEntryInit(OFDPA_FLOW_TABLE_ID_MPLS_1, entry)
entry.flowData.mplsFlowEntry.match_criteria.etherType = 0x8847
entry.flowData.mplsFlowEntry.match_criteria.mplsBos = 1
entry.flowData.mplsFlowEntry.match_criteria.mplsLabel = label
entry.flowData.mplsFlowEntry.popLabelAction = 1
entry.flowData.mplsFlowEntry.gotoTableId = OFDPA_FLOW_TABLE_ID_MPLS_TYPE
entry.flowData.mplsFlowEntry.popCwAction = 1
entry.flowData.mplsFlowEntry.popL2HeaderAction = 1
entry.flowData.mplsFlowEntry.mplsType = 1
entry.flowData.mplsFlowEntry.mplsTypeAction = 1
entry.flowData.mplsFlowEntry.mplsL2PortAction = 1
entry.flowData.mplsFlowEntry.mplsL2Port = 0x00020010
entry.flowData.mplsFlowEntry.tunnelIdAction = 1
entry.flowData.mplsFlowEntry.tunnelId = 0x00010001
entry.flowData.mplsFlowEntry.groupID = gid
rc = ofdpaFlowAdd(entry)
if rc != OFDPA_E_NONE:
raise Exception("failed to add MPLS1 flow entry: %d" % rc)
def addACLPolicyFlow(port):
entry = ofdpaFlowEntry_t()
ofdpaFlowEntryInit(OFDPA_FLOW_TABLE_ID_ACL_POLICY, entry)
entry.flowData.policyAclFlowEntry.match_criteria.inPort = port
entry.flowData.policyAclFlowEntry.match_criteria.inPortMask = (OFDPA_INPORT_FIELD_MASK)
entry.flowData.policyAclFlowEntry.match_criteria.etherTypeMask = (OFDPA_ETHERTYPE_ALL_MASK)
entry.flowData.policyAclFlowEntry.outputPort = (OFDPA_PORT_CONTROLLER)
rc = ofdpaFlowAdd(entry)
if rc != OFDPA_E_NONE:
raise Exception("Unable to add ACL Flow entry. rc = %d " % rc)
def main():
rc = ofdpaClientInitialize("vpws")
if rc != OFDPA_E_NONE:
print 'failed to initialize ofdpa'
sys.exit(1)
in_port = 9
out_port = 1
vid = 10
label = 20
src_mac = "7c:fe:90:35:3e:22"
dst_mac = "7c:fe:90:35:3e:23"
group_id = addL2InterfaceGroup(vid, out_port)
group_id = addMPLSInterfaceGroup(vid, src_mac, dst_mac, group_id)
group_id = addMPLSL2VPNGroup(label, group_id)
addVLANFlowEntry(in_port)
addMPLSL2PortFlow(group_id)
addACLPolicyFlow(in_port)
addVLANFlowEntry(out_port, vid)
addTerminationMACFlowEntry(ether_type = 0x8847, mac=src_mac, vid=vid)
group_id = addL2UnfilteredInterfaceGroup(in_port)
addMPLS1FlowEntry(group_id, label)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment