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