@@ -60,9 +60,15 @@ static int include_cb(struct nl_object *obj, struct nl_parser_param *p)
6060
6161 if (ops -> co_include_event )
6262 return ops -> co_include_event (ca -> ca_cache , obj , ca -> ca_change ,
63+ ca -> ca_change_v2 ,
6364 ca -> ca_change_data );
64- else
65- return nl_cache_include (ca -> ca_cache , obj , ca -> ca_change , ca -> ca_change_data );
65+ else {
66+ if (ca -> ca_change_v2 )
67+ return nl_cache_include_v2 (ca -> ca_cache , obj , ca -> ca_change_v2 , ca -> ca_change_data );
68+ else
69+ return nl_cache_include (ca -> ca_cache , obj , ca -> ca_change , ca -> ca_change_data );
70+ }
71+
6672}
6773
6874static int event_input (struct nl_msg * msg , void * arg )
@@ -194,6 +200,70 @@ int nl_cache_mngr_alloc(struct nl_sock *sk, int protocol, int flags,
194200 return err ;
195201}
196202
203+ /**
204+ * Set change_func_v2 for cache manager
205+ * @arg mngr Cache manager.
206+ * @arg cache Cache associated with the callback
207+ * @arg cb Function to be called upon changes.
208+ * @arg data Argument passed on to change callback
209+ *
210+ * Adds callback change_func_v2 to a registered cache. This callback provides
211+ * in like the standard change_func the added or remove netlink object. In case
212+ * of a change the old and the new object is provided as well as the according
213+ * diff. If this callback is registered this has a higher priority then the
214+ * change_func registered during cache registration. Hence only one callback is
215+ * executed.
216+ *
217+ * The first netlink object in the callback is refering to the old object and
218+ * the second to the new. This means on NL_ACT_CHANGE the first is the previous
219+ * object in the cache and the second the updated version. On NL_ACT_DEL the
220+ * first is the deleted object the second is NULL. On NL_ACT_NEW the first is
221+ * NULL and the second the new netlink object.
222+ *
223+ * The user is responsible for calling nl_cache_mngr_poll() or monitor
224+ * the socket and call nl_cache_mngr_data_ready() to allow the library
225+ * to process netlink notification events.
226+ *
227+ * @see nl_cache_mngr_poll()
228+ * @see nl_cache_mngr_data_ready()
229+ *
230+ * @return 0 on success or a negative error code.
231+ * @return -NLE_PROTO_MISMATCH Protocol mismatch between cache manager and
232+ * cache type
233+ * @return -NLE_OPNOTSUPP Cache type does not support updates
234+ * @return -NLE_RANGE Cache of this type is not registered
235+ */
236+ static int nl_cache_mngr_set_change_func_v2 (struct nl_cache_mngr * mngr ,
237+ struct nl_cache * cache ,
238+ change_func_v2_t cb , void * data )
239+ {
240+ struct nl_cache_ops * ops ;
241+ int i ;
242+
243+ ops = cache -> c_ops ;
244+ if (!ops )
245+ return - NLE_INVAL ;
246+
247+ if (ops -> co_protocol != mngr -> cm_protocol )
248+ return - NLE_PROTO_MISMATCH ;
249+
250+ if (ops -> co_groups == NULL )
251+ return - NLE_OPNOTSUPP ;
252+
253+ for (i = 0 ; i < mngr -> cm_nassocs ; i ++ )
254+ if (mngr -> cm_assocs [i ].ca_cache == cache )
255+ break ;
256+
257+ if (i >= mngr -> cm_nassocs ) {
258+ return - NLE_RANGE ;
259+ }
260+
261+ mngr -> cm_assocs [i ].ca_change_v2 = cb ;
262+ mngr -> cm_assocs [i ].ca_change_data = data ;
263+
264+ return 0 ;
265+ }
266+
197267/**
198268 * Add cache to cache manager
199269 * @arg mngr Cache manager.
@@ -291,6 +361,41 @@ int nl_cache_mngr_add_cache(struct nl_cache_mngr *mngr, struct nl_cache *cache,
291361 return err ;
292362}
293363
364+ /**
365+ * Add cache to cache manager
366+ * @arg mngr Cache manager.
367+ * @arg cache Cache to be added to cache manager
368+ * @arg cb V2 function to be called upon changes.
369+ * @arg data Argument passed on to change callback
370+ *
371+ * Adds cache to the manager. The operation will trigger a full
372+ * dump request from the kernel to initially fill the contents
373+ * of the cache. The manager will subscribe to the notification group
374+ * of the cache and keep track of any further changes.
375+ *
376+ * The user is responsible for calling nl_cache_mngr_poll() or monitor
377+ * the socket and call nl_cache_mngr_data_ready() to allow the library
378+ * to process netlink notification events.
379+ *
380+ * @see nl_cache_mngr_poll()
381+ * @see nl_cache_mngr_data_ready()
382+ *
383+ * @return 0 on success or a negative error code.
384+ * @return -NLE_PROTO_MISMATCH Protocol mismatch between cache manager and
385+ * cache type
386+ * @return -NLE_OPNOTSUPP Cache type does not support updates
387+ * @return -NLE_EXIST Cache of this type already being managed
388+ */
389+ int nl_cache_mngr_add_cache_v2 (struct nl_cache_mngr * mngr , struct nl_cache * cache ,
390+ change_func_v2_t cb , void * data ) {
391+ int err ;
392+ err = nl_cache_mngr_add_cache (mngr , cache , NULL , NULL );
393+ if (err < 0 )
394+ return err ;
395+
396+ return nl_cache_mngr_set_change_func_v2 (mngr , cache , cb , data );
397+ }
398+
294399/**
295400 * Add cache to cache manager
296401 * @arg mngr Cache manager.
0 commit comments