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" |
Jonathan Hart | 828908c | 2020-04-15 14:23:45 -0700 | [diff] [blame] | 21 | ofp "github.com/opencord/goloxi/of13" |
Maninder | 12b909f | 2020-10-23 14:23:36 +0530 | [diff] [blame] | 22 | "github.com/opencord/voltha-protos/v4/go/openflow_13" |
ssiddiqui | df20bf7 | 2021-08-23 14:00:56 +0530 | [diff] [blame] | 23 | "net" |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 24 | "strings" |
| 25 | "sync" |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | var mu sync.Mutex |
| 29 | var xid uint32 = 1 |
| 30 | |
| 31 | func GetXid() uint32 { |
| 32 | mu.Lock() |
| 33 | defer mu.Unlock() |
| 34 | xid++ |
| 35 | return xid |
| 36 | } |
| 37 | func PadString(value string, padSize int) string { |
| 38 | size := len(value) |
| 39 | nullsNeeded := padSize - size |
| 40 | null := fmt.Sprintf("%c", '\000') |
| 41 | padded := strings.Repeat(null, nullsNeeded) |
| 42 | return fmt.Sprintf("%s%s", value, padded) |
| 43 | } |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 44 | |
| 45 | func extractAction(action ofp.IAction) *openflow_13.OfpAction { |
| 46 | var ofpAction openflow_13.OfpAction |
| 47 | switch action.GetType() { |
| 48 | case ofp.OFPATOutput: |
| 49 | var outputAction openflow_13.OfpAction_Output |
| 50 | loxiOutputAction := action.(*ofp.ActionOutput) |
| 51 | var output openflow_13.OfpActionOutput |
Don Newton | b437c6f | 2019-12-18 11:51:57 -0500 | [diff] [blame] | 52 | output.Port = uint32(loxiOutputAction.GetPort()) |
| 53 | /* |
| 54 | var maxLen uint16 |
| 55 | maxLen = loxiOutputAction.GetMaxLen() |
| 56 | output.MaxLen = uint32(maxLen) |
| 57 | |
| 58 | */ |
| 59 | output.MaxLen = 0 |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 60 | outputAction.Output = &output |
| 61 | ofpAction.Action = &outputAction |
| 62 | ofpAction.Type = openflow_13.OfpActionType_OFPAT_OUTPUT |
| 63 | case ofp.OFPATCopyTtlOut: //CopyTtltOut |
| 64 | case ofp.OFPATCopyTtlIn: //CopyTtlIn |
| 65 | case ofp.OFPATSetMplsTtl: //SetMplsTtl |
ssiddiqui | df20bf7 | 2021-08-23 14:00:56 +0530 | [diff] [blame] | 66 | mplsTtl := action.(*ofp.ActionSetMplsTtl) |
| 67 | setMplsTtl := openflow_13.OfpAction_MplsTtl{ |
| 68 | MplsTtl: &openflow_13.OfpActionMplsTtl{ |
| 69 | MplsTtl: uint32(mplsTtl.MplsTtl), |
| 70 | }, |
| 71 | } |
| 72 | ofpAction.Type = openflow_13.OfpActionType_OFPAT_SET_MPLS_TTL |
| 73 | ofpAction.Action = &setMplsTtl |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 74 | case ofp.OFPATDecMplsTtl: //DecMplsTtl |
| 75 | case ofp.OFPATPushVLAN: //PushVlan |
| 76 | var pushVlan openflow_13.OfpAction_Push |
| 77 | loxiPushAction := action.(*ofp.ActionPushVlan) |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 78 | var push openflow_13.OfpActionPush |
| 79 | push.Ethertype = uint32(loxiPushAction.Ethertype) //TODO This should be available in the fields |
| 80 | pushVlan.Push = &push |
| 81 | ofpAction.Type = openflow_13.OfpActionType_OFPAT_PUSH_VLAN |
| 82 | ofpAction.Action = &pushVlan |
| 83 | case ofp.OFPATPopVLAN: //PopVlan |
| 84 | ofpAction.Type = openflow_13.OfpActionType_OFPAT_POP_VLAN |
| 85 | case ofp.OFPATPushMpls: //PushMpls |
ssiddiqui | df20bf7 | 2021-08-23 14:00:56 +0530 | [diff] [blame] | 86 | var pushMpls openflow_13.OfpAction_Push |
| 87 | mplsPushAction := action.(*ofp.ActionPushMpls) |
| 88 | var push openflow_13.OfpActionPush |
| 89 | push.Ethertype = uint32(mplsPushAction.Ethertype) |
| 90 | pushMpls.Push = &push |
| 91 | ofpAction.Type = openflow_13.OfpActionType_OFPAT_PUSH_MPLS |
| 92 | ofpAction.Action = &pushMpls |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 93 | case ofp.OFPATPopMpls: //PopMpls |
ssiddiqui | df20bf7 | 2021-08-23 14:00:56 +0530 | [diff] [blame] | 94 | ofpAction.Type = openflow_13.OfpActionType_OFPAT_POP_MPLS |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 95 | case ofp.OFPATSetQueue: //SetQueue |
| 96 | case ofp.OFPATGroup: //ActionGroup |
Jonathan Hart | 60c5d77 | 2020-03-30 18:28:40 -0700 | [diff] [blame] | 97 | ofpAction.Type = openflow_13.OfpActionType_OFPAT_GROUP |
| 98 | group := action.(*ofp.ActionGroup) |
| 99 | ofpAction.Action = &openflow_13.OfpAction_Group{ |
| 100 | Group: &openflow_13.OfpActionGroup{ |
| 101 | GroupId: group.GroupId, |
| 102 | }, |
| 103 | } |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 104 | case ofp.OFPATSetNwTtl: //SetNwTtl |
| 105 | case ofp.OFPATDecNwTtl: //DecNwTtl |
| 106 | case ofp.OFPATSetField: //SetField |
| 107 | ofpAction.Type = openflow_13.OfpActionType_OFPAT_SET_FIELD |
| 108 | var ofpAction_SetField openflow_13.OfpAction_SetField |
| 109 | var ofpActionSetField openflow_13.OfpActionSetField |
| 110 | var ofpOxmField openflow_13.OfpOxmField |
| 111 | ofpOxmField.OxmClass = openflow_13.OfpOxmClass_OFPXMC_OPENFLOW_BASIC |
| 112 | var ofpOxmField_OfbField openflow_13.OfpOxmField_OfbField |
| 113 | var ofpOxmOfbField openflow_13.OfpOxmOfbField |
| 114 | loxiSetField := action.(*ofp.ActionSetField) |
| 115 | oxmName := loxiSetField.Field.GetOXMName() |
| 116 | switch oxmName { |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 117 | //TODO handle set field sith other fields |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 118 | case "vlan_vid": |
| 119 | ofpOxmOfbField.Type = openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_VLAN_VID |
| 120 | var vlanVid openflow_13.OfpOxmOfbField_VlanVid |
| 121 | var VlanVid = loxiSetField.Field.GetOXMValue().(uint16) |
| 122 | vlanVid.VlanVid = uint32(VlanVid) |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 123 | ofpOxmOfbField.Value = &vlanVid |
Gamze Abaka | 3e2b2ce | 2020-05-09 10:21:40 +0000 | [diff] [blame] | 124 | case "vlan_pcp": |
| 125 | ofpOxmOfbField.Type = openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_VLAN_PCP |
| 126 | var vlanPcp openflow_13.OfpOxmOfbField_VlanPcp |
| 127 | var VlanPcp = loxiSetField.Field.GetOXMValue().(uint8) |
| 128 | vlanPcp.VlanPcp = uint32(VlanPcp) |
| 129 | ofpOxmOfbField.Value = &vlanPcp |
ssiddiqui | df20bf7 | 2021-08-23 14:00:56 +0530 | [diff] [blame] | 130 | case "mpls_label": |
| 131 | ofpOxmOfbField.Type = openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_MPLS_LABEL |
| 132 | var mplsLabel openflow_13.OfpOxmOfbField_MplsLabel |
| 133 | label := loxiSetField.Field.GetOXMValue().(uint32) |
| 134 | mplsLabel.MplsLabel = label |
| 135 | ofpOxmOfbField.Value = &mplsLabel |
| 136 | case "mpls_bos": |
| 137 | ofpOxmOfbField.Type = openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_MPLS_BOS |
| 138 | var mplsBos openflow_13.OfpOxmOfbField_MplsBos |
| 139 | bos := loxiSetField.Field.GetOXMValue().(uint8) |
| 140 | mplsBos.MplsBos = uint32(bos) |
| 141 | ofpOxmOfbField.Value = &mplsBos |
| 142 | case "eth_src": |
| 143 | ofpOxmOfbField.Type = openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_ETH_SRC |
| 144 | var ethSrc openflow_13.OfpOxmOfbField_EthSrc |
| 145 | src := loxiSetField.Field.GetOXMValue().(net.HardwareAddr) |
| 146 | ethSrc.EthSrc = src |
| 147 | ofpOxmOfbField.Value = ðSrc |
| 148 | case "eth_dst": |
| 149 | ofpOxmOfbField.Type = openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_ETH_DST |
| 150 | var ethDst openflow_13.OfpOxmOfbField_EthDst |
| 151 | dst := loxiSetField.Field.GetOXMValue().(net.HardwareAddr) |
| 152 | ethDst.EthDst = dst |
| 153 | ofpOxmOfbField.Value = ðDst |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 154 | } |
| 155 | ofpOxmField_OfbField.OfbField = &ofpOxmOfbField |
| 156 | ofpOxmField.Field = &ofpOxmField_OfbField |
| 157 | ofpActionSetField.Field = &ofpOxmField |
| 158 | ofpAction_SetField.SetField = &ofpActionSetField |
| 159 | ofpAction.Action = &ofpAction_SetField |
Don Newton | b437c6f | 2019-12-18 11:51:57 -0500 | [diff] [blame] | 160 | |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 161 | case ofp.OFPATPushPbb: //PushPbb |
| 162 | case ofp.OFPATPopPbb: //PopPbb |
| 163 | case ofp.OFPATExperimenter: //Experimenter |
| 164 | |
| 165 | } |
| 166 | return &ofpAction |
| 167 | |
| 168 | } |