net/i40e: fix VF overwrite PF RSS LUT for X722
[dpdk.git] / usertools / dpdk-telemetry-client.py
1 # SPDK-License-Identifier: BSD-3-Clause
2 # Copyright(c) 2018 Intel Corporation
3
4 import socket
5 import os
6 import sys
7 import time
8
9 BUFFER_SIZE = 200000
10
11 METRICS_REQ = "{\"action\":0,\"command\":\"ports_all_stat_values\",\"data\":null}"
12 API_REG = "{\"action\":1,\"command\":\"clients\",\"data\":{\"client_path\":\""
13 API_UNREG = "{\"action\":2,\"command\":\"clients\",\"data\":{\"client_path\":\""
14 DEFAULT_FP = "/var/run/dpdk/default_client"
15
16 class Socket:
17
18     def __init__(self):
19         self.send_fd = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET)
20         self.recv_fd = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET)
21         self.client_fd = None
22
23     def __del__(self):
24         try:
25             self.send_fd.close()
26             self.recv_fd.close()
27             self.client_fd.close()
28         except:
29             print("Error - Sockets could not be closed")
30
31 class Client:
32
33     def __init__(self): # Creates a client instance
34         self.socket = Socket()
35         self.file_path = None
36         self.choice = None
37         self.unregistered = 0
38
39     def __del__(self):
40         try:
41             if self.unregistered == 0:
42                 self.unregister();
43         except:
44             print("Error - Client could not be destroyed")
45
46     def getFilepath(self, file_path): # Gets arguments from Command-Line and assigns to instance of client
47         self.file_path = file_path
48
49     def register(self): # Connects a client to DPDK-instance
50         if os.path.exists(self.file_path):
51             os.unlink(self.file_path)
52         try:
53             self.socket.recv_fd.bind(self.file_path)
54         except socket.error as msg:
55             print ("Error - Socket binding error: " + str(msg) + "\n")
56         self.socket.recv_fd.settimeout(2)
57         self.socket.send_fd.connect("/var/run/dpdk/rte/telemetry")
58         JSON = (API_REG + self.file_path + "\"}}")
59         self.socket.send_fd.sendall(JSON)
60         self.socket.recv_fd.listen(1)
61         self.socket.client_fd = self.socket.recv_fd.accept()[0]
62
63     def unregister(self): # Unregister a given client
64         self.socket.client_fd.send(API_UNREG + self.file_path + "\"}}")
65         self.socket.client_fd.close()
66
67     def requestMetrics(self): # Requests metrics for given client
68         self.socket.client_fd.send(METRICS_REQ)
69         data = self.socket.client_fd.recv(BUFFER_SIZE)
70         print "\nResponse: \n", str(data)
71
72     def repeatedlyRequestMetrics(self, sleep_time): # Recursively requests metrics for given client
73         print("\nPlease enter the number of times you'd like to continuously request Metrics:")
74         n_requests = int(input("\n:"))
75         print("\033[F") #Removes the user input from screen, cleans it up
76         print("\033[K")
77         for i in range(n_requests):
78             self.requestMetrics()
79             time.sleep(sleep_time)
80
81     def interactiveMenu(self, sleep_time): # Creates Interactive menu within the script
82         while self.choice != 3:
83             print("\nOptions Menu")
84             print("[1] Send for Metrics for all ports")
85             print("[2] Send for Metrics for all ports recursively")
86             print("[3] Unregister client")
87
88             try:
89                 self.choice = int(input("\n:"))
90                 print("\033[F") #Removes the user input for screen, cleans it up
91                 print("\033[K")
92                 if self.choice == 1:
93                     self.requestMetrics()
94                 elif self.choice == 2:
95                     self.repeatedlyRequestMetrics(sleep_time)
96                 elif self.choice == 3:
97                     self.unregister()
98                     self.unregistered = 1
99                 else:
100                     print("Error - Invalid request choice")
101             except:
102                 pass
103
104 if __name__ == "__main__":
105
106     sleep_time = 1
107     file_path = ""
108     if (len(sys.argv) == 2):
109         file_path = sys.argv[1]
110     else:
111         print("Warning - No filepath passed, using default (" + DEFAULT_FP + ").")
112         file_path = DEFAULT_FP
113     client = Client()
114     client.getFilepath(file_path)
115     client.register()
116     client.interactiveMenu(sleep_time)