bpf, xdp: Implement LINK_UPDATE for BPF XDP link
Add support for LINK_UPDATE command for BPF XDP link to enable reliable replacement of underlying BPF program. Signed-off-by: Andrii Nakryiko <andriin@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20200722064603.3350758-6-andriin@fb.com
This commit is contained in:
		
							parent
							
								
									aa8d3a716b
								
							
						
					
					
						commit
						026a4c28e1
					
				| @ -8996,9 +8996,52 @@ static void bpf_xdp_link_dealloc(struct bpf_link *link) | |||||||
| 	kfree(xdp_link); | 	kfree(xdp_link); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | static int bpf_xdp_link_update(struct bpf_link *link, struct bpf_prog *new_prog, | ||||||
|  | 			       struct bpf_prog *old_prog) | ||||||
|  | { | ||||||
|  | 	struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link); | ||||||
|  | 	enum bpf_xdp_mode mode; | ||||||
|  | 	bpf_op_t bpf_op; | ||||||
|  | 	int err = 0; | ||||||
|  | 
 | ||||||
|  | 	rtnl_lock(); | ||||||
|  | 
 | ||||||
|  | 	/* link might have been auto-released already, so fail */ | ||||||
|  | 	if (!xdp_link->dev) { | ||||||
|  | 		err = -ENOLINK; | ||||||
|  | 		goto out_unlock; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if (old_prog && link->prog != old_prog) { | ||||||
|  | 		err = -EPERM; | ||||||
|  | 		goto out_unlock; | ||||||
|  | 	} | ||||||
|  | 	old_prog = link->prog; | ||||||
|  | 	if (old_prog == new_prog) { | ||||||
|  | 		/* no-op, don't disturb drivers */ | ||||||
|  | 		bpf_prog_put(new_prog); | ||||||
|  | 		goto out_unlock; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	mode = dev_xdp_mode(xdp_link->flags); | ||||||
|  | 	bpf_op = dev_xdp_bpf_op(xdp_link->dev, mode); | ||||||
|  | 	err = dev_xdp_install(xdp_link->dev, mode, bpf_op, NULL, | ||||||
|  | 			      xdp_link->flags, new_prog); | ||||||
|  | 	if (err) | ||||||
|  | 		goto out_unlock; | ||||||
|  | 
 | ||||||
|  | 	old_prog = xchg(&link->prog, new_prog); | ||||||
|  | 	bpf_prog_put(old_prog); | ||||||
|  | 
 | ||||||
|  | out_unlock: | ||||||
|  | 	rtnl_unlock(); | ||||||
|  | 	return err; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| static const struct bpf_link_ops bpf_xdp_link_lops = { | static const struct bpf_link_ops bpf_xdp_link_lops = { | ||||||
| 	.release = bpf_xdp_link_release, | 	.release = bpf_xdp_link_release, | ||||||
| 	.dealloc = bpf_xdp_link_dealloc, | 	.dealloc = bpf_xdp_link_dealloc, | ||||||
|  | 	.update_prog = bpf_xdp_link_update, | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) | int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user