github.com/zartbot/goflow@v0.0.0-20221001080520-456f7dfad288/service/ciscoavc/flowdirection.go (about) 1 package ciscoavc 2 3 import "github.com/zartbot/go_utils/map2value" 4 5 func Swap(d map[string]interface{}, a string, b string) { 6 d[a], d[b] = d[b], d[a] 7 } 8 9 func FixConnRecordDirection(d map[string]interface{}) error { 10 //Flow direction detect based on Observation Point view 11 // 12 // Ref: RFC:5103 Bidirectional Flow Export Using IP Flow Information Export (IPFIX) 13 // +-------+------------------+----------------------------------------+ 14 // | Value | Name | Description | 15 // +-------+------------------+----------------------------------------+ 16 // | 0x00 | arbitrary | Direction was assigned arbitrarily. | 17 // | 0x01 | initiator | The Biflow Source is the flow | 18 // | | | initiator, as determined by the | 19 // | | | Metering Process' best effort to | 20 // | | | detect the initiator. | 21 // | 0x02 | reverseInitiator | The Biflow Destination is the flow | 22 // | | | initiator, as determined by the | 23 // | | | Metering Process' best effort to | 24 // | | | detect the initiator. This value is | 25 // | | | provided for the convenience of | 26 // | | | Exporting Processes to revise an | 27 // | | | initiator estimate without re-encoding | 28 // | | | the Biflow Record. | 29 // | 0x03 | perimeter | The Biflow Source is the endpoint | 30 // | | | outside of a defined perimeter. The | 31 // | | | perimeter's definition is implicit in | 32 // | | | the set of Biflow Source and Biflow | 33 // | | | Destination addresses exported in the | 34 // | | | Biflow Records. | 35 // +-------+------------------+----------------------------------------+ 36 bidirectionT, bDirectionValid := d["biflowDirection"] 37 flowdirectionT, fDirectionValid := d["flowDirection"] 38 if bDirectionValid && fDirectionValid && bidirectionT != nil && flowdirectionT != nil { 39 //only process tcp packet with latency field 40 ProtocolID, _ := map2value.MapToUInt8(d, "protocolIdentifier") 41 //_, NetworkDelayExist := d["conn_delay_network_to_server_mean"] 42 dataType, _ := map2value.MapToString(d, "Type") 43 44 PktsRetransExist := false 45 BytesRetransExist := false 46 server_retrans_pkt_cnt, err_S_R_P_CNT := map2value.MapToUInt32(d, "conn_server_cnt_pkts_retransmit") 47 client_retrans_pkt_cnt, err_C_R_P_CNT := map2value.MapToUInt32(d, "conn_client_cnt_pkts_retransmit") 48 if (err_S_R_P_CNT == nil) && (err_C_R_P_CNT == nil) { 49 PktsRetransExist = true 50 } 51 52 server_retrans_bytes_cnt, err_S_R_B_CNT := map2value.MapToUInt32(d, "conn_server_cnt_bytes_retransmit") 53 client_retrans_bytes_cnt, err_C_R_B_CNT := map2value.MapToUInt32(d, "conn_client_cnt_bytes_retransmit") 54 if (err_S_R_B_CNT == nil) && (err_C_R_B_CNT == nil) { 55 BytesRetransExist = true 56 } 57 58 if flowdirectionT.(uint8) == 0 { 59 //FlowDirection is INPUT 60 61 /*Client(Initiator)-----OUT-----^observationPoint^|==Device==|---IN------Server(Responder) */ 62 if (ProtocolID == 6) && dataType == "art" { 63 d["in_network_delay_mean"] = d["conn_delay_network_to_server_mean"] 64 d["out_network_delay_mean"] = d["conn_delay_network_to_client_mean"] 65 } 66 67 if PktsRetransExist { 68 d["in2out_pkts_loss_sum"] = uint64(server_retrans_pkt_cnt) 69 d["out2in_pkts_loss_sum"] = uint64(client_retrans_pkt_cnt) 70 d["in2out_pkts_loss_rate"] = d["conn_server_pkts_retransmit_rate"] 71 d["out2in_pkts_loss_rate"] = d["conn_client_pkts_retransmit_rate"] 72 } 73 74 if BytesRetransExist { 75 d["in2out_bytes_loss_sum"] = uint64(server_retrans_bytes_cnt) 76 d["out2in_bytes_loss_sum"] = uint64(client_retrans_bytes_cnt) 77 d["in2out_bytes_loss_rate"] = d["conn_server_bytes_retransmit_rate"] 78 d["out2in_bytes_loss_rate"] = d["conn_client_bytes_retransmit_rate"] 79 } 80 81 d["in2out_bytes"] = d["conn_server_to_client_bytes"] 82 d["out2in_bytes"] = d["conn_client_to_server_bytes"] 83 d["in2out_pkts"] = d["responderPackets"] 84 d["out2in_pkts"] = d["initiatorPackets"] 85 } else { 86 //FlowDirection is OUTPUT 87 /*Server(Responder)-----OUT-----^observationPoint^|==Device==|---IN------Client(Initiator) */ 88 if (ProtocolID == 6) && dataType == "art" { 89 d["out_network_delay_mean"] = d["conn_delay_network_to_server_mean"] 90 d["in_network_delay_mean"] = d["conn_delay_network_to_client_mean"] 91 } 92 if PktsRetransExist { 93 d["out2in_pkts_loss_sum"] = uint64(server_retrans_pkt_cnt) 94 d["in2out_pkts_loss_sum"] = uint64(client_retrans_pkt_cnt) 95 d["out2in_pkts_loss_rate"] = d["conn_server_pkts_retransmit_rate"] 96 d["in2out_pkts_loss_rate"] = d["conn_client_pkts_retransmit_rate"] 97 } 98 if BytesRetransExist { 99 d["out2in_bytes_loss_sum"] = uint64(server_retrans_bytes_cnt) 100 d["in2out_bytes_loss_sum"] = uint64(client_retrans_bytes_cnt) 101 d["out2in_bytes_loss_rate"] = d["conn_server_bytes_retransmit_rate"] 102 d["in2out_bytes_loss_rate"] = d["conn_client_bytes_retransmit_rate"] 103 } 104 d["out2in_bytes"] = d["conn_server_to_client_bytes"] 105 d["in2out_bytes"] = d["conn_client_to_server_bytes"] 106 d["out2in_pkts"] = d["responderPackets"] 107 d["in2out_pkts"] = d["initiatorPackets"] 108 } 109 // this is a very tricky detection since Router currently only report 0,1,2 110 // all biflowDirection == 0 flow has src=0.0.0.0 , dst=0.0.0.0 ... 111 if bidirectionT.(uint8) == 2 { 112 if (ProtocolID == 6) && dataType == "art" { 113 Swap(d, "in_network_delay_mean", "out_network_delay_mean") 114 } 115 if PktsRetransExist { 116 Swap(d, "out2in_pkts_loss_sum", "in2out_pkts_loss_sum") 117 Swap(d, "out2in_pkts_loss_rate", "in2out_pkts_loss_rate") 118 } 119 if BytesRetransExist { 120 Swap(d, "out2in_bytes_loss_sum", "in2out_bytes_loss_sum") 121 Swap(d, "out2in_bytes_loss_rate", "in2out_bytes_loss_rate") 122 } 123 Swap(d, "out2in_bytes", "in2out_bytes") 124 Swap(d, "out2in_pkts", "in2out_pkts") 125 } 126 } 127 return nil 128 } 129 130 func FixRTPRecordDirection(d map[string]interface{}) error { 131 bidirectionT, bDirectionValid := d["biflowDirection"] 132 flowdirectionT, fDirectionValid := d["flowDirection"] 133 if bDirectionValid && fDirectionValid && bidirectionT != nil && flowdirectionT != nil { 134 if flowdirectionT.(uint8) == 0 { 135 //FlowDirection is INPUT 136 d["out2in_pkts_loss_rate"] = d["rtp_loss_rate"] 137 d["out2in_rtp_jitter_mean"] = d["rtp_jitter_mean"] 138 d["out2in_rtp_mos"] = d["rtp_mos"] 139 d["out2in_performance_score"] = d["performance_score"] 140 } else { 141 //FLowDirection is OUTPUT 142 d["in2out_pkts_loss_rate"] = d["rtp_loss_rate"] 143 d["in2out_rtp_jitter_mean"] = d["rtp_jitter_mean"] 144 d["in2out_rtp_mos"] = d["rtp_mos"] 145 d["in2out_performance_score"] = d["performance_score"] 146 } 147 } 148 return nil 149 }