blob: a6e9f3cfc74485c5752e3c6126bc7bcd63948bf4 [file] [log] [blame]
Don Newton98fd8812019-09-23 15:15:02 -04001/*
David K. Bainbridge157bdab2020-01-16 14:38:05 -08002 Copyright 2020 the original author or authors.
Don Newton98fd8812019-09-23 15:15:02 -04003
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
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
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 implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17package openflow
18
19import (
20 "fmt"
ssiddiquidf20bf72021-08-23 14:00:56 +053021 "net"
David K. Bainbridge157bdab2020-01-16 14:38:05 -080022 "strings"
23 "sync"
David K. Bainbridgee05cf0c2021-08-19 03:16:50 +000024
25 ofp "github.com/opencord/goloxi/of13"
26 "github.com/opencord/voltha-protos/v5/go/openflow_13"
Don Newton98fd8812019-09-23 15:15:02 -040027)
28
29var mu sync.Mutex
30var xid uint32 = 1
31
32func GetXid() uint32 {
33 mu.Lock()
34 defer mu.Unlock()
35 xid++
36 return xid
37}
38func PadString(value string, padSize int) string {
39 size := len(value)
40 nullsNeeded := padSize - size
41 null := fmt.Sprintf("%c", '\000')
42 padded := strings.Repeat(null, nullsNeeded)
43 return fmt.Sprintf("%s%s", value, padded)
44}
Don Newtone0d34a82019-11-14 10:58:06 -050045
46func extractAction(action ofp.IAction) *openflow_13.OfpAction {
47 var ofpAction openflow_13.OfpAction
48 switch action.GetType() {
49 case ofp.OFPATOutput:
50 var outputAction openflow_13.OfpAction_Output
51 loxiOutputAction := action.(*ofp.ActionOutput)
52 var output openflow_13.OfpActionOutput
Don Newtonb437c6f2019-12-18 11:51:57 -050053 output.Port = uint32(loxiOutputAction.GetPort())
54 /*
55 var maxLen uint16
56 maxLen = loxiOutputAction.GetMaxLen()
57 output.MaxLen = uint32(maxLen)
58
59 */
60 output.MaxLen = 0
Don Newtone0d34a82019-11-14 10:58:06 -050061 outputAction.Output = &output
62 ofpAction.Action = &outputAction
63 ofpAction.Type = openflow_13.OfpActionType_OFPAT_OUTPUT
64 case ofp.OFPATCopyTtlOut: //CopyTtltOut
65 case ofp.OFPATCopyTtlIn: //CopyTtlIn
66 case ofp.OFPATSetMplsTtl: //SetMplsTtl
ssiddiquidf20bf72021-08-23 14:00:56 +053067 mplsTtl := action.(*ofp.ActionSetMplsTtl)
68 setMplsTtl := openflow_13.OfpAction_MplsTtl{
69 MplsTtl: &openflow_13.OfpActionMplsTtl{
70 MplsTtl: uint32(mplsTtl.MplsTtl),
71 },
72 }
73 ofpAction.Type = openflow_13.OfpActionType_OFPAT_SET_MPLS_TTL
74 ofpAction.Action = &setMplsTtl
Don Newtone0d34a82019-11-14 10:58:06 -050075 case ofp.OFPATDecMplsTtl: //DecMplsTtl
76 case ofp.OFPATPushVLAN: //PushVlan
77 var pushVlan openflow_13.OfpAction_Push
78 loxiPushAction := action.(*ofp.ActionPushVlan)
Don Newtone0d34a82019-11-14 10:58:06 -050079 var push openflow_13.OfpActionPush
80 push.Ethertype = uint32(loxiPushAction.Ethertype) //TODO This should be available in the fields
81 pushVlan.Push = &push
82 ofpAction.Type = openflow_13.OfpActionType_OFPAT_PUSH_VLAN
83 ofpAction.Action = &pushVlan
84 case ofp.OFPATPopVLAN: //PopVlan
85 ofpAction.Type = openflow_13.OfpActionType_OFPAT_POP_VLAN
86 case ofp.OFPATPushMpls: //PushMpls
ssiddiquidf20bf72021-08-23 14:00:56 +053087 var pushMpls openflow_13.OfpAction_Push
88 mplsPushAction := action.(*ofp.ActionPushMpls)
89 var push openflow_13.OfpActionPush
90 push.Ethertype = uint32(mplsPushAction.Ethertype)
91 pushMpls.Push = &push
92 ofpAction.Type = openflow_13.OfpActionType_OFPAT_PUSH_MPLS
93 ofpAction.Action = &pushMpls
Don Newtone0d34a82019-11-14 10:58:06 -050094 case ofp.OFPATPopMpls: //PopMpls
ssiddiquidf20bf72021-08-23 14:00:56 +053095 ofpAction.Type = openflow_13.OfpActionType_OFPAT_POP_MPLS
Don Newtone0d34a82019-11-14 10:58:06 -050096 case ofp.OFPATSetQueue: //SetQueue
97 case ofp.OFPATGroup: //ActionGroup
Jonathan Hart60c5d772020-03-30 18:28:40 -070098 ofpAction.Type = openflow_13.OfpActionType_OFPAT_GROUP
99 group := action.(*ofp.ActionGroup)
100 ofpAction.Action = &openflow_13.OfpAction_Group{
101 Group: &openflow_13.OfpActionGroup{
102 GroupId: group.GroupId,
103 },
104 }
Don Newtone0d34a82019-11-14 10:58:06 -0500105 case ofp.OFPATSetNwTtl: //SetNwTtl
106 case ofp.OFPATDecNwTtl: //DecNwTtl
107 case ofp.OFPATSetField: //SetField
108 ofpAction.Type = openflow_13.OfpActionType_OFPAT_SET_FIELD
109 var ofpAction_SetField openflow_13.OfpAction_SetField
110 var ofpActionSetField openflow_13.OfpActionSetField
111 var ofpOxmField openflow_13.OfpOxmField
112 ofpOxmField.OxmClass = openflow_13.OfpOxmClass_OFPXMC_OPENFLOW_BASIC
113 var ofpOxmField_OfbField openflow_13.OfpOxmField_OfbField
114 var ofpOxmOfbField openflow_13.OfpOxmOfbField
115 loxiSetField := action.(*ofp.ActionSetField)
116 oxmName := loxiSetField.Field.GetOXMName()
117 switch oxmName {
Don Newton7577f072020-01-06 12:41:11 -0500118 //TODO handle set field sith other fields
Don Newtone0d34a82019-11-14 10:58:06 -0500119 case "vlan_vid":
120 ofpOxmOfbField.Type = openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_VLAN_VID
121 var vlanVid openflow_13.OfpOxmOfbField_VlanVid
122 var VlanVid = loxiSetField.Field.GetOXMValue().(uint16)
123 vlanVid.VlanVid = uint32(VlanVid)
Don Newtone0d34a82019-11-14 10:58:06 -0500124 ofpOxmOfbField.Value = &vlanVid
Gamze Abaka3e2b2ce2020-05-09 10:21:40 +0000125 case "vlan_pcp":
126 ofpOxmOfbField.Type = openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_VLAN_PCP
127 var vlanPcp openflow_13.OfpOxmOfbField_VlanPcp
128 var VlanPcp = loxiSetField.Field.GetOXMValue().(uint8)
129 vlanPcp.VlanPcp = uint32(VlanPcp)
130 ofpOxmOfbField.Value = &vlanPcp
ssiddiquidf20bf72021-08-23 14:00:56 +0530131 case "mpls_label":
132 ofpOxmOfbField.Type = openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_MPLS_LABEL
133 var mplsLabel openflow_13.OfpOxmOfbField_MplsLabel
134 label := loxiSetField.Field.GetOXMValue().(uint32)
135 mplsLabel.MplsLabel = label
136 ofpOxmOfbField.Value = &mplsLabel
137 case "mpls_bos":
138 ofpOxmOfbField.Type = openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_MPLS_BOS
139 var mplsBos openflow_13.OfpOxmOfbField_MplsBos
140 bos := loxiSetField.Field.GetOXMValue().(uint8)
141 mplsBos.MplsBos = uint32(bos)
142 ofpOxmOfbField.Value = &mplsBos
143 case "eth_src":
144 ofpOxmOfbField.Type = openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_ETH_SRC
145 var ethSrc openflow_13.OfpOxmOfbField_EthSrc
146 src := loxiSetField.Field.GetOXMValue().(net.HardwareAddr)
147 ethSrc.EthSrc = src
148 ofpOxmOfbField.Value = &ethSrc
149 case "eth_dst":
150 ofpOxmOfbField.Type = openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_ETH_DST
151 var ethDst openflow_13.OfpOxmOfbField_EthDst
152 dst := loxiSetField.Field.GetOXMValue().(net.HardwareAddr)
153 ethDst.EthDst = dst
154 ofpOxmOfbField.Value = &ethDst
Don Newtone0d34a82019-11-14 10:58:06 -0500155 }
156 ofpOxmField_OfbField.OfbField = &ofpOxmOfbField
157 ofpOxmField.Field = &ofpOxmField_OfbField
158 ofpActionSetField.Field = &ofpOxmField
159 ofpAction_SetField.SetField = &ofpActionSetField
160 ofpAction.Action = &ofpAction_SetField
Don Newtonb437c6f2019-12-18 11:51:57 -0500161
Don Newtone0d34a82019-11-14 10:58:06 -0500162 case ofp.OFPATPushPbb: //PushPbb
163 case ofp.OFPATPopPbb: //PopPbb
164 case ofp.OFPATExperimenter: //Experimenter
165
166 }
167 return &ofpAction
168
169}