|
| 1 | + |
| 2 | +param ( |
| 3 | + [Parameter(Mandatory=$false, HelpMessage="Rule for Pod")][string]$podIp = "10.224.0.53", |
| 4 | + [Parameter(Mandatory=$false, HelpMessage="Rule for Pod")][string]$ipFamily = "IPV4", |
| 5 | + [Parameter(Mandatory=$false, HelpMessage="Rule for Pod")][string]$protoFamily = "TCP", |
| 6 | + [Parameter(Mandatory=$false, HelpMessage="Rule for Pod")][bool]$PrintMatchedRules = $true |
| 7 | +) |
| 8 | + |
| 9 | + |
| 10 | +$podMac = (Get-HnsEndpoint | Where-Object IPAddress -Eq $podIp).MacAddress |
| 11 | +$podMacShortened = $podMac.Replace("-", "") |
| 12 | +$PodPortId = ((vfpctrl /list-vmswitch-port /format 1 | ConvertFrom-Json).Ports | Where-Object MacAddress -EQ $podMacShortened).Name |
| 13 | +$ExtPortId = ((vfpctrl /list-vmswitch-port /format 1 | ConvertFrom-Json).Ports | Where-Object Id -EQ "ExternalPort").Name |
| 14 | +$HostPortId = ((vfpctrl /list-vmswitch-port /format 1 | ConvertFrom-Json).Ports | Where-Object Id -Like "Container NIC*").Name |
| 15 | + |
| 16 | +$podLayers = ((vfpctrl /port $PodPortId /list-layer /format 1 | ConvertFrom-Json).Layers | Sort-Object -Property Priority).Name |
| 17 | +$extPortLayers = ((vfpctrl /port $ExtPortId /list-layer /format 1 | ConvertFrom-Json).Layers | Sort-Object -Property Priority).Name |
| 18 | +$hostPortLayers = ((vfpctrl /port $HostPortId /list-layer /format 1 | ConvertFrom-Json).Layers | Sort-Object -Property Priority).Name |
| 19 | + |
| 20 | +function RemoveNoise() { |
| 21 | + param ( |
| 22 | + [Parameter(Mandatory=$true)][System.Object]$Rule |
| 23 | + ) |
| 24 | + $Rule.PSObject.Properties.Remove('$type') |
| 25 | + $Rule.PSObject.Properties.Remove('Type') |
| 26 | + $Rule.PSObject.Properties.Remove('SubType') |
| 27 | + $Rule.PSObject.Properties.Remove('MssDelta') |
| 28 | + $Rule.PSObject.Properties.Remove('ReverseMssDelta') |
| 29 | + $Rule.PSObject.Properties.Remove('RuleFlags') |
| 30 | + $Rule.PSObject.Properties.Remove('PaRouteRuleFlags') |
| 31 | + $Rule.PSObject.Properties.Remove('CachePruningThreshold') |
| 32 | + $Rule.PSObject.Properties.Remove('InformationArray') |
| 33 | + $Rule.PSObject.Properties.Remove('NumHeaders') |
| 34 | + $Rule.PSObject.Properties.Remove('PartialRewriteTypes') |
| 35 | + return $Rule |
| 36 | +} |
| 37 | + |
| 38 | +function LogVfpCounter { |
| 39 | + param ( |
| 40 | + [parameter(Mandatory=$false)][string] $value = "", |
| 41 | + [parameter(Mandatory=$false)][bool] $error = $false |
| 42 | + ) |
| 43 | + # Add-Content -Path $VfpRuleFile -Value $value |
| 44 | + if ($error -eq $true) { |
| 45 | + Write-Host $value -ForegroundColor Red |
| 46 | + } |
| 47 | + else { |
| 48 | + Write-Host $value |
| 49 | + } |
| 50 | + |
| 51 | +} |
| 52 | + |
| 53 | +function NewLine { |
| 54 | + param ( |
| 55 | + [parameter(Mandatory=$false)][int] $NoOfLines = 1 |
| 56 | + ) |
| 57 | + for ($i = 1; $i -le $NoOfLines; $i++) { |
| 58 | + LogVfpCounter "" |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +function PrintRules { |
| 63 | + param ( |
| 64 | + [Parameter(Mandatory=$true)][string[]]$Layers, |
| 65 | + [Parameter(Mandatory=$true)][string]$PortId, |
| 66 | + [Parameter(Mandatory=$false)][string]$Dir="OUT" |
| 67 | + ) |
| 68 | + |
| 69 | + $ruleCounterMap = @{} |
| 70 | + |
| 71 | + foreach($layer in $Layers) { |
| 72 | + $groups = ((vfpctrl /port $PortId /layer $layer /list-group /format 1 | ConvertFrom-Json).Groups | Sort-Object -Property Priority).Name |
| 73 | + foreach($group in $groups) { |
| 74 | + if($group.Contains("_$DIR") -ne $true) { |
| 75 | + continue |
| 76 | + } |
| 77 | + if(($group.Contains("IPV4") -eq $true) -or ($group.Contains("IPV6") -eq $true)) { |
| 78 | + if($group.Contains($ipFamily) -ne $true) { |
| 79 | + continue |
| 80 | + } |
| 81 | + } |
| 82 | + if(($group.Contains("TCP") -eq $true) -or ($group.Contains("UDP") -eq $true) -or ($group.Contains("ICMP") -eq $true)) { |
| 83 | + if($group.Contains($protoFamily) -ne $true) { |
| 84 | + continue |
| 85 | + } |
| 86 | + } |
| 87 | + $rules = (vfpctrl /port $PortId /layer $layer /group $group /get-rule-counter /format 1 | ConvertFrom-Json | Sort-Object -Property Priority).Rules |
| 88 | + foreach ($rule in $rules) { |
| 89 | + |
| 90 | + $ruleId = $rule.Name |
| 91 | + if (($rule.Id).Length -gt 0) { |
| 92 | + $ruleId = $rule.Id |
| 93 | + } |
| 94 | + |
| 95 | + $ruleKey = "$portId-$layer-$group-$ruleId" |
| 96 | + |
| 97 | + $informationArray = $rule.InformationArray |
| 98 | + |
| 99 | + $rule = RemoveNoise -Rule $rule |
| 100 | + |
| 101 | + if ($informationArray.Count -gt 0) { |
| 102 | + $rule | Add-Member -MemberType NoteProperty -Name RuleCounters -Value $informationArray[0].RuleCounters |
| 103 | + } |
| 104 | + |
| 105 | + $ruleJson = $rule | ConvertTo-Json -Depth 10 |
| 106 | + |
| 107 | + if ($informationArray.Count -gt 0) { |
| 108 | + |
| 109 | + $ruleCounters = $informationArray[0].RuleCounters |
| 110 | + $matchedPackets = $ruleCounters.MatchedPackets |
| 111 | + $droppedPackets = $ruleCounters.DroppedPackets |
| 112 | + $pendingPackets = $ruleCounters.PendingPackets |
| 113 | + $droppedFlows = $ruleCounters.DroppedFlows |
| 114 | + |
| 115 | + if (($droppedPackets -gt 0) -or ($pendingPackets -gt 0) -or ($droppedFlows -gt 0)) { |
| 116 | + LogVfpCounter " Dropped Rule : " -error $true |
| 117 | + LogVfpCounter " ================ " -error $true |
| 118 | + NewLine 1 |
| 119 | + LogVfpCounter " Layer : $layer , Group : $group , Id : $ruleId " -error $true |
| 120 | + NewLine 1 |
| 121 | + LogVfpCounter " $ruleJson " -error $true |
| 122 | + NewLine 2 |
| 123 | + } |
| 124 | + elseif (($PrintMatchedRules -eq $true) -and ($matchedPackets -gt 0)) { |
| 125 | + LogVfpCounter " Matched Rule : " |
| 126 | + LogVfpCounter " ================ " |
| 127 | + NewLine 1 |
| 128 | + LogVfpCounter " Layer : $layer , Group : $group , Id : $ruleId " |
| 129 | + NewLine 1 |
| 130 | + LogVfpCounter " $ruleJson " |
| 131 | + |
| 132 | + NewLine 2 |
| 133 | + } |
| 134 | + |
| 135 | + $ruleCounterMap[$ruleKey] = $rule |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + return $ruleCounterMap |
| 142 | +} |
| 143 | + |
| 144 | +Write-Host "#===================== Pod VFP Port Rules in Outbound Direction ================#" |
| 145 | +NewLine 2 |
| 146 | +$podPortRulesOutbound = PrintRules -Layers $podLayers -portId $PodPortId -Dir "OUT" |
| 147 | +NewLine 2 |
| 148 | +Write-Host "#===================== External VFP Port Rules in Outbound Direction ================#" |
| 149 | +NewLine 2 |
| 150 | +$extPortRulesOutbound = PrintRules -Layers $extPortLayers -portId $ExtPortId -Dir "OUT" |
| 151 | +NewLine 2 |
| 152 | +Write-Host "#===================== External VFP Port Rules in Inbound Direction ================#" |
| 153 | +NewLine 2 |
| 154 | +$extPortRulesIntbound = PrintRules -Layers $extPortLayers -portId $ExtPortId -Dir "IN" |
| 155 | +NewLine 2 |
| 156 | +Write-Host "#===================== Pod VFP Port Rules in Inbound Direction ================#" |
| 157 | +NewLine 2 |
| 158 | +$podPortRulesIutbound = PrintRules -Layers $podLayers -portId $PodPortId -Dir "IN" |
0 commit comments