Georgios Katsikas | 70671b3 | 2018-07-02 18:47:27 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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 | |
| 17 | package org.onosproject.drivers.server.devices.nic; |
| 18 | |
| 19 | import org.onosproject.net.flow.FlowRule; |
| 20 | |
| 21 | import org.onlab.packet.MacAddress; |
| 22 | |
| 23 | /** |
| 24 | * Implementation of a DPDK-based network interface card (NIC) flow rule. |
| 25 | */ |
| 26 | public class DefaultDpdkNicFlowRule extends DefaultNicFlowRule { |
| 27 | |
| 28 | public DefaultDpdkNicFlowRule( |
Georgios Katsikas | 740d328 | 2020-03-18 12:05:03 +0100 | [diff] [blame^] | 29 | FlowRule flowRule, |
| 30 | String trafficClassId, |
| 31 | String interfaceName, |
| 32 | long interfaceNumber, |
| 33 | long cpuCoreIndex, |
| 34 | NicRuleScope scope) { |
| 35 | super(flowRule, trafficClassId, interfaceName, |
| 36 | interfaceNumber, cpuCoreIndex, scope); |
Georgios Katsikas | 70671b3 | 2018-07-02 18:47:27 +0200 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | @Override |
| 40 | public String createRule() { |
Georgios Katsikas | 3e6d0f0 | 2018-07-25 17:04:04 +0200 | [diff] [blame] | 41 | return "flow create " + Long.toString(this.interfaceNumber) + " " + ruleBody(); |
Georgios Katsikas | 70671b3 | 2018-07-02 18:47:27 +0200 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public String removeRule() { |
Georgios Katsikas | 3e6d0f0 | 2018-07-25 17:04:04 +0200 | [diff] [blame] | 46 | return "flow destroy " + Long.toString(this.interfaceNumber) + " rule " + id().toString(); |
Georgios Katsikas | 70671b3 | 2018-07-02 18:47:27 +0200 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public String ruleBody() { |
| 51 | String rule = ""; |
| 52 | |
| 53 | rule += this.scope() + " pattern "; |
| 54 | |
| 55 | if (this.hasEthernet()) { |
| 56 | rule += "eth "; |
| 57 | |
| 58 | if (this.ethernetType() != null) { |
| 59 | rule += "type is " + Integer.toString(this.ethernetTypeValue()) + " "; |
| 60 | } |
| 61 | |
| 62 | if (this.ethernetSrcAddress() != null) { |
| 63 | rule += "src spec " + this.ethernetSrcAddress().toString() + " "; |
| 64 | rule += "src mask " + MacAddress.BROADCAST.toString() + " "; |
| 65 | } |
| 66 | |
| 67 | if (this.ethernetDstAddress() != null) { |
| 68 | rule += "dst spec " + this.ethernetDstAddress().toString() + " "; |
| 69 | rule += "dst mask " + MacAddress.BROADCAST.toString() + " "; |
| 70 | } |
| 71 | |
| 72 | rule += "/ "; |
| 73 | } |
| 74 | |
| 75 | if (this.hasIpv4()) { |
| 76 | rule += "ipv4 "; |
| 77 | |
| 78 | if (this.ipv4Protocol() > 0) { |
Georgios Katsikas | 43ec470 | 2018-10-12 08:46:48 +0200 | [diff] [blame] | 79 | rule += "proto is " + Integer.toString(this.ipv4Protocol()) + " "; |
Georgios Katsikas | 70671b3 | 2018-07-02 18:47:27 +0200 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | if (this.ipv4SrcAddress() != null) { |
Georgios Katsikas | da37cc8 | 2018-10-02 01:01:30 +0200 | [diff] [blame] | 83 | if ((this.ipv4SrcMask() != null) && (this.ipv4SrcMask().prefixLength() < 32)) { |
| 84 | rule += "src spec " + this.ipv4SrcAddress().toString() + " "; |
| 85 | rule += "src prefix " + this.ipv4SrcMask().prefixLength() + " "; |
| 86 | } else { |
| 87 | rule += "src is " + this.ipv4SrcAddress().toString() + " "; |
| 88 | } |
Georgios Katsikas | 70671b3 | 2018-07-02 18:47:27 +0200 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | if (this.ipv4DstAddress() != null) { |
Georgios Katsikas | da37cc8 | 2018-10-02 01:01:30 +0200 | [diff] [blame] | 92 | if ((this.ipv4DstMask() != null) && (this.ipv4DstMask().prefixLength() < 32)) { |
| 93 | rule += "dst spec " + this.ipv4DstAddress().toString() + " "; |
| 94 | rule += "dst prefix " + this.ipv4DstMask().prefixLength() + " "; |
| 95 | } else { |
| 96 | rule += "dst is " + this.ipv4DstAddress().toString() + " "; |
| 97 | } |
Georgios Katsikas | 70671b3 | 2018-07-02 18:47:27 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | rule += "/ "; |
| 101 | } |
| 102 | |
| 103 | if (this.hasTransport()) { |
| 104 | |
| 105 | if (this.hasUdp()) { |
| 106 | rule += "udp "; |
| 107 | } else if (this.hasTcp()) { |
| 108 | rule += "tcp "; |
| 109 | } |
| 110 | |
| 111 | if (this.sourcePort() > 0) { |
| 112 | rule += "src is " + Integer.toString(this.sourcePort()) + " "; |
| 113 | } |
| 114 | |
| 115 | if (this.destinationPort() > 0) { |
| 116 | rule += "dst is " + Integer.toString(this.destinationPort()) + " "; |
| 117 | } |
| 118 | |
| 119 | rule += "/ "; |
| 120 | } |
| 121 | |
| 122 | rule += "end "; |
| 123 | |
| 124 | if (this.actions() != null) { |
| 125 | rule += "actions "; |
| 126 | |
| 127 | for (NicRuleAction action : this.actions()) { |
| 128 | rule += action.actionType().toString() + " "; |
| 129 | |
| 130 | // No subsequent field |
| 131 | if (action.actionField().isEmpty()) { |
Georgios Katsikas | 43ec470 | 2018-10-12 08:46:48 +0200 | [diff] [blame] | 132 | rule += "/ "; |
Georgios Katsikas | 70671b3 | 2018-07-02 18:47:27 +0200 | [diff] [blame] | 133 | continue; |
| 134 | } |
| 135 | |
| 136 | // A subsequent field is associated with a value |
| 137 | rule += action.actionField() + " "; |
Georgios Katsikas | 43ec470 | 2018-10-12 08:46:48 +0200 | [diff] [blame] | 138 | rule += Long.toString(action.actionValue()) + " / "; |
Georgios Katsikas | 70671b3 | 2018-07-02 18:47:27 +0200 | [diff] [blame] | 139 | } |
| 140 | |
Georgios Katsikas | 43ec470 | 2018-10-12 08:46:48 +0200 | [diff] [blame] | 141 | rule += " end"; |
Georgios Katsikas | 70671b3 | 2018-07-02 18:47:27 +0200 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | return rule; |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public boolean equals(Object obj) { |
| 149 | return super.equals(obj); |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public int hashCode() { |
| 154 | return super.hashCode(); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public String toString() { |
| 159 | return super.toString(); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Returns a DPDK NIC rule builder. |
| 164 | * |
| 165 | * @return builder |
| 166 | */ |
| 167 | public static Builder nicRulebuilder() { |
| 168 | return new Builder(); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Default DPDK NIC rule builder. |
| 173 | */ |
| 174 | public static final class Builder extends DefaultNicFlowRule.Builder { |
| 175 | |
| 176 | public Builder() { |
| 177 | super(); |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public NicFlowRule build() { |
| 182 | return new DefaultDpdkNicFlowRule( |
| 183 | flowRule, trafficClassId, interfaceName, interfaceNumber, |
| 184 | cpuCoreIndex, scope); |
| 185 | } |
| 186 | |
| 187 | } |
| 188 | |
| 189 | } |