| 1 | --- |
| 2 | date: 2018-04-02 |
| 3 | footer: libibverbs |
| 4 | header: "Libibverbs Programmer's Manual" |
| 5 | layout: page |
| 6 | license: 'Licensed under the OpenIB.org BSD license (FreeBSD Variant) - See COPYING.md' |
| 7 | section: 3 |
| 8 | title: ibv_read_counters |
| 9 | --- |
| 10 | |
| 11 | # NAME |
| 12 | |
| 13 | **ibv_read_counters** - Read counter values |
| 14 | |
| 15 | # SYNOPSIS |
| 16 | |
| 17 | ```c |
| 18 | #include <infiniband/verbs.h> |
| 19 | |
| 20 | int ibv_read_counters(struct ibv_counters *counters, |
| 21 | uint64_t *counters_value, |
| 22 | uint32_t ncounters, |
| 23 | uint32_t flags); |
| 24 | ``` |
| 25 | |
| 26 | # DESCRIPTION |
| 27 | |
| 28 | **ibv_read_counters**() returns the values of the chosen counters into |
| 29 | *counters_value* array of which can accumulate *ncounters*. |
| 30 | The values are filled according to the configuration defined by the |
| 31 | user in the **ibv_attach_counters_point_xxx** functions. |
| 32 | |
| 33 | # ARGUMENTS |
| 34 | |
| 35 | *counters* |
| 36 | : Counters object to read. |
| 37 | |
| 38 | *counters_value* |
| 39 | : Input buffer to hold read result. |
| 40 | |
| 41 | *ncounters* |
| 42 | : Number of counters to fill. |
| 43 | |
| 44 | *flags* |
| 45 | : Use enum ibv_read_counters_flags. |
| 46 | |
| 47 | ## *flags* Argument |
| 48 | IBV_READ_COUNTERS_ATTR_PREFER_CACHED |
| 49 | : Will prefer reading the values from driver cache, else it will do volatile hardware access which is the default. |
| 50 | |
| 51 | # RETURN VALUE |
| 52 | |
| 53 | **ibv_read_counters**() returns 0 on success, or the value of errno on failure |
| 54 | (which indicates the failure reason) |
| 55 | |
| 56 | # EXAMPLE |
| 57 | |
| 58 | Example: Statically attach counters to a new flow |
| 59 | |
| 60 | This example demonstrates the use of counters which are attached statically with |
| 61 | the creation of a new flow. |
| 62 | The counters are read from hardware periodically, and finally all resources are released. |
| 63 | ```c |
| 64 | /* create counters object and define its counters points */ |
| 65 | /* create simple L2 flow with hardcoded MAC, and a count action */ |
| 66 | /* read counters periodically, every 1sec, until loop ends */ |
| 67 | /* assumes user prepared a RAW_PACKET QP as input */ |
| 68 | /* only limited error checking in run time for code simplicity */ |
| 69 | |
| 70 | #include <inttypes.h> |
| 71 | #include <infiniband/verbs.h> |
| 72 | |
| 73 | /* the below MAC should be replaced by user */ |
| 74 | #define FLOW_SPEC_ETH_MAC_VAL { |
| 75 | .dst_mac = { 0x00, 0x01, 0x02, 0x03, 0x04,0x05}, |
| 76 | .src_mac = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, |
| 77 | .ether_type = 0, .vlan_tag = 0, } |
| 78 | #define FLOW_SPEC_ETH_MAC_MASK { |
| 79 | .dst_mac = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, |
| 80 | .src_mac = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, |
| 81 | .ether_type = 0, .vlan_tag = 0, } |
| 82 | |
| 83 | void example_create_flow_with_counters_on_raw_qp(struct ibv_qp *qp) { |
| 84 | int idx = 0; |
| 85 | int loop = 10; |
| 86 | struct ibv_flow *flow = NULL; |
| 87 | struct ibv_counters *counters = NULL; |
| 88 | struct ibv_counters_init_attr init_attr = {0}; |
| 89 | struct ibv_counter_attach_attr attach_attr = {0}; |
| 90 | |
| 91 | /* create single counters handle */ |
| 92 | counters = ibv_create_counters(qp->context, &init_attr); |
| 93 | |
| 94 | /* define counters points */ |
| 95 | attach_attr.counter_desc = IBV_COUNTER_PACKETS; |
| 96 | attach_attr.index = idx++; |
| 97 | ret = ibv_attach_counters_point_flow(counters, &attach_attr, NULL); |
| 98 | if (ret == ENOTSUP) { |
| 99 | fprintf(stderr, "Attaching IBV_COUNTER_PACKETS to flow is not \ |
| 100 | supported"); |
| 101 | exit(1); |
| 102 | } |
| 103 | attach_attr.counter_desc = IBV_COUNTER_BYTES; |
| 104 | attach_attr.index = idx++; |
| 105 | ibv_attach_counters_point_flow(counters, &attach_attr, NULL); |
| 106 | if (ret == ENOTSUP) { |
| 107 | fprintf(stderr, "Attaching IBV_COUNTER_BYTES to flow is not \ |
| 108 | supported"); |
| 109 | exit(1); |
| 110 | } |
| 111 | |
| 112 | /* define a new flow attr that includes the counters handle */ |
| 113 | struct raw_eth_flow_attr { |
| 114 | struct ibv_flow_attr attr; |
| 115 | struct ibv_flow_spec_eth spec_eth; |
| 116 | struct ibv_flow_spec_counter_action spec_count; |
| 117 | } flow_attr = { |
| 118 | .attr = { |
| 119 | .comp_mask = 0, |
| 120 | .type = IBV_FLOW_ATTR_NORMAL, |
| 121 | .size = sizeof(flow_attr), |
| 122 | .priority = 0, |
| 123 | .num_of_specs = 2, /* ETH + COUNT */ |
| 124 | .port = 1, |
| 125 | .flags = 0, |
| 126 | }, |
| 127 | .spec_eth = { |
| 128 | .type = IBV_EXP_FLOW_SPEC_ETH, |
| 129 | .size = sizeof(struct ibv_flow_spec_eth), |
| 130 | .val = FLOW_SPEC_ETH_MAC_VAL, |
| 131 | .mask = FLOW_SPEC_ETH_MAC_MASK, |
| 132 | }, |
| 133 | .spec_count = { |
| 134 | .type = IBV_FLOW_SPEC_ACTION_COUNT, |
| 135 | .size = sizeof(struct ibv_flow_spec_counter_action), |
| 136 | .counters = counters, /* attached this counters handle |
| 137 | to the newly created ibv_flow */ } }; |
| 138 | |
| 139 | /* create the flow */ |
| 140 | flow = ibv_create_flow(qp, &flow_attr.attr); |
| 141 | |
| 142 | /* allocate array for counters value reading */ |
| 143 | uint64_t *counters_value = malloc(sizeof(uint64_t) * idx); |
| 144 | |
| 145 | /* periodical read and print of flow counters */ |
| 146 | while (--loop) { |
| 147 | sleep(1); |
| 148 | |
| 149 | /* read hardware counters values */ |
| 150 | ibv_read_counters(counters, counters_value, idx, |
| 151 | IBV_READ_COUNTERS_ATTR_PREFER_CACHED); |
| 152 | |
| 153 | printf("PACKETS = %"PRIu64", BYTES = %"PRIu64 \n", |
| 154 | counters_value[0], counters_value[1] ); |
| 155 | } |
| 156 | |
| 157 | /* all done, release all */ |
| 158 | free(counters_value); |
| 159 | |
| 160 | /* destroy flow and detach counters */ |
| 161 | ibv_destroy_flow(flow); |
| 162 | |
| 163 | /* destroy counters handle */ |
| 164 | ibv_destroy_counters(counters); |
| 165 | |
| 166 | return; |
| 167 | } |
| 168 | ``` |
| 169 | |
| 170 | # SEE ALSO |
| 171 | |
| 172 | **ibv_create_counters**, **ibv_destroy_counters**, |
| 173 | **ibv_attach_counters_point_flow**, **ibv_create_flow** |
| 174 | |
| 175 | # AUTHORS |
| 176 | |
| 177 | Raed Salem <[email protected]> |
| 178 | |
| 179 | Alex Rosenbaum <[email protected]> |