+static inline int
+has_meter(void)
+{
+ int i;
+
+ for (i = 0; i < MAX_ACTIONS_NUM; i++) {
+ if (flow_actions[i] == 0)
+ break;
+ if (flow_actions[i]
+ & FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_METER))
+ return 1;
+ }
+ return 0;
+}
+
+static void
+create_meter_policy(void)
+{
+ struct rte_mtr_error error;
+ int ret, port_id;
+ struct rte_mtr_meter_policy_params policy;
+ uint16_t nr_ports;
+ struct rte_flow_action actions[RTE_COLORS][MAX_ACTIONS_NUM];
+ int i;
+
+ memset(actions, 0, sizeof(actions));
+ memset(&policy, 0, sizeof(policy));
+ nr_ports = rte_eth_dev_count_avail();
+ for (port_id = 0; port_id < nr_ports; port_id++) {
+ for (i = 0; i < RTE_COLORS; i++)
+ fill_actions(actions[i], all_actions[i], 0, 0, 0,
+ 0, 0, 0, unique_data, rx_queues_count,
+ dst_ports[port_id]);
+ policy.actions[RTE_COLOR_GREEN] = actions[RTE_COLOR_GREEN];
+ policy.actions[RTE_COLOR_YELLOW] = actions[RTE_COLOR_YELLOW];
+ policy.actions[RTE_COLOR_RED] = actions[RTE_COLOR_RED];
+ policy_id[port_id] = port_id + 10;
+ ret = rte_mtr_meter_policy_add(port_id, policy_id[port_id],
+ &policy, &error);
+ if (ret) {
+ fprintf(stderr, "port %d: failed to create meter policy\n",
+ port_id);
+ policy_id[port_id] = UINT32_MAX;
+ }
+ memset(actions, 0, sizeof(actions));
+ }
+}
+
+static void
+destroy_meter_policy(void)
+{
+ struct rte_mtr_error error;
+ uint16_t nr_ports;
+ int port_id;
+
+ nr_ports = rte_eth_dev_count_avail();
+ for (port_id = 0; port_id < nr_ports; port_id++) {
+ /* If port outside portmask */
+ if (!((ports_mask >> port_id) & 0x1))
+ continue;
+
+ if (rte_mtr_meter_policy_delete
+ (port_id, policy_id[port_id], &error)) {
+ fprintf(stderr, "port %u: failed to delete meter policy\n",
+ port_id);
+ rte_exit(EXIT_FAILURE, "Error: Failed to delete meter policy.\n");
+ }
+ }
+}
+
+static void
+create_meter_rule(int port_id, uint32_t counter)
+{
+ int ret;
+ struct rte_mtr_params params;
+ struct rte_mtr_error error;
+
+ memset(¶ms, 0, sizeof(struct rte_mtr_params));
+ params.meter_enable = 1;
+ params.stats_mask = 0xffff;
+ params.use_prev_mtr_color = 0;
+ params.dscp_table = NULL;
+
+ /*create meter*/
+ params.meter_profile_id = DEFAULT_METER_PROF_ID;
+
+ if (!policy_mtr) {
+ ret = rte_mtr_create(port_id, counter, ¶ms, 1, &error);
+ } else {
+ params.meter_policy_id = policy_id[port_id];
+ ret = rte_mtr_create(port_id, counter, ¶ms, 0, &error);
+ }
+
+ if (ret != 0) {
+ printf("Port %u create meter idx(%d) error(%d) message: %s\n",
+ port_id, counter, error.type,
+ error.message ? error.message : "(no stated reason)");
+ rte_exit(EXIT_FAILURE, "Error in creating meter\n");
+ }
+}
+
+static void
+destroy_meter_rule(int port_id, uint32_t counter)
+{
+ struct rte_mtr_error error;
+
+ if (policy_mtr && policy_id[port_id] != UINT32_MAX) {
+ if (rte_mtr_meter_policy_delete(port_id, policy_id[port_id],
+ &error))
+ fprintf(stderr, "Error: Failed to delete meter policy\n");
+ policy_id[port_id] = UINT32_MAX;
+ }
+ if (rte_mtr_destroy(port_id, counter, &error)) {
+ fprintf(stderr, "Port %d: Failed to delete meter.\n",
+ port_id);
+ rte_exit(EXIT_FAILURE, "Error in deleting meter rule");
+ }
+}
+
+static void
+meters_handler(int port_id, uint8_t core_id, uint8_t ops)
+{
+ uint64_t start_batch;
+ double cpu_time_used, insertion_rate;
+ int rules_count_per_core, rules_batch_idx;
+ uint32_t counter, start_counter = 0, end_counter;
+ double cpu_time_per_batch[MAX_BATCHES_COUNT] = { 0 };
+
+ rules_count_per_core = rules_count / mc_pool.cores_count;
+
+ if (core_id)
+ start_counter = core_id * rules_count_per_core;
+ end_counter = (core_id + 1) * rules_count_per_core;
+
+ cpu_time_used = 0;
+ start_batch = rte_get_timer_cycles();
+ for (counter = start_counter; counter < end_counter; counter++) {
+ if (ops == METER_CREATE)
+ create_meter_rule(port_id, counter);
+ else
+ destroy_meter_rule(port_id, counter);
+ /*
+ * Save the insertion rate for rules batch.
+ * Check if the insertion reached the rules
+ * patch counter, then save the insertion rate
+ * for this batch.
+ */
+ if (!((counter + 1) % rules_batch)) {
+ rules_batch_idx = ((counter + 1) / rules_batch) - 1;
+ cpu_time_per_batch[rules_batch_idx] =
+ ((double)(rte_get_timer_cycles() - start_batch))
+ / rte_get_timer_hz();
+ cpu_time_used += cpu_time_per_batch[rules_batch_idx];
+ start_batch = rte_get_timer_cycles();
+ }
+ }
+
+ /* Print insertion rates for all batches */
+ if (dump_iterations)
+ print_rules_batches(cpu_time_per_batch);
+
+ insertion_rate =
+ ((double) (rules_count_per_core / cpu_time_used) / 1000);
+
+ /* Insertion rate for all rules in one core */
+ printf(":: Port %d :: Core %d Meter %s :: start @[%d] - end @[%d],"
+ " use:%.02fs, rate:%.02fk Rule/Sec\n",
+ port_id, core_id, ops == METER_CREATE ? "create" : "delete",
+ start_counter, end_counter - 1,
+ cpu_time_used, insertion_rate);
+
+ if (ops == METER_CREATE)
+ mc_pool.meters_record.insertion[port_id][core_id]
+ = cpu_time_used;
+ else
+ mc_pool.meters_record.deletion[port_id][core_id]
+ = cpu_time_used;
+}
+
+static void
+destroy_meter_profile(void)
+{
+ struct rte_mtr_error error;
+ uint16_t nr_ports;
+ int port_id;
+
+ nr_ports = rte_eth_dev_count_avail();
+ for (port_id = 0; port_id < nr_ports; port_id++) {
+ /* If port outside portmask */
+ if (!((ports_mask >> port_id) & 0x1))
+ continue;
+
+ if (rte_mtr_meter_profile_delete
+ (port_id, DEFAULT_METER_PROF_ID, &error)) {
+ printf("Port %u del profile error(%d) message: %s\n",
+ port_id, error.type,
+ error.message ? error.message : "(no stated reason)");
+ rte_exit(EXIT_FAILURE, "Error: Destroy meter profile Failed!\n");
+ }
+ }
+}
+
+static void
+create_meter_profile(void)
+{
+ uint16_t nr_ports;
+ int ret, port_id;
+ struct rte_mtr_meter_profile mp;
+ struct rte_mtr_error error;
+
+ /*
+ *currently , only create one meter file for one port
+ *1 meter profile -> N meter rules -> N rte flows
+ */
+ memset(&mp, 0, sizeof(struct rte_mtr_meter_profile));
+ nr_ports = rte_eth_dev_count_avail();
+ for (port_id = 0; port_id < nr_ports; port_id++) {
+ /* If port outside portmask */
+ if (!((ports_mask >> port_id) & 0x1))
+ continue;
+ mp.alg = RTE_MTR_SRTCM_RFC2697;
+ mp.srtcm_rfc2697.cir = meter_profile_values[0] ?
+ meter_profile_values[0] : METER_CIR;
+ mp.srtcm_rfc2697.cbs = meter_profile_values[1] ?
+ meter_profile_values[1] : METER_CIR / 8;
+ mp.srtcm_rfc2697.ebs = meter_profile_values[2];
+ mp.packet_mode = packet_mode;
+ ret = rte_mtr_meter_profile_add
+ (port_id, DEFAULT_METER_PROF_ID, &mp, &error);
+ if (ret != 0) {
+ printf("Port %u create Profile error(%d) message: %s\n",
+ port_id, error.type,
+ error.message ? error.message : "(no stated reason)");
+ rte_exit(EXIT_FAILURE, "Error: Creation meter profile Failed!\n");
+ }
+ }
+}
+