blob: 5831b79c3f3d1622132a6e469770858367dee537 [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(
29 FlowRule flowRule,
30 String trafficClassId,
31 String interfaceName,
32 long interfaceNumber,
33 long cpuCoreIndex,
34 NicRuleScope scope) {
35 super(flowRule, trafficClassId, interfaceName, interfaceNumber,
36 cpuCoreIndex, scope);
37 }
38
39 @Override
40 public String createRule() {
41 String rule = "flow create " + Long.toString(this.interfaceNumber) + " ";
42 return rule + ruleBody();
43 }
44
45 @Override
46 public String removeRule() {
47 String rule = "flow remove " + Long.toString(this.interfaceNumber) + " ";
48 return rule + ruleBody();
49 }
50
51 @Override
52 public String ruleBody() {
53 String rule = "";
54
55 rule += this.scope() + " pattern ";
56
57 if (this.hasEthernet()) {
58 rule += "eth ";
59
60 if (this.ethernetType() != null) {
61 rule += "type is " + Integer.toString(this.ethernetTypeValue()) + " ";
62 }
63
64 if (this.ethernetSrcAddress() != null) {
65 rule += "src spec " + this.ethernetSrcAddress().toString() + " ";
66 rule += "src mask " + MacAddress.BROADCAST.toString() + " ";
67 }
68
69 if (this.ethernetDstAddress() != null) {
70 rule += "dst spec " + this.ethernetDstAddress().toString() + " ";
71 rule += "dst mask " + MacAddress.BROADCAST.toString() + " ";
72 }
73
74 rule += "/ ";
75 }
76
77 if (this.hasIpv4()) {
78 rule += "ipv4 ";
79
80 if (this.ipv4Protocol() > 0) {
81 rule += "proto spec " + Integer.toString(this.ipv4Protocol()) + " ";
82 rule += "proto mask 0x0 ";
83 }
84
85 if (this.ipv4SrcAddress() != null) {
86 rule += "src is " + this.ipv4SrcAddress().toString() + " ";
87 }
88
89 if (this.ipv4SrcMask() != null) {
90 rule += "src mask " + this.ipv4SrcMask().toString() + " ";
91 }
92
93 if (this.ipv4DstAddress() != null) {
94 rule += "dst is " + this.ipv4DstAddress().toString() + " ";
95 }
96
97 if (this.ipv4DstMask() != null) {
98 rule += "dst mask " + this.ipv4DstMask().toString() + " ";
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}