pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015-present Open Networking Foundation |
| 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 | package org.onosproject.segmentrouting.cli; |
| 17 | |
| 18 | import org.apache.karaf.shell.api.action.Argument; |
| 19 | import org.apache.karaf.shell.api.action.Command; |
pierventre | 4f68ffa | 2021-03-09 22:52:14 +0100 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.Option; |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 22 | |
| 23 | import org.onlab.packet.IPv4; |
| 24 | import org.onlab.packet.IpPrefix; |
| 25 | import org.onlab.packet.MacAddress; |
| 26 | import org.onlab.packet.TpPort; |
| 27 | import org.onlab.packet.VlanId; |
| 28 | import org.onosproject.cli.AbstractShellCommand; |
| 29 | import org.onosproject.cli.net.IpProtocol; |
| 30 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 31 | import org.onosproject.net.flow.TrafficSelector; |
| 32 | import org.onosproject.segmentrouting.policy.api.PolicyId; |
| 33 | import org.onosproject.segmentrouting.policy.api.PolicyService; |
| 34 | import org.onosproject.segmentrouting.policy.api.TrafficMatch; |
| 35 | import org.onosproject.segmentrouting.policy.api.TrafficMatchId; |
Wailok Shum | 37dd29a | 2021-04-27 18:13:55 +0800 | [diff] [blame] | 36 | import org.onosproject.segmentrouting.policy.api.TrafficMatchPriority; |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Command to add a traffic match. |
| 40 | */ |
| 41 | @Service |
pierventre | 4f68ffa | 2021-03-09 22:52:14 +0100 | [diff] [blame] | 42 | @Command(scope = "onos", name = "sr-tm-add", |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 43 | description = "Create a new traffic match") |
| 44 | public class TrafficMatchAddCommand extends AbstractShellCommand { |
| 45 | |
| 46 | @Argument(index = 0, name = "policyId", |
| 47 | description = "policy id", |
| 48 | required = true, multiValued = false) |
| 49 | String policyId; |
| 50 | |
Wailok Shum | 37dd29a | 2021-04-27 18:13:55 +0800 | [diff] [blame] | 51 | @Argument(index = 1, name = "priority", |
| 52 | description = "priority", |
| 53 | required = true, multiValued = false) |
| 54 | int priority; |
| 55 | |
pierventre | 4f68ffa | 2021-03-09 22:52:14 +0100 | [diff] [blame] | 56 | @Option(name = "-sip", aliases = "--srcIp", |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 57 | description = "src IP", |
pierventre | 4f68ffa | 2021-03-09 22:52:14 +0100 | [diff] [blame] | 58 | valueToShowInHelp = "10.0.0.1", |
| 59 | multiValued = false) |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 60 | String srcIp; |
| 61 | |
pierventre | 4f68ffa | 2021-03-09 22:52:14 +0100 | [diff] [blame] | 62 | @Option(name = "-sp", aliases = "--srcPort", |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 63 | description = "src port", |
pierventre | 4f68ffa | 2021-03-09 22:52:14 +0100 | [diff] [blame] | 64 | valueToShowInHelp = "1001", |
| 65 | multiValued = false) |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 66 | short srcPort; |
| 67 | |
pierventre | 4f68ffa | 2021-03-09 22:52:14 +0100 | [diff] [blame] | 68 | @Option(name = "-dip", aliases = "--dstIp", |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 69 | description = "dst IP", |
pierventre | 4f68ffa | 2021-03-09 22:52:14 +0100 | [diff] [blame] | 70 | valueToShowInHelp = "10.0.0.2", |
| 71 | multiValued = false) |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 72 | String dstIp; |
| 73 | |
pierventre | 4f68ffa | 2021-03-09 22:52:14 +0100 | [diff] [blame] | 74 | @Option(name = "-dp", aliases = "--dstPort", |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 75 | description = "dst port", |
pierventre | 4f68ffa | 2021-03-09 22:52:14 +0100 | [diff] [blame] | 76 | valueToShowInHelp = "1002", |
| 77 | multiValued = false) |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 78 | short dstPort; |
| 79 | |
pierventre | 4f68ffa | 2021-03-09 22:52:14 +0100 | [diff] [blame] | 80 | @Option(name = "-p", aliases = "--proto", |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 81 | description = "IP protocol", |
pierventre | 4f68ffa | 2021-03-09 22:52:14 +0100 | [diff] [blame] | 82 | valueToShowInHelp = "0x11", |
| 83 | multiValued = false) |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 84 | String proto; |
| 85 | |
pierventre | 4f68ffa | 2021-03-09 22:52:14 +0100 | [diff] [blame] | 86 | // TODO Consider to filter out the following fields for red policies |
| 87 | @Option(name = "-smac", aliases = "--srcMac", |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 88 | description = "src MAC", |
pierventre | 4f68ffa | 2021-03-09 22:52:14 +0100 | [diff] [blame] | 89 | valueToShowInHelp = "00:00:00:00:00:01", |
| 90 | multiValued = false) |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 91 | String srcMac; |
| 92 | |
pierventre | 4f68ffa | 2021-03-09 22:52:14 +0100 | [diff] [blame] | 93 | @Option(name = "-dmac", aliases = "--dstMac", |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 94 | description = "dst MAC", |
pierventre | 4f68ffa | 2021-03-09 22:52:14 +0100 | [diff] [blame] | 95 | valueToShowInHelp = "00:00:00:00:00:02", |
| 96 | multiValued = false) |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 97 | String dstMac; |
| 98 | |
pierventre | 4f68ffa | 2021-03-09 22:52:14 +0100 | [diff] [blame] | 99 | @Option(name = "-vid", aliases = "--VlanId", |
| 100 | description = "vlan ID", |
| 101 | valueToShowInHelp = "10", |
| 102 | multiValued = false) |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 103 | short vlanId = -1; |
| 104 | |
| 105 | @Override |
| 106 | protected void doExecute() { |
| 107 | TrafficSelector trafficSelector = parseArguments(); |
| 108 | if (trafficSelector.equals(DefaultTrafficSelector.emptySelector())) { |
| 109 | print("Empty traffic selector is not allowed"); |
| 110 | return; |
| 111 | } |
Wailok Shum | 37dd29a | 2021-04-27 18:13:55 +0800 | [diff] [blame] | 112 | TrafficMatchPriority trafficMatchPriority; |
| 113 | try { |
| 114 | trafficMatchPriority = new TrafficMatchPriority(priority); |
| 115 | } catch (IllegalArgumentException ex) { |
| 116 | print(ex.getMessage()); |
| 117 | return; |
| 118 | } |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 119 | |
| 120 | PolicyService policyService = AbstractShellCommand.get(PolicyService.class); |
| 121 | TrafficMatchId trafficMatchId = policyService.addOrUpdateTrafficMatch( |
Wailok Shum | 37dd29a | 2021-04-27 18:13:55 +0800 | [diff] [blame] | 122 | new TrafficMatch(trafficSelector, PolicyId.of(policyId), trafficMatchPriority)); |
pierventre | 30368ab | 2021-02-24 23:23:22 +0100 | [diff] [blame] | 123 | print("Traffic match %s has been submitted", trafficMatchId); |
| 124 | } |
| 125 | |
| 126 | private TrafficSelector parseArguments() { |
| 127 | TrafficSelector.Builder trafficSelectorBuilder = DefaultTrafficSelector.builder(); |
| 128 | if (srcIp != null) { |
| 129 | trafficSelectorBuilder.matchIPSrc(IpPrefix.valueOf(srcIp)); |
| 130 | } |
| 131 | if (dstIp != null) { |
| 132 | trafficSelectorBuilder.matchIPDst(IpPrefix.valueOf(dstIp)); |
| 133 | } |
| 134 | byte ipProtocol = 0; |
| 135 | if (proto != null) { |
| 136 | ipProtocol = (byte) (0xFF & IpProtocol.parseFromString(proto)); |
| 137 | trafficSelectorBuilder.matchIPProtocol(ipProtocol); |
| 138 | } |
| 139 | if (srcPort != 0) { |
| 140 | if (ipProtocol == IPv4.PROTOCOL_TCP) { |
| 141 | trafficSelectorBuilder.matchTcpSrc(TpPort.tpPort(srcPort)); |
| 142 | } else if (ipProtocol == IPv4.PROTOCOL_UDP) { |
| 143 | trafficSelectorBuilder.matchUdpSrc(TpPort.tpPort(srcPort)); |
| 144 | } |
| 145 | } |
| 146 | if (dstPort != 0) { |
| 147 | if (ipProtocol == IPv4.PROTOCOL_TCP) { |
| 148 | trafficSelectorBuilder.matchTcpDst(TpPort.tpPort(dstPort)); |
| 149 | } else if (ipProtocol == IPv4.PROTOCOL_UDP) { |
| 150 | trafficSelectorBuilder.matchUdpDst(TpPort.tpPort(dstPort)); |
| 151 | } |
| 152 | } |
| 153 | if (srcMac != null) { |
| 154 | trafficSelectorBuilder.matchEthSrc(MacAddress.valueOf(srcMac)); |
| 155 | } |
| 156 | if (dstMac != null) { |
| 157 | trafficSelectorBuilder.matchEthDst(MacAddress.valueOf(dstMac)); |
| 158 | } |
| 159 | if (vlanId != -1) { |
| 160 | trafficSelectorBuilder.matchVlanId(VlanId.vlanId(vlanId)); |
| 161 | } |
| 162 | return trafficSelectorBuilder.build(); |
| 163 | } |
| 164 | } |