@@ -215,6 +215,12 @@ static int nsim_dev_debugfs_init(struct nsim_dev *nsim_dev)
215215 & nsim_dev -> fail_reload );
216216 debugfs_create_file ("trap_flow_action_cookie" , 0600 , nsim_dev -> ddir ,
217217 nsim_dev , & nsim_dev_trap_fa_cookie_fops );
218+ debugfs_create_bool ("fail_trap_policer_set" , 0600 ,
219+ nsim_dev -> ddir ,
220+ & nsim_dev -> fail_trap_policer_set );
221+ debugfs_create_bool ("fail_trap_policer_counter_get" , 0600 ,
222+ nsim_dev -> ddir ,
223+ & nsim_dev -> fail_trap_policer_counter_get );
218224 return 0 ;
219225}
220226
@@ -392,6 +398,7 @@ struct nsim_trap_item {
392398struct nsim_trap_data {
393399 struct delayed_work trap_report_dw ;
394400 struct nsim_trap_item * trap_items_arr ;
401+ u64 * trap_policers_cnt_arr ;
395402 struct nsim_dev * nsim_dev ;
396403 spinlock_t trap_lock ; /* Protects trap_items_arr */
397404};
@@ -426,6 +433,24 @@ enum {
426433 DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id, \
427434 NSIM_TRAP_METADATA)
428435
436+ #define NSIM_DEV_TRAP_POLICER_MIN_RATE 1
437+ #define NSIM_DEV_TRAP_POLICER_MAX_RATE 8000
438+ #define NSIM_DEV_TRAP_POLICER_MIN_BURST 8
439+ #define NSIM_DEV_TRAP_POLICER_MAX_BURST 65536
440+
441+ #define NSIM_TRAP_POLICER (_id , _rate , _burst ) \
442+ DEVLINK_TRAP_POLICER(_id, _rate, _burst, \
443+ NSIM_DEV_TRAP_POLICER_MAX_RATE, \
444+ NSIM_DEV_TRAP_POLICER_MIN_RATE, \
445+ NSIM_DEV_TRAP_POLICER_MAX_BURST, \
446+ NSIM_DEV_TRAP_POLICER_MIN_BURST)
447+
448+ static const struct devlink_trap_policer nsim_trap_policers_arr [] = {
449+ NSIM_TRAP_POLICER (1 , 1000 , 128 ),
450+ NSIM_TRAP_POLICER (2 , 2000 , 256 ),
451+ NSIM_TRAP_POLICER (3 , 3000 , 512 ),
452+ };
453+
429454static const struct devlink_trap_group nsim_trap_groups_arr [] = {
430455 DEVLINK_TRAP_GROUP_GENERIC (L2_DROPS ),
431456 DEVLINK_TRAP_GROUP_GENERIC (L3_DROPS ),
@@ -568,6 +593,7 @@ static void nsim_dev_trap_report_work(struct work_struct *work)
568593
569594static int nsim_dev_traps_init (struct devlink * devlink )
570595{
596+ size_t policers_count = ARRAY_SIZE (nsim_trap_policers_arr );
571597 struct nsim_dev * nsim_dev = devlink_priv (devlink );
572598 struct nsim_trap_data * nsim_trap_data ;
573599 int err ;
@@ -584,6 +610,14 @@ static int nsim_dev_traps_init(struct devlink *devlink)
584610 goto err_trap_data_free ;
585611 }
586612
613+ nsim_trap_data -> trap_policers_cnt_arr = kcalloc (policers_count ,
614+ sizeof (u64 ),
615+ GFP_KERNEL );
616+ if (!nsim_trap_data -> trap_policers_cnt_arr ) {
617+ err = - ENOMEM ;
618+ goto err_trap_items_free ;
619+ }
620+
587621 /* The lock is used to protect the action state of the registered
588622 * traps. The value is written by user and read in delayed work when
589623 * iterating over all the traps.
@@ -592,10 +626,15 @@ static int nsim_dev_traps_init(struct devlink *devlink)
592626 nsim_trap_data -> nsim_dev = nsim_dev ;
593627 nsim_dev -> trap_data = nsim_trap_data ;
594628
629+ err = devlink_trap_policers_register (devlink , nsim_trap_policers_arr ,
630+ policers_count );
631+ if (err )
632+ goto err_trap_policers_cnt_free ;
633+
595634 err = devlink_trap_groups_register (devlink , nsim_trap_groups_arr ,
596635 ARRAY_SIZE (nsim_trap_groups_arr ));
597636 if (err )
598- goto err_trap_items_free ;
637+ goto err_trap_policers_unregister ;
599638
600639 err = devlink_traps_register (devlink , nsim_traps_arr ,
601640 ARRAY_SIZE (nsim_traps_arr ), NULL );
@@ -612,6 +651,11 @@ static int nsim_dev_traps_init(struct devlink *devlink)
612651err_trap_groups_unregister :
613652 devlink_trap_groups_unregister (devlink , nsim_trap_groups_arr ,
614653 ARRAY_SIZE (nsim_trap_groups_arr ));
654+ err_trap_policers_unregister :
655+ devlink_trap_policers_unregister (devlink , nsim_trap_policers_arr ,
656+ ARRAY_SIZE (nsim_trap_policers_arr ));
657+ err_trap_policers_cnt_free :
658+ kfree (nsim_trap_data -> trap_policers_cnt_arr );
615659err_trap_items_free :
616660 kfree (nsim_trap_data -> trap_items_arr );
617661err_trap_data_free :
@@ -628,6 +672,9 @@ static void nsim_dev_traps_exit(struct devlink *devlink)
628672 ARRAY_SIZE (nsim_traps_arr ));
629673 devlink_trap_groups_unregister (devlink , nsim_trap_groups_arr ,
630674 ARRAY_SIZE (nsim_trap_groups_arr ));
675+ devlink_trap_policers_unregister (devlink , nsim_trap_policers_arr ,
676+ ARRAY_SIZE (nsim_trap_policers_arr ));
677+ kfree (nsim_dev -> trap_data -> trap_policers_cnt_arr );
631678 kfree (nsim_dev -> trap_data -> trap_items_arr );
632679 kfree (nsim_dev -> trap_data );
633680}
@@ -766,13 +813,49 @@ nsim_dev_devlink_trap_action_set(struct devlink *devlink,
766813 return 0 ;
767814}
768815
816+ static int
817+ nsim_dev_devlink_trap_policer_set (struct devlink * devlink ,
818+ const struct devlink_trap_policer * policer ,
819+ u64 rate , u64 burst ,
820+ struct netlink_ext_ack * extack )
821+ {
822+ struct nsim_dev * nsim_dev = devlink_priv (devlink );
823+
824+ if (nsim_dev -> fail_trap_policer_set ) {
825+ NL_SET_ERR_MSG_MOD (extack , "User setup the operation to fail for testing purposes" );
826+ return - EINVAL ;
827+ }
828+
829+ return 0 ;
830+ }
831+
832+ static int
833+ nsim_dev_devlink_trap_policer_counter_get (struct devlink * devlink ,
834+ const struct devlink_trap_policer * policer ,
835+ u64 * p_drops )
836+ {
837+ struct nsim_dev * nsim_dev = devlink_priv (devlink );
838+ u64 * cnt ;
839+
840+ if (nsim_dev -> fail_trap_policer_counter_get )
841+ return - EINVAL ;
842+
843+ cnt = & nsim_dev -> trap_data -> trap_policers_cnt_arr [policer -> id - 1 ];
844+ * p_drops = * cnt ;
845+ * cnt += jiffies % 64 ;
846+
847+ return 0 ;
848+ }
849+
769850static const struct devlink_ops nsim_dev_devlink_ops = {
770851 .reload_down = nsim_dev_reload_down ,
771852 .reload_up = nsim_dev_reload_up ,
772853 .info_get = nsim_dev_info_get ,
773854 .flash_update = nsim_dev_flash_update ,
774855 .trap_init = nsim_dev_devlink_trap_init ,
775856 .trap_action_set = nsim_dev_devlink_trap_action_set ,
857+ .trap_policer_set = nsim_dev_devlink_trap_policer_set ,
858+ .trap_policer_counter_get = nsim_dev_devlink_trap_policer_counter_get ,
776859};
777860
778861#define NSIM_DEV_MAX_MACS_DEFAULT 32
0 commit comments