pipeline: add timestamp action
[dpdk.git] / lib / librte_pipeline / rte_table_action.c
index d22493a..1a32c71 100644 (file)
@@ -1030,6 +1030,146 @@ pkt_ipv6_work_nat(struct ipv6_hdr *ip,
        }
 }
 
+/**
+ * RTE_TABLE_ACTION_TTL
+ */
+static int
+ttl_cfg_check(struct rte_table_action_ttl_config *ttl)
+{
+       if (ttl->drop == 0)
+               return -ENOTSUP;
+
+       return 0;
+}
+
+struct ttl_data {
+       uint32_t n_packets;
+} __attribute__((__packed__));
+
+#define TTL_INIT(data, decrement)                         \
+       ((data)->n_packets = (decrement) ? 1 : 0)
+
+#define TTL_DEC_GET(data)                                  \
+       ((uint8_t)((data)->n_packets & 1))
+
+#define TTL_STATS_RESET(data)                             \
+       ((data)->n_packets = ((data)->n_packets & 1))
+
+#define TTL_STATS_READ(data)                               \
+       ((data)->n_packets >> 1)
+
+#define TTL_STATS_ADD(data, value)                        \
+       ((data)->n_packets =                                  \
+               (((((data)->n_packets >> 1) + (value)) << 1) |    \
+               ((data)->n_packets & 1)))
+
+static int
+ttl_apply(void *data,
+       struct rte_table_action_ttl_params *p)
+{
+       struct ttl_data *d = data;
+
+       TTL_INIT(d, p->decrement);
+
+       return 0;
+}
+
+static __rte_always_inline uint64_t
+pkt_ipv4_work_ttl(struct ipv4_hdr *ip,
+       struct ttl_data *data)
+{
+       uint32_t drop;
+       uint16_t cksum = ip->hdr_checksum;
+       uint8_t ttl = ip->time_to_live;
+       uint8_t ttl_diff = TTL_DEC_GET(data);
+
+       cksum += ttl_diff;
+       ttl -= ttl_diff;
+
+       ip->hdr_checksum = cksum;
+       ip->time_to_live = ttl;
+
+       drop = (ttl == 0) ? 1 : 0;
+       TTL_STATS_ADD(data, drop);
+
+       return drop;
+}
+
+static __rte_always_inline uint64_t
+pkt_ipv6_work_ttl(struct ipv6_hdr *ip,
+       struct ttl_data *data)
+{
+       uint32_t drop;
+       uint8_t ttl = ip->hop_limits;
+       uint8_t ttl_diff = TTL_DEC_GET(data);
+
+       ttl -= ttl_diff;
+
+       ip->hop_limits = ttl;
+
+       drop = (ttl == 0) ? 1 : 0;
+       TTL_STATS_ADD(data, drop);
+
+       return drop;
+}
+
+/**
+ * RTE_TABLE_ACTION_STATS
+ */
+static int
+stats_cfg_check(struct rte_table_action_stats_config *stats)
+{
+       if ((stats->n_packets_enabled == 0) && (stats->n_bytes_enabled == 0))
+               return -EINVAL;
+
+       return 0;
+}
+
+struct stats_data {
+       uint64_t n_packets;
+       uint64_t n_bytes;
+} __attribute__((__packed__));
+
+static int
+stats_apply(struct stats_data *data,
+       struct rte_table_action_stats_params *p)
+{
+       data->n_packets = p->n_packets;
+       data->n_bytes = p->n_bytes;
+
+       return 0;
+}
+
+static __rte_always_inline void
+pkt_work_stats(struct stats_data *data,
+       uint16_t total_length)
+{
+       data->n_packets++;
+       data->n_bytes += total_length;
+}
+
+/**
+ * RTE_TABLE_ACTION_TIME
+ */
+struct time_data {
+       uint64_t time;
+} __attribute__((__packed__));
+
+static int
+time_apply(struct time_data *data,
+       struct rte_table_action_time_params *p)
+{
+       data->time = p->time;
+       return 0;
+}
+
+static __rte_always_inline void
+pkt_work_time(struct time_data *data,
+       uint64_t time)
+{
+       data->time = time;
+}
+
 /**
  * Action profile
  */
@@ -1042,6 +1182,9 @@ action_valid(enum rte_table_action_type action)
        case RTE_TABLE_ACTION_TM:
        case RTE_TABLE_ACTION_ENCAP:
        case RTE_TABLE_ACTION_NAT:
+       case RTE_TABLE_ACTION_TTL:
+       case RTE_TABLE_ACTION_STATS:
+       case RTE_TABLE_ACTION_TIME:
                return 1;
        default:
                return 0;
@@ -1058,6 +1201,8 @@ struct ap_config {
        struct rte_table_action_tm_config tm;
        struct rte_table_action_encap_config encap;
        struct rte_table_action_nat_config nat;
+       struct rte_table_action_ttl_config ttl;
+       struct rte_table_action_stats_config stats;
 };
 
 static size_t
