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 | |
Georgios Katsikas | 3e6d0f0 | 2018-07-25 17:04:04 +0200 | [diff] [blame^] | 21 | import org.onlab.packet.Ip4Address; |
Georgios Katsikas | 70671b3 | 2018-07-02 18:47:27 +0200 | [diff] [blame] | 22 | import org.onlab.packet.MacAddress; |
| 23 | |
| 24 | /** |
| 25 | * Implementation of a DPDK-based network interface card (NIC) flow rule. |
| 26 | */ |
| 27 | public class DefaultDpdkNicFlowRule extends DefaultNicFlowRule { |
| 28 | |
| 29 | public DefaultDpdkNicFlowRule( |
| 30 | FlowRule flowRule, |
| 31 | String trafficClassId, |
| 32 | String interfaceName, |
| 33 | long interfaceNumber, |
| 34 | long cpuCoreIndex, |
| 35 | NicRuleScope scope) { |
Georgios Katsikas | 3e6d0f0 | 2018-07-25 17:04:04 +0200 | [diff] [blame^] | 36 | super(flowRule, trafficClassId, interfaceName, 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) { |
| 79 | rule += "proto spec " + Integer.toString(this.ipv4Protocol()) + " "; |
| 80 | rule += "proto mask 0x0 "; |
| 81 | } |
| 82 | |
| 83 | if (this.ipv4SrcAddress() != null) { |
| 84 | rule += "src is " + this.ipv4SrcAddress().toString() + " "; |
| 85 | } |
| 86 | |
| 87 | if (this.ipv4SrcMask() != null) { |
Georgios Katsikas | 3e6d0f0 | 2018-07-25 17:04:04 +0200 | [diff] [blame^] | 88 | rule += "src spec " + this.ipv4SrcMask().address().getIp4Address().toString() + " "; |
| 89 | rule += "src mask " + Ip4Address.makeMaskPrefix(this.ipv4SrcMask().prefixLength()).toString() + " "; |
Georgios Katsikas | 70671b3 | 2018-07-02 18:47:27 +0200 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | if (this.ipv4DstAddress() != null) { |
| 93 | rule += "dst is " + this.ipv4DstAddress().toString() + " "; |
| 94 | } |
| 95 | |
| 96 | if (this.ipv4DstMask() != null) { |
Georgios Katsikas | 3e6d0f0 | 2018-07-25 17:04:04 +0200 | [diff] [blame^] | 97 | rule += "dst spec " + this.ipv4DstMask().address().getIp4Address().toString() + " "; |
| 98 | rule += "dst mask " + Ip4Address.makeMaskPrefix(this.ipv4DstMask().prefixLength()).toString() + " "; |
Georgios Katsikas | 70671b3 | 2018-07-02 18:47:27 +0200 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | rule += "/ "; |
| 102 | } |
| 103 | |
| 104 | if (this.hasTransport()) { |
| 105 | |
| 106 | if (this.hasUdp()) { |
| 107 | rule += "udp "; |
| 108 | } else if (this.hasTcp()) { |
| 109 | rule += "tcp "; |
| 110 | } |
| 111 | |
| 112 | if (this.sourcePort() > 0) { |
| 113 | rule += "src is " + Integer.toString(this.sourcePort()) + " "; |
| 114 | } |
| 115 | |
| 116 | if (this.destinationPort() > 0) { |
| 117 | rule += "dst is " + Integer.toString(this.destinationPort()) + " "; |
| 118 | } |
| 119 | |
| 120 | rule += "/ "; |
| 121 | } |
| 122 | |
| 123 | rule += "end "; |
| 124 | |
| 125 | if (this.actions() != null) { |
| 126 | rule += "actions "; |
| 127 | |
| 128 | for (NicRuleAction action : this.actions()) { |
| 129 | rule += action.actionType().toString() + " "; |
| 130 | |
| 131 | // No subsequent field |
| 132 | if (action.actionField().isEmpty()) { |
| 133 | continue; |
| 134 | } |
| 135 | |
| 136 | // A subsequent field is associated with a value |
| 137 | rule += action.actionField() + " "; |
| 138 | rule += Long.toString(action.actionValue()) + " "; |
| 139 | } |
| 140 | |
| 141 | rule += "/ end"; |
| 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 | } |