Skip to content

Commit

Permalink
Provide generic API for OVS register match/set value
Browse files Browse the repository at this point in the history
1. Use RegMark/RegField in OpenFlow Match or Load actions instead of passing the register ID and range.
2. The usage for OVS registers(including xxreg) should be predefined in regmarks.go.
3. Use reg0[0..3] to indicate the source of packet, and lease reg0[4..15] for other usages.

Signed-off-by: wenyingd <wenyingd@vmware.com>
  • Loading branch information
wenyingd committed Jul 23, 2021
1 parent 7469d7e commit 2d1eb2c
Show file tree
Hide file tree
Showing 14 changed files with 575 additions and 361 deletions.
31 changes: 17 additions & 14 deletions pkg/agent/controller/networkpolicy/packetin.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,24 +105,27 @@ func (c *Controller) HandlePacketIn(pktIn *ofctrl.PacketIn) error {

matches := pktIn.GetMatches()
// Get custom reasons in this packet-in.
match := getMatchRegField(matches, uint32(openflow.CustomReasonMarkReg))
customReasons, err := getInfoInReg(match, openflow.CustomReasonMarkRange.ToNXRange())
match := getMatchRegField(matches, openflow.CustomReasonField)
customReasons, err := getInfoInReg(match, openflow.CustomReasonField.GetRange().ToNXRange())
if err != nil {
return fmt.Errorf("received error while unloading customReason from reg: %v", err)
}

// Use reasons to choose operations.
if customReasons&openflow.CustomReasonLogging == openflow.CustomReasonLogging {
var checkCustomReason = func(customReasonMark *binding.RegMark) bool {
return customReasons&customReasonMark.GetValue() == customReasonMark.GetValue()
}
if checkCustomReason(openflow.MarkCustomReasonLogging) {
if err := c.logPacket(pktIn); err != nil {
return err
}
}
if customReasons&openflow.CustomReasonReject == openflow.CustomReasonReject {
if checkCustomReason(openflow.MarkCustomReasonReject) {
if err := c.rejectRequest(pktIn); err != nil {
return err
}
}
if customReasons&openflow.CustomReasonDeny == openflow.CustomReasonDeny {
if checkCustomReason(openflow.MarkCustomReasonDeny) {
if err := c.storeDenyConnection(pktIn); err != nil {
return err
}
Expand Down Expand Up @@ -153,26 +156,26 @@ func (c *Controller) logPacket(pktIn *ofctrl.PacketIn) error {
}

// getMatchRegField returns match to the regNum register.
func getMatchRegField(matchers *ofctrl.Matchers, regNum uint32) *ofctrl.MatchField {
return matchers.GetMatchByName(fmt.Sprintf("NXM_NX_REG%d", regNum))
func getMatchRegField(matchers *ofctrl.Matchers, field *binding.RegField) *ofctrl.MatchField {
return matchers.GetMatchByName(fmt.Sprintf(field.GetNXFieldName()))
}

// getMatch receives ofctrl matchers and table id, match field.
// Modifies match field to Ingress/Egress register based on tableID.
func getMatch(matchers *ofctrl.Matchers, tableID binding.TableIDType, disposition uint32) *ofctrl.MatchField {
// Get match from CNPDenyConjIDReg if disposition is not allow.
if disposition != openflow.DispositionAllow {
return getMatchRegField(matchers, uint32(openflow.CNPDenyConjIDReg))
return getMatchRegField(matchers, openflow.CNPDenyConjIDField)
}
// Get match from ingress/egress reg if disposition is allow
for _, table := range append(openflow.GetAntreaPolicyEgressTables(), openflow.EgressRuleTable) {
if tableID == table {
return getMatchRegField(matchers, uint32(openflow.EgressReg))
return getMatchRegField(matchers, openflow.TFEgressConjIDField)
}
}
for _, table := range append(openflow.GetAntreaPolicyIngressTables(), openflow.IngressRuleTable) {
if tableID == table {
return getMatchRegField(matchers, uint32(openflow.IngressReg))
return getMatchRegField(matchers, openflow.TFIngressConjIDField)
}
}
return nil
Expand All @@ -199,8 +202,8 @@ func getNetworkPolicyInfo(pktIn *ofctrl.PacketIn, c *Controller, ob *logInfo) er
ob.tableName = openflow.GetFlowTableName(tableID)

// Get disposition Allow or Drop
match = getMatchRegField(matchers, uint32(openflow.DispositionMarkReg))
info, err := getInfoInReg(match, openflow.APDispositionMarkRange.ToNXRange())
match = getMatchRegField(matchers, openflow.APDispositionField)
info, err := getInfoInReg(match, openflow.APDispositionField.GetRange().ToNXRange())
if err != nil {
return fmt.Errorf("received error while unloading disposition from reg: %v", err)
}
Expand Down Expand Up @@ -372,8 +375,8 @@ func (c *Controller) storeDenyConnection(pktIn *ofctrl.PacketIn) error {
// Get table ID
tableID := binding.TableIDType(pktIn.TableId)
// Get disposition Allow, Drop or Reject
match = getMatchRegField(matchers, uint32(openflow.DispositionMarkReg))
id, err := getInfoInReg(match, openflow.APDispositionMarkRange.ToNXRange())
match = getMatchRegField(matchers, openflow.APDispositionField)
id, err := getInfoInReg(match, openflow.APDispositionField.GetRange().ToNXRange())
if err != nil {
return fmt.Errorf("error when getting disposition from reg: %v", err)
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/agent/controller/traceflow/packetin.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func (c *Controller) parsePacketIn(pktIn *ofctrl.PacketIn) (*crdv1alpha1.Tracefl
obs = append(obs, *ob)
}
// Collect egress conjunctionID and get NetworkPolicy from cache.
if match := getMatchRegField(matchers, uint32(openflow.EgressReg)); match != nil {
if match := getMatchRegField(matchers, openflow.TFEgressConjIDField); match != nil {
egressInfo, err := getRegValue(match, nil)
if err != nil {
return nil, nil, nil, err
Expand All @@ -191,7 +191,7 @@ func (c *Controller) parsePacketIn(pktIn *ofctrl.PacketIn) (*crdv1alpha1.Tracefl
}

// Collect ingress conjunctionID and get NetworkPolicy from cache.
if match := getMatchRegField(matchers, uint32(openflow.IngressReg)); match != nil {
if match := getMatchRegField(matchers, openflow.TFIngressConjIDField); match != nil {
ingressInfo, err := getRegValue(match, nil)
if err != nil {
return nil, nil, nil, err
Expand All @@ -207,7 +207,7 @@ func (c *Controller) parsePacketIn(pktIn *ofctrl.PacketIn) (*crdv1alpha1.Tracefl
// Get drop table.
if tableID == uint8(openflow.EgressMetricTable) || tableID == uint8(openflow.IngressMetricTable) {
ob := getNetworkPolicyObservation(tableID, tableID == uint8(openflow.IngressMetricTable))
if match := getMatchRegField(matchers, uint32(openflow.CNPDenyConjIDReg)); match != nil {
if match := getMatchRegField(matchers, openflow.CNPDenyConjIDField); match != nil {
notAllowConjInfo, err := getRegValue(match, nil)
if err != nil {
return nil, nil, nil, err
Expand Down Expand Up @@ -239,7 +239,7 @@ func (c *Controller) parsePacketIn(pktIn *ofctrl.PacketIn) (*crdv1alpha1.Tracefl
}
}
var outputPort uint32
if match := getMatchRegField(matchers, uint32(openflow.PortCacheReg)); match != nil {
if match := getMatchRegField(matchers, openflow.TargetOFPortField); match != nil {
outputPort, err = getRegValue(match, nil)
if err != nil {
return nil, nil, nil, err
Expand Down Expand Up @@ -271,8 +271,8 @@ func (c *Controller) parsePacketIn(pktIn *ofctrl.PacketIn) (*crdv1alpha1.Tracefl
return tf, &nodeResult, capturedPacket, nil
}

func getMatchRegField(matchers *ofctrl.Matchers, regNum uint32) *ofctrl.MatchField {
return matchers.GetMatchByName(fmt.Sprintf("NXM_NX_REG%d", regNum))
func getMatchRegField(matchers *ofctrl.Matchers, field *binding.RegField) *ofctrl.MatchField {
return matchers.GetMatchByName(field.GetNXFieldName())
}

func getMatchTunnelDstField(matchers *ofctrl.Matchers, isIPv6 bool) *ofctrl.MatchField {
Expand Down
6 changes: 2 additions & 4 deletions pkg/agent/openflow/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1021,8 +1021,7 @@ func (c *client) SendTCPPacketOut(

// Reject response packet should bypass ConnTrack
if isReject {
name := fmt.Sprintf("%s%d", binding.NxmFieldReg, marksReg)
packetOutBuilder = packetOutBuilder.AddLoadAction(name, uint64(CustomReasonReject), CustomReasonMarkRange)
packetOutBuilder = packetOutBuilder.AddLoadRegMark(MarkCustomReasonReject)
}

packetOutObj := packetOutBuilder.Done()
Expand Down Expand Up @@ -1060,8 +1059,7 @@ func (c *client) SendICMPPacketOut(

// Reject response packet should bypass ConnTrack
if isReject {
name := fmt.Sprintf("%s%d", binding.NxmFieldReg, marksReg)
packetOutBuilder = packetOutBuilder.AddLoadAction(name, uint64(CustomReasonReject), CustomReasonMarkRange)
packetOutBuilder = packetOutBuilder.AddLoadRegMark(MarkCustomReasonReject)
}

packetOutObj := packetOutBuilder.Done()
Expand Down
Loading

0 comments on commit 2d1eb2c

Please sign in to comment.