@@ -1072,6 +1217,10 @@ action_cfg_size(enum rte_table_action_type action)
                return sizeof(struct rte_table_action_encap_config);
        case RTE_TABLE_ACTION_NAT:
                return sizeof(struct rte_table_action_nat_config);
+       case RTE_TABLE_ACTION_TTL:
+               return sizeof(struct rte_table_action_ttl_config);
+       case RTE_TABLE_ACTION_STATS:
+               return sizeof(struct rte_table_action_stats_config);
        default:
                return 0;
        }
@@ -1094,6 +1243,12 @@ action_cfg_get(struct ap_config *ap_config,
        case RTE_TABLE_ACTION_NAT:
                return &ap_config->nat;
 
+       case RTE_TABLE_ACTION_TTL:
+               return &ap_config->ttl;
+
+       case RTE_TABLE_ACTION_STATS:
+               return &ap_config->stats;
+
        default:
                return NULL;
        }
@@ -1138,6 +1293,15 @@ action_data_size(enum rte_table_action_type action,
                return nat_data_size(&ap_config->nat,
                        &ap_config->common);
 
+       case RTE_TABLE_ACTION_TTL:
+               return sizeof(struct ttl_data);
+
+       case RTE_TABLE_ACTION_STATS:
+               return sizeof(struct stats_data);
+
+       case RTE_TABLE_ACTION_TIME:
+               return sizeof(struct time_data);
+
        default:
                return 0;
        }
@@ -1225,6 +1389,14 @@ rte_table_action_profile_action_register(struct rte_table_action_profile *profil
                status = nat_cfg_check(action_config);
                break;
 
+       case RTE_TABLE_ACTION_TTL:
+               status = ttl_cfg_check(action_config);
+               break;
+
+       case RTE_TABLE_ACTION_STATS:
+               status = stats_cfg_check(action_config);
+               break;
+
        default:
                status = 0;
                break;
@@ -1358,6 +1530,18 @@ rte_table_action_apply(struct rte_table_action *action,
                        action_params,
                        &action->cfg.common);
 
+       case RTE_TABLE_ACTION_TTL:
+               return ttl_apply(action_data,
+                       action_params);
+
+       case RTE_TABLE_ACTION_STATS:
+               return stats_apply(action_data,
+                       action_params);
+
+       case RTE_TABLE_ACTION_TIME:
+               return time_apply(action_data,
+                       action_params);
+
        default:
                return -EINVAL;
        }
@@ -1524,6 +1708,92 @@ rte_table_action_meter_read(struct rte_table_action *action,
        return 0;
 }
 
+int
+rte_table_action_ttl_read(struct rte_table_action *action,
+       void *data,
+       struct rte_table_action_ttl_counters *stats,
+       int clear)
+{
+       struct ttl_data *ttl_data;
+
+       /* Check input arguments */
+       if ((action == NULL) ||
+               ((action->cfg.action_mask &
+               (1LLU << RTE_TABLE_ACTION_TTL)) == 0) ||
+               (data == NULL))
+               return -EINVAL;
+
+       ttl_data = action_data_get(data, action, RTE_TABLE_ACTION_TTL);
+
+       /* Read */
+       if (stats)
+               stats->n_packets = TTL_STATS_READ(ttl_data);
+
+       /* Clear */
+       if (clear)
+               TTL_STATS_RESET(ttl_data);
+
+       return 0;
+}
+
+int
+rte_table_action_stats_read(struct rte_table_action *action,
+       void *data,
+       struct rte_table_action_stats_counters *stats,
+       int clear)
+{
+       struct stats_data *stats_data;
+
+       /* Check input arguments */
+       if ((action == NULL) ||
+               ((action->cfg.action_mask &
+               (1LLU << RTE_TABLE_ACTION_STATS)) == 0) ||
+               (data == NULL))
+               return -EINVAL;
+
+       stats_data = action_data_get(data, action,
+               RTE_TABLE_ACTION_STATS);
+
+       /* Read */
+       if (stats) {
+               stats->n_packets = stats_data->n_packets;
+               stats->n_bytes = stats_data->n_bytes;
+               stats->n_packets_valid = 1;
+               stats->n_bytes_valid = 1;
+       }
+
+       /* Clear */
+       if (clear) {
+               stats_data->n_packets = 0;
+               stats_data->n_bytes = 0;
+       }
+
+       return 0;
+}
+
+int
+rte_table_action_time_read(struct rte_table_action *action,
+       void *data,
+       uint64_t *timestamp)
+{
+       struct time_data *time_data;
+
+       /* Check input arguments */
+       if ((action == NULL) ||
+               ((action->cfg.action_mask &
+               (1LLU << RTE_TABLE_ACTION_TIME)) == 0) ||
+               (data == NULL) ||
+               (timestamp == NULL))
+               return -EINVAL;
+
+       time_data = action_data_get(data, action, RTE_TABLE_ACTION_TIME);
+
+       /* Read */
+       *timestamp = time_data->time;
+
+       return 0;
+}
+
 static __rte_always_inline uint64_t
 pkt_work(struct rte_mbuf *mbuf,
        struct rte_pipeline_table_entry *table_entry,
@@ -1597,6 +1867,30 @@ pkt_work(struct rte_mbuf *mbuf,
                        pkt_ipv6_work_nat(ip, data, &cfg->nat);
        }
 
+       if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_TTL)) {
+               void *data =
+                       action_data_get(table_entry, action, RTE_TABLE_ACTION_TTL);
+
+               if (cfg->common.ip_version)
+                       drop_mask |= pkt_ipv4_work_ttl(ip, data);
+               else
+                       drop_mask |= pkt_ipv6_work_ttl(ip, data);
+       }
+
+       if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_STATS)) {
+               void *data =
+                       action_data_get(table_entry, action, RTE_TABLE_ACTION_STATS);
+
+               pkt_work_stats(data, total_length);
+       }
+
+       if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_TIME)) {
+               void *data =
+                       action_data_get(table_entry, action, RTE_TABLE_ACTION_TIME);
+
+               pkt_work_time(data, time);
+       }
+
        return drop_mask;
 }
 
