blob: 34d70ee0f7cf4312a3dacd74597af8aa13f1368d [file] [log] [blame]
Georgios Katsikas70671b32018-07-02 18:47:27 +02001/*
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
17package org.onosproject.drivers.server.devices.nic;
18
19import org.onosproject.net.flow.FlowRule;
20
21import org.onlab.packet.MacAddress;
22
23/**
24 * Implementation of a DPDK-based network interface card (NIC) flow rule.
25 */
26public class DefaultDpdkNicFlowRule extends DefaultNicFlowRule {
27
28 public DefaultDpdkNicFlowRule(
Georgios Katsikas740d3282020-03-18 12:05:03 +010029 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 Katsikas70671b32018-07-02 18:47:27 +020037 }
38
39 @Override
40 public String createRule() {
Georgios Katsikas3e6d0f02018-07-25 17:04:04 +020041 return "flow create " + Long.toString(this.interfaceNumber) + " " + ruleBody();
Georgios Katsikas70671b32018-07-02 18:47:27 +020042 }
43
44 @Override
45 public String removeRule() {
Georgios Katsikas3e6d0f02018-07-25 17:04:04 +020046 return "flow destroy " + Long.toString(this.interfaceNumber) + " rule " + id().toString();
Georgios Katsikas70671b32018-07-02 18:47:27 +020047 }
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 Katsikas43ec4702018-10-12 08:46:48 +020079 rule += "proto is " + Integer.toString(this.ipv4Protocol()) + " ";
Georgios Katsikas70671b32018-07-02 18:47:27 +020080 }
81
82 if (this.ipv4SrcAddress() != null) {
Georgios Katsikasda37cc82018-10-02 01:01:30 +020083 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 Katsikas70671b32018-07-02 18:47:27 +020089 }
90
91 if (this.ipv4DstAddress() != null) {
Georgios Katsikasda37cc82018-10-02 01:01:30 +020092 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 Katsikas70671b32018-07-02 18:47:27 +020098 }
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 Katsikas43ec4702018-10-12 08:46:48 +0200132 rule += "/ ";
Georgios Katsikas70671b32018-07-02 18:47:27 +0200133 continue;
134 }
135
136 // A subsequent field is associated with a value
137 rule += action.actionField() + " ";
Georgios Katsikas43ec4702018-10-12 08:46:48 +0200138 rule += Long.toString(action.actionValue()) + " / ";
Georgios Katsikas70671b32018-07-02 18:47:27 +0200139 }
140
Georgios Katsikas43ec4702018-10-12 08:46:48 +0200141 rule += " end";
Georgios Katsikas70671b32018-07-02 18:47:27 +0200142 }
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}