pierventre | e9261c9 | 2021-08-27 13:12:06 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2021-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 | package org.onosproject.segmentrouting.metadata; |
| 17 | |
| 18 | import org.onosproject.net.flow.criteria.Criterion; |
| 19 | import org.onosproject.net.flow.criteria.MetadataCriterion; |
| 20 | import org.onosproject.net.flow.instructions.Instructions; |
| 21 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 22 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 23 | import org.onosproject.net.flowobjective.Objective; |
| 24 | |
| 25 | /** |
| 26 | * Defines the SegmentRouting metadata extensions. |
| 27 | */ |
| 28 | public final class SRObjectiveMetadata { |
| 29 | // Helper class |
| 30 | private SRObjectiveMetadata() { |
| 31 | } |
| 32 | |
| 33 | ////////////////////////////////////////////////////////////////////////////// |
| 34 | // 64 .... 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 // |
| 35 | // X X X X X X X X X X X X X X X X X X X X X 1 1 1 1 1 // |
| 36 | ////////////////////////////////////////////////////////////////////////////// |
| 37 | // Metadata instruction is used as 8 byte sequence to carry up to 64 metadata |
| 38 | |
| 39 | // FIXME We are assuming SR as the only app programming this meta. |
| 40 | // SDFAB-530 to get rid of this limitation |
| 41 | |
| 42 | /** |
| 43 | * SR is setting this metadata when a double tagged filtering objective is removed |
| 44 | * and no other hosts is sharing the same input port. Thus, termination mac entries |
| 45 | * can be removed together with the vlan table entries. |
| 46 | * |
| 47 | * See org.onosproject.segmentrouting.RoutingRulePopulator#buildDoubleTaggedFilteringObj() |
| 48 | * See org.onosproject.segmentrouting.RoutingRulePopulator#processDoubleTaggedFilter() |
| 49 | */ |
| 50 | public static final long CLEANUP_DOUBLE_TAGGED_HOST_ENTRIES = 1L; |
| 51 | |
| 52 | /** |
| 53 | * SR is setting this metadata when an interface config update has been performed |
| 54 | * and thus termination mac entries should not be removed. |
| 55 | * |
| 56 | * See org.onosproject.segmentrouting.RoutingRulePopulator#processSinglePortFiltersInternal |
| 57 | */ |
| 58 | public static final long INTERFACE_CONFIG_UPDATE = 1L << 1; |
| 59 | |
| 60 | /** |
| 61 | * SR is setting this metadata to signal the driver when the config is for the pair port, |
| 62 | * i.e. ports connecting two leaves. |
| 63 | * |
| 64 | * See org.onosproject.segmentrouting.RoutingRulePopulator#portType |
| 65 | */ |
| 66 | public static final long PAIR_PORT = 1L << 2; |
| 67 | |
| 68 | /** |
| 69 | * SR is setting this metadata to signal the driver when the config is for an edge port, |
| 70 | * i.e. ports facing an host. |
| 71 | * |
| 72 | * See org.onosproject.segmentrouting.policy.impl.PolicyManager#trafficMatchFwdObjective |
| 73 | * See org.onosproject.segmentrouting.RoutingRulePopulator#portType |
| 74 | */ |
| 75 | public static final long EDGE_PORT = 1L << 3; |
| 76 | |
| 77 | /** |
| 78 | * SR is setting this metadata to signal the driver when the config is for an infra port, |
| 79 | * i.e. ports connecting a leaf with a spine. |
| 80 | */ |
| 81 | public static final long INFRA_PORT = 1L << 4; |
| 82 | |
| 83 | private static final long METADATA_MASK = 0x1FL; |
| 84 | |
| 85 | /** |
| 86 | * Check metadata passed from SegmentRouting app. |
| 87 | * |
| 88 | * @param obj the objective containing the metadata |
| 89 | * @return true if the objective contains valid metadata, false otherwise |
| 90 | */ |
| 91 | public static boolean isValidSrMetadata(Objective obj) { |
| 92 | long meta = 0; |
| 93 | if (obj instanceof FilteringObjective) { |
| 94 | FilteringObjective filtObj = (FilteringObjective) obj; |
| 95 | if (filtObj.meta() == null) { |
| 96 | return true; |
| 97 | } |
| 98 | Instructions.MetadataInstruction metaIns = filtObj.meta().writeMetadata(); |
| 99 | if (metaIns == null) { |
| 100 | return true; |
| 101 | } |
| 102 | meta = metaIns.metadata() & metaIns.metadataMask(); |
| 103 | } else if (obj instanceof ForwardingObjective) { |
| 104 | ForwardingObjective fwdObj = (ForwardingObjective) obj; |
| 105 | if (fwdObj.meta() == null) { |
| 106 | return true; |
| 107 | } |
| 108 | MetadataCriterion metaCrit = (MetadataCriterion) fwdObj.meta().getCriterion(Criterion.Type.METADATA); |
| 109 | if (metaCrit == null) { |
| 110 | return true; |
| 111 | } |
| 112 | meta = metaCrit.metadata(); |
| 113 | } |
| 114 | return meta != 0 && (meta ^ METADATA_MASK) <= METADATA_MASK; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Verify if a given flag has been set into the metadata. |
| 119 | * |
| 120 | * @param obj the objective containing the metadata |
| 121 | * @param flag the flag to verify |
| 122 | * @return true if the flag is set, false otherwise |
| 123 | */ |
| 124 | public static boolean isSrMetadataSet(Objective obj, long flag) { |
| 125 | long meta = 0; |
| 126 | if (obj instanceof FilteringObjective) { |
| 127 | FilteringObjective filtObj = (FilteringObjective) obj; |
| 128 | if (filtObj.meta() == null) { |
| 129 | return false; |
| 130 | } |
| 131 | Instructions.MetadataInstruction metaIns = filtObj.meta().writeMetadata(); |
| 132 | if (metaIns == null) { |
| 133 | return false; |
| 134 | } |
| 135 | meta = metaIns.metadata() & metaIns.metadataMask(); |
| 136 | } else if (obj instanceof ForwardingObjective) { |
| 137 | ForwardingObjective fwdObj = (ForwardingObjective) obj; |
| 138 | if (fwdObj.meta() == null) { |
| 139 | return false; |
| 140 | } |
| 141 | MetadataCriterion metaCrit = (MetadataCriterion) fwdObj.meta().getCriterion(Criterion.Type.METADATA); |
| 142 | if (metaCrit == null) { |
| 143 | return false; |
| 144 | } |
| 145 | meta = metaCrit.metadata(); |
| 146 | } |
| 147 | return (meta & flag) == flag; |
| 148 | } |
| 149 | } |