@@ -1803,6 +2097,61 @@ pkt4_work(struct rte_mbuf **mbufs,
                }
        }
 
+       if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_TTL)) {
+               void *data0 =
+                       action_data_get(table_entry0, action, RTE_TABLE_ACTION_TTL);
+               void *data1 =
+                       action_data_get(table_entry1, action, RTE_TABLE_ACTION_TTL);
+               void *data2 =
+                       action_data_get(table_entry2, action, RTE_TABLE_ACTION_TTL);
+               void *data3 =
+                       action_data_get(table_entry3, action, RTE_TABLE_ACTION_TTL);
+
+               if (cfg->common.ip_version) {
+                       drop_mask0 |= pkt_ipv4_work_ttl(ip0, data0);
+                       drop_mask1 |= pkt_ipv4_work_ttl(ip1, data1);
+                       drop_mask2 |= pkt_ipv4_work_ttl(ip2, data2);
+                       drop_mask3 |= pkt_ipv4_work_ttl(ip3, data3);
+               } else {
+                       drop_mask0 |= pkt_ipv6_work_ttl(ip0, data0);
+                       drop_mask1 |= pkt_ipv6_work_ttl(ip1, data1);
+                       drop_mask2 |= pkt_ipv6_work_ttl(ip2, data2);
+                       drop_mask3 |= pkt_ipv6_work_ttl(ip3, data3);
+               }
+       }
+
+       if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_STATS)) {
+               void *data0 =
+                       action_data_get(table_entry0, action, RTE_TABLE_ACTION_STATS);
+               void *data1 =
+                       action_data_get(table_entry1, action, RTE_TABLE_ACTION_STATS);
+               void *data2 =
+                       action_data_get(table_entry2, action, RTE_TABLE_ACTION_STATS);
+               void *data3 =
+                       action_data_get(table_entry3, action, RTE_TABLE_ACTION_STATS);
+
+               pkt_work_stats(data0, total_length0);
+               pkt_work_stats(data1, total_length1);
+               pkt_work_stats(data2, total_length2);
+               pkt_work_stats(data3, total_length3);
+       }
+
+       if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_TIME)) {
+               void *data0 =
+                       action_data_get(table_entry0, action, RTE_TABLE_ACTION_TIME);
+               void *data1 =
+                       action_data_get(table_entry1, action, RTE_TABLE_ACTION_TIME);
+               void *data2 =
+                       action_data_get(table_entry2, action, RTE_TABLE_ACTION_TIME);
+               void *data3 =
+                       action_data_get(table_entry3, action, RTE_TABLE_ACTION_TIME);
+
+               pkt_work_time(data0, time);
+               pkt_work_time(data1, time);
+               pkt_work_time(data2, time);
+               pkt_work_time(data3, time);
+       }
+
        return drop_mask0 |
                (drop_mask1 << 1) |
                (drop_mask2 << 2) |
@@ -1820,7 +2169,8 @@ ah(struct rte_pipeline *p,
        uint64_t pkts_drop_mask = 0;
        uint64_t time = 0;
 
-       if (cfg->action_mask & (1LLU << RTE_TABLE_ACTION_MTR))
+       if (cfg->action_mask & ((1LLU << RTE_TABLE_ACTION_MTR) |
+               (1LLU << RTE_TABLE_ACTION_TIME)))
                time = rte_rdtsc();
 
        if ((pkts_mask & (pkts_mask + 1)) == 0) {