Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 1 | /* |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 2 | Copyright 2020 the original author or authors. |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 3 | |
| 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 | |
| 17 | package openflow |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
ssiddiqui | df20bf7 | 2021-08-23 14:00:56 +0530 | [diff] [blame] | 21 | "net" |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 22 | "strings" |
| 23 | "sync" |
David K. Bainbridge | e05cf0c | 2021-08-19 03:16:50 +0000 | [diff] [blame] | 24 | |
| 25 | ofp "github.com/opencord/goloxi/of13" |
| 26 | "github.com/opencord/voltha-protos/v5/go/openflow_13" |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | var mu sync.Mutex |
| 30 | var xid uint32 = 1 |
| 31 | |
| 32 | func GetXid() uint32 { |
| 33 | mu.Lock() |
| 34 | defer mu.Unlock() |
| 35 | xid++ |
| 36 | return xid |
| 37 | } |
| 38 | func 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 Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 45 | |
| 46 | func 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 Newton | b437c6f | 2019-12-18 11:51:57 -0500 | [diff] [blame] | 53 | 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 Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 61 | 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 |
ssiddiqui | df20bf7 | 2021-08-23 14:00:56 +0530 | [diff] [blame] | 67 | 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 Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 75 | case ofp.OFPATDecMplsTtl: //DecMplsTtl |
| 76 | case ofp.OFPATPushVLAN: //PushVlan |
| 77 | var pushVlan openflow_13.OfpAction_Push |
| 78 | loxiPushAction := action.(*ofp.ActionPushVlan) |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 79 | 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 |
ssiddiqui | df20bf7 | 2021-08-23 14:00:56 +0530 | [diff] [blame] | 87 | 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 Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 94 | case ofp.OFPATPopMpls: //PopMpls |
ssiddiqui | df20bf7 | 2021-08-23 14:00:56 +0530 | [diff] [blame] | 95 | ofpAction.Type = openflow_13.OfpActionType_OFPAT_POP_MPLS |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 96 | case ofp.OFPATSetQueue: //SetQueue |
| 97 | case ofp.OFPATGroup: //ActionGroup |
Jonathan Hart | 60c5d77 | 2020-03-30 18:28:40 -0700 | [diff] [blame] | 98 | 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 Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 105 | 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 Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 118 | //TODO handle set field sith other fields |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 119 | 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 Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 124 | ofpOxmOfbField.Value = &vlanVid |
Gamze Abaka | 3e2b2ce | 2020-05-09 10:21:40 +0000 | [diff] [blame] | 125 | 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 |
ssiddiqui | df20bf7 | 2021-08-23 14:00:56 +0530 | [diff] [blame] | 131 | 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 = ðSrc |
| 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 = ðDst |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 155 | } |
| 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 Newton | b437c6f | 2019-12-18 11:51:57 -0500 | [diff] [blame] | 161 | |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 162 | case ofp.OFPATPushPbb: //PushPbb |
| 163 | case ofp.OFPATPopPbb: //PopPbb |
| 164 | case ofp.OFPATExperimenter: //Experimenter |
| 165 | |
| 166 | } |
| 167 | return &ofpAction |
| 168 | |
| 169 | } |