datapath.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047
  1. /*
  2. * Copyright (c) 2007-2012 Nicira, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301, USA
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/if_arp.h>
  22. #include <linux/if_vlan.h>
  23. #include <linux/in.h>
  24. #include <linux/ip.h>
  25. #include <linux/jhash.h>
  26. #include <linux/delay.h>
  27. #include <linux/time.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/genetlink.h>
  30. #include <linux/kernel.h>
  31. #include <linux/kthread.h>
  32. #include <linux/mutex.h>
  33. #include <linux/percpu.h>
  34. #include <linux/rcupdate.h>
  35. #include <linux/tcp.h>
  36. #include <linux/udp.h>
  37. #include <linux/ethtool.h>
  38. #include <linux/wait.h>
  39. #include <asm/div64.h>
  40. #include <linux/highmem.h>
  41. #include <linux/netfilter_bridge.h>
  42. #include <linux/netfilter_ipv4.h>
  43. #include <linux/inetdevice.h>
  44. #include <linux/list.h>
  45. #include <linux/openvswitch.h>
  46. #include <linux/rculist.h>
  47. #include <linux/dmi.h>
  48. #include <linux/workqueue.h>
  49. #include <net/genetlink.h>
  50. #include <net/net_namespace.h>
  51. #include <net/netns/generic.h>
  52. #include "datapath.h"
  53. #include "flow.h"
  54. #include "vport-internal_dev.h"
  55. /**
  56. * struct ovs_net - Per net-namespace data for ovs.
  57. * @dps: List of datapaths to enable dumping them all out.
  58. * Protected by genl_mutex.
  59. */
  60. struct ovs_net {
  61. struct list_head dps;
  62. };
  63. static int ovs_net_id __read_mostly;
  64. #define REHASH_FLOW_INTERVAL (10 * 60 * HZ)
  65. static void rehash_flow_table(struct work_struct *work);
  66. static DECLARE_DELAYED_WORK(rehash_flow_wq, rehash_flow_table);
  67. /**
  68. * DOC: Locking:
  69. *
  70. * Writes to device state (add/remove datapath, port, set operations on vports,
  71. * etc.) are protected by RTNL.
  72. *
  73. * Writes to other state (flow table modifications, set miscellaneous datapath
  74. * parameters, etc.) are protected by genl_mutex. The RTNL lock nests inside
  75. * genl_mutex.
  76. *
  77. * Reads are protected by RCU.
  78. *
  79. * There are a few special cases (mostly stats) that have their own
  80. * synchronization but they nest under all of above and don't interact with
  81. * each other.
  82. */
  83. static struct vport *new_vport(const struct vport_parms *);
  84. static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *,
  85. const struct dp_upcall_info *);
  86. static int queue_userspace_packet(struct net *, int dp_ifindex,
  87. struct sk_buff *,
  88. const struct dp_upcall_info *);
  89. /* Must be called with rcu_read_lock, genl_mutex, or RTNL lock. */
  90. static struct datapath *get_dp(struct net *net, int dp_ifindex)
  91. {
  92. struct datapath *dp = NULL;
  93. struct net_device *dev;
  94. rcu_read_lock();
  95. dev = dev_get_by_index_rcu(net, dp_ifindex);
  96. if (dev) {
  97. struct vport *vport = ovs_internal_dev_get_vport(dev);
  98. if (vport)
  99. dp = vport->dp;
  100. }
  101. rcu_read_unlock();
  102. return dp;
  103. }
  104. /* Must be called with rcu_read_lock or RTNL lock. */
  105. const char *ovs_dp_name(const struct datapath *dp)
  106. {
  107. struct vport *vport = ovs_vport_rtnl_rcu(dp, OVSP_LOCAL);
  108. return vport->ops->get_name(vport);
  109. }
  110. static int get_dpifindex(struct datapath *dp)
  111. {
  112. struct vport *local;
  113. int ifindex;
  114. rcu_read_lock();
  115. local = ovs_vport_rcu(dp, OVSP_LOCAL);
  116. if (local)
  117. ifindex = local->ops->get_ifindex(local);
  118. else
  119. ifindex = 0;
  120. rcu_read_unlock();
  121. return ifindex;
  122. }
  123. static void destroy_dp_rcu(struct rcu_head *rcu)
  124. {
  125. struct datapath *dp = container_of(rcu, struct datapath, rcu);
  126. ovs_flow_tbl_destroy((__force struct flow_table *)dp->table);
  127. free_percpu(dp->stats_percpu);
  128. release_net(ovs_dp_get_net(dp));
  129. kfree(dp->ports);
  130. kfree(dp);
  131. }
  132. static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
  133. u16 port_no)
  134. {
  135. return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
  136. }
  137. struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
  138. {
  139. struct vport *vport;
  140. struct hlist_node *n;
  141. struct hlist_head *head;
  142. head = vport_hash_bucket(dp, port_no);
  143. hlist_for_each_entry_rcu(vport, n, head, dp_hash_node) {
  144. if (vport->port_no == port_no)
  145. return vport;
  146. }
  147. return NULL;
  148. }
  149. /* Called with RTNL lock and genl_lock. */
  150. static struct vport *new_vport(const struct vport_parms *parms)
  151. {
  152. struct vport *vport;
  153. vport = ovs_vport_add(parms);
  154. if (!IS_ERR(vport)) {
  155. struct datapath *dp = parms->dp;
  156. struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
  157. hlist_add_head_rcu(&vport->dp_hash_node, head);
  158. }
  159. return vport;
  160. }
  161. /* Called with RTNL lock. */
  162. void ovs_dp_detach_port(struct vport *p)
  163. {
  164. ASSERT_RTNL();
  165. /* First drop references to device. */
  166. hlist_del_rcu(&p->dp_hash_node);
  167. /* Then destroy it. */
  168. ovs_vport_del(p);
  169. }
  170. /* Must be called with rcu_read_lock. */
  171. void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
  172. {
  173. struct datapath *dp = p->dp;
  174. struct sw_flow *flow;
  175. struct dp_stats_percpu *stats;
  176. struct sw_flow_key key;
  177. u64 *stats_counter;
  178. int error;
  179. int key_len;
  180. stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
  181. /* Extract flow from 'skb' into 'key'. */
  182. error = ovs_flow_extract(skb, p->port_no, &key, &key_len);
  183. if (unlikely(error)) {
  184. kfree_skb(skb);
  185. return;
  186. }
  187. /* Look up flow. */
  188. flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table), &key, key_len);
  189. if (unlikely(!flow)) {
  190. struct dp_upcall_info upcall;
  191. upcall.cmd = OVS_PACKET_CMD_MISS;
  192. upcall.key = &key;
  193. upcall.userdata = NULL;
  194. upcall.portid = p->upcall_portid;
  195. ovs_dp_upcall(dp, skb, &upcall);
  196. consume_skb(skb);
  197. stats_counter = &stats->n_missed;
  198. goto out;
  199. }
  200. OVS_CB(skb)->flow = flow;
  201. stats_counter = &stats->n_hit;
  202. ovs_flow_used(OVS_CB(skb)->flow, skb);
  203. ovs_execute_actions(dp, skb);
  204. out:
  205. /* Update datapath statistics. */
  206. u64_stats_update_begin(&stats->sync);
  207. (*stats_counter)++;
  208. u64_stats_update_end(&stats->sync);
  209. }
  210. static struct genl_family dp_packet_genl_family = {
  211. .id = GENL_ID_GENERATE,
  212. .hdrsize = sizeof(struct ovs_header),
  213. .name = OVS_PACKET_FAMILY,
  214. .version = OVS_PACKET_VERSION,
  215. .maxattr = OVS_PACKET_ATTR_MAX,
  216. .netnsok = true
  217. };
  218. int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
  219. const struct dp_upcall_info *upcall_info)
  220. {
  221. struct dp_stats_percpu *stats;
  222. int dp_ifindex;
  223. int err;
  224. if (upcall_info->portid == 0) {
  225. err = -ENOTCONN;
  226. goto err;
  227. }
  228. dp_ifindex = get_dpifindex(dp);
  229. if (!dp_ifindex) {
  230. err = -ENODEV;
  231. goto err;
  232. }
  233. if (!skb_is_gso(skb))
  234. err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
  235. else
  236. err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
  237. if (err)
  238. goto err;
  239. return 0;
  240. err:
  241. stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
  242. u64_stats_update_begin(&stats->sync);
  243. stats->n_lost++;
  244. u64_stats_update_end(&stats->sync);
  245. return err;
  246. }
  247. static int queue_gso_packets(struct net *net, int dp_ifindex,
  248. struct sk_buff *skb,
  249. const struct dp_upcall_info *upcall_info)
  250. {
  251. unsigned short gso_type = skb_shinfo(skb)->gso_type;
  252. struct dp_upcall_info later_info;
  253. struct sw_flow_key later_key;
  254. struct sk_buff *segs, *nskb;
  255. int err;
  256. segs = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
  257. if (IS_ERR(segs))
  258. return PTR_ERR(segs);
  259. /* Queue all of the segments. */
  260. skb = segs;
  261. do {
  262. err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
  263. if (err)
  264. break;
  265. if (skb == segs && gso_type & SKB_GSO_UDP) {
  266. /* The initial flow key extracted by ovs_flow_extract()
  267. * in this case is for a first fragment, so we need to
  268. * properly mark later fragments.
  269. */
  270. later_key = *upcall_info->key;
  271. later_key.ip.frag = OVS_FRAG_TYPE_LATER;
  272. later_info = *upcall_info;
  273. later_info.key = &later_key;
  274. upcall_info = &later_info;
  275. }
  276. } while ((skb = skb->next));
  277. /* Free all of the segments. */
  278. skb = segs;
  279. do {
  280. nskb = skb->next;
  281. if (err)
  282. kfree_skb(skb);
  283. else
  284. consume_skb(skb);
  285. } while ((skb = nskb));
  286. return err;
  287. }
  288. static int queue_userspace_packet(struct net *net, int dp_ifindex,
  289. struct sk_buff *skb,
  290. const struct dp_upcall_info *upcall_info)
  291. {
  292. struct ovs_header *upcall;
  293. struct sk_buff *nskb = NULL;
  294. struct sk_buff *user_skb; /* to be queued to userspace */
  295. struct nlattr *nla;
  296. unsigned int len;
  297. int err;
  298. if (vlan_tx_tag_present(skb)) {
  299. nskb = skb_clone(skb, GFP_ATOMIC);
  300. if (!nskb)
  301. return -ENOMEM;
  302. nskb = __vlan_put_tag(nskb, vlan_tx_tag_get(nskb));
  303. if (!nskb)
  304. return -ENOMEM;
  305. nskb->vlan_tci = 0;
  306. skb = nskb;
  307. }
  308. if (nla_attr_size(skb->len) > USHRT_MAX) {
  309. err = -EFBIG;
  310. goto out;
  311. }
  312. len = sizeof(struct ovs_header);
  313. len += nla_total_size(skb->len);
  314. len += nla_total_size(FLOW_BUFSIZE);
  315. if (upcall_info->cmd == OVS_PACKET_CMD_ACTION)
  316. len += nla_total_size(8);
  317. user_skb = genlmsg_new(len, GFP_ATOMIC);
  318. if (!user_skb) {
  319. err = -ENOMEM;
  320. goto out;
  321. }
  322. upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
  323. 0, upcall_info->cmd);
  324. upcall->dp_ifindex = dp_ifindex;
  325. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
  326. ovs_flow_to_nlattrs(upcall_info->key, user_skb);
  327. nla_nest_end(user_skb, nla);
  328. if (upcall_info->userdata)
  329. nla_put_u64(user_skb, OVS_PACKET_ATTR_USERDATA,
  330. nla_get_u64(upcall_info->userdata));
  331. nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
  332. skb_copy_and_csum_dev(skb, nla_data(nla));
  333. err = genlmsg_unicast(net, user_skb, upcall_info->portid);
  334. out:
  335. kfree_skb(nskb);
  336. return err;
  337. }
  338. /* Called with genl_mutex. */
  339. static int flush_flows(struct datapath *dp)
  340. {
  341. struct flow_table *old_table;
  342. struct flow_table *new_table;
  343. old_table = genl_dereference(dp->table);
  344. new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
  345. if (!new_table)
  346. return -ENOMEM;
  347. rcu_assign_pointer(dp->table, new_table);
  348. ovs_flow_tbl_deferred_destroy(old_table);
  349. return 0;
  350. }
  351. static int validate_actions(const struct nlattr *attr,
  352. const struct sw_flow_key *key, int depth);
  353. static int validate_sample(const struct nlattr *attr,
  354. const struct sw_flow_key *key, int depth)
  355. {
  356. const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
  357. const struct nlattr *probability, *actions;
  358. const struct nlattr *a;
  359. int rem;
  360. memset(attrs, 0, sizeof(attrs));
  361. nla_for_each_nested(a, attr, rem) {
  362. int type = nla_type(a);
  363. if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
  364. return -EINVAL;
  365. attrs[type] = a;
  366. }
  367. if (rem)
  368. return -EINVAL;
  369. probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
  370. if (!probability || nla_len(probability) != sizeof(u32))
  371. return -EINVAL;
  372. actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
  373. if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
  374. return -EINVAL;
  375. return validate_actions(actions, key, depth + 1);
  376. }
  377. static int validate_tp_port(const struct sw_flow_key *flow_key)
  378. {
  379. if (flow_key->eth.type == htons(ETH_P_IP)) {
  380. if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
  381. return 0;
  382. } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
  383. if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
  384. return 0;
  385. }
  386. return -EINVAL;
  387. }
  388. static int validate_set(const struct nlattr *a,
  389. const struct sw_flow_key *flow_key)
  390. {
  391. const struct nlattr *ovs_key = nla_data(a);
  392. int key_type = nla_type(ovs_key);
  393. /* There can be only one key in a action */
  394. if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
  395. return -EINVAL;
  396. if (key_type > OVS_KEY_ATTR_MAX ||
  397. nla_len(ovs_key) != ovs_key_lens[key_type])
  398. return -EINVAL;
  399. switch (key_type) {
  400. const struct ovs_key_ipv4 *ipv4_key;
  401. const struct ovs_key_ipv6 *ipv6_key;
  402. case OVS_KEY_ATTR_PRIORITY:
  403. case OVS_KEY_ATTR_ETHERNET:
  404. break;
  405. case OVS_KEY_ATTR_IPV4:
  406. if (flow_key->eth.type != htons(ETH_P_IP))
  407. return -EINVAL;
  408. if (!flow_key->ip.proto)
  409. return -EINVAL;
  410. ipv4_key = nla_data(ovs_key);
  411. if (ipv4_key->ipv4_proto != flow_key->ip.proto)
  412. return -EINVAL;
  413. if (ipv4_key->ipv4_frag != flow_key->ip.frag)
  414. return -EINVAL;
  415. break;
  416. case OVS_KEY_ATTR_IPV6:
  417. if (flow_key->eth.type != htons(ETH_P_IPV6))
  418. return -EINVAL;
  419. if (!flow_key->ip.proto)
  420. return -EINVAL;
  421. ipv6_key = nla_data(ovs_key);
  422. if (ipv6_key->ipv6_proto != flow_key->ip.proto)
  423. return -EINVAL;
  424. if (ipv6_key->ipv6_frag != flow_key->ip.frag)
  425. return -EINVAL;
  426. if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
  427. return -EINVAL;
  428. break;
  429. case OVS_KEY_ATTR_TCP:
  430. if (flow_key->ip.proto != IPPROTO_TCP)
  431. return -EINVAL;
  432. return validate_tp_port(flow_key);
  433. case OVS_KEY_ATTR_UDP:
  434. if (flow_key->ip.proto != IPPROTO_UDP)
  435. return -EINVAL;
  436. return validate_tp_port(flow_key);
  437. default:
  438. return -EINVAL;
  439. }
  440. return 0;
  441. }
  442. static int validate_userspace(const struct nlattr *attr)
  443. {
  444. static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
  445. [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
  446. [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_U64 },
  447. };
  448. struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
  449. int error;
  450. error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
  451. attr, userspace_policy);
  452. if (error)
  453. return error;
  454. if (!a[OVS_USERSPACE_ATTR_PID] ||
  455. !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
  456. return -EINVAL;
  457. return 0;
  458. }
  459. static int validate_actions(const struct nlattr *attr,
  460. const struct sw_flow_key *key, int depth)
  461. {
  462. const struct nlattr *a;
  463. int rem, err;
  464. if (depth >= SAMPLE_ACTION_DEPTH)
  465. return -EOVERFLOW;
  466. nla_for_each_nested(a, attr, rem) {
  467. /* Expected argument lengths, (u32)-1 for variable length. */
  468. static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
  469. [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
  470. [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
  471. [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
  472. [OVS_ACTION_ATTR_POP_VLAN] = 0,
  473. [OVS_ACTION_ATTR_SET] = (u32)-1,
  474. [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
  475. };
  476. const struct ovs_action_push_vlan *vlan;
  477. int type = nla_type(a);
  478. if (type > OVS_ACTION_ATTR_MAX ||
  479. (action_lens[type] != nla_len(a) &&
  480. action_lens[type] != (u32)-1))
  481. return -EINVAL;
  482. switch (type) {
  483. case OVS_ACTION_ATTR_UNSPEC:
  484. return -EINVAL;
  485. case OVS_ACTION_ATTR_USERSPACE:
  486. err = validate_userspace(a);
  487. if (err)
  488. return err;
  489. break;
  490. case OVS_ACTION_ATTR_OUTPUT:
  491. if (nla_get_u32(a) >= DP_MAX_PORTS)
  492. return -EINVAL;
  493. break;
  494. case OVS_ACTION_ATTR_POP_VLAN:
  495. break;
  496. case OVS_ACTION_ATTR_PUSH_VLAN:
  497. vlan = nla_data(a);
  498. if (vlan->vlan_tpid != htons(ETH_P_8021Q))
  499. return -EINVAL;
  500. if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
  501. return -EINVAL;
  502. break;
  503. case OVS_ACTION_ATTR_SET:
  504. err = validate_set(a, key);
  505. if (err)
  506. return err;
  507. break;
  508. case OVS_ACTION_ATTR_SAMPLE:
  509. err = validate_sample(a, key, depth);
  510. if (err)
  511. return err;
  512. break;
  513. default:
  514. return -EINVAL;
  515. }
  516. }
  517. if (rem > 0)
  518. return -EINVAL;
  519. return 0;
  520. }
  521. static void clear_stats(struct sw_flow *flow)
  522. {
  523. flow->used = 0;
  524. flow->tcp_flags = 0;
  525. flow->packet_count = 0;
  526. flow->byte_count = 0;
  527. }
  528. static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
  529. {
  530. struct ovs_header *ovs_header = info->userhdr;
  531. struct nlattr **a = info->attrs;
  532. struct sw_flow_actions *acts;
  533. struct sk_buff *packet;
  534. struct sw_flow *flow;
  535. struct datapath *dp;
  536. struct ethhdr *eth;
  537. int len;
  538. int err;
  539. int key_len;
  540. err = -EINVAL;
  541. if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
  542. !a[OVS_PACKET_ATTR_ACTIONS] ||
  543. nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN)
  544. goto err;
  545. len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
  546. packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
  547. err = -ENOMEM;
  548. if (!packet)
  549. goto err;
  550. skb_reserve(packet, NET_IP_ALIGN);
  551. memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len);
  552. skb_reset_mac_header(packet);
  553. eth = eth_hdr(packet);
  554. /* Normally, setting the skb 'protocol' field would be handled by a
  555. * call to eth_type_trans(), but it assumes there's a sending
  556. * device, which we may not have. */
  557. if (ntohs(eth->h_proto) >= 1536)
  558. packet->protocol = eth->h_proto;
  559. else
  560. packet->protocol = htons(ETH_P_802_2);
  561. /* Build an sw_flow for sending this packet. */
  562. flow = ovs_flow_alloc();
  563. err = PTR_ERR(flow);
  564. if (IS_ERR(flow))
  565. goto err_kfree_skb;
  566. err = ovs_flow_extract(packet, -1, &flow->key, &key_len);
  567. if (err)
  568. goto err_flow_free;
  569. err = ovs_flow_metadata_from_nlattrs(&flow->key.phy.priority,
  570. &flow->key.phy.in_port,
  571. a[OVS_PACKET_ATTR_KEY]);
  572. if (err)
  573. goto err_flow_free;
  574. err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
  575. if (err)
  576. goto err_flow_free;
  577. flow->hash = ovs_flow_hash(&flow->key, key_len);
  578. acts = ovs_flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
  579. err = PTR_ERR(acts);
  580. if (IS_ERR(acts))
  581. goto err_flow_free;
  582. rcu_assign_pointer(flow->sf_acts, acts);
  583. OVS_CB(packet)->flow = flow;
  584. packet->priority = flow->key.phy.priority;
  585. rcu_read_lock();
  586. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  587. err = -ENODEV;
  588. if (!dp)
  589. goto err_unlock;
  590. local_bh_disable();
  591. err = ovs_execute_actions(dp, packet);
  592. local_bh_enable();
  593. rcu_read_unlock();
  594. ovs_flow_free(flow);
  595. return err;
  596. err_unlock:
  597. rcu_read_unlock();
  598. err_flow_free:
  599. ovs_flow_free(flow);
  600. err_kfree_skb:
  601. kfree_skb(packet);
  602. err:
  603. return err;
  604. }
  605. static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
  606. [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
  607. [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
  608. [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
  609. };
  610. static struct genl_ops dp_packet_genl_ops[] = {
  611. { .cmd = OVS_PACKET_CMD_EXECUTE,
  612. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  613. .policy = packet_policy,
  614. .doit = ovs_packet_cmd_execute
  615. }
  616. };
  617. static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
  618. {
  619. int i;
  620. struct flow_table *table = genl_dereference(dp->table);
  621. stats->n_flows = ovs_flow_tbl_count(table);
  622. stats->n_hit = stats->n_missed = stats->n_lost = 0;
  623. for_each_possible_cpu(i) {
  624. const struct dp_stats_percpu *percpu_stats;
  625. struct dp_stats_percpu local_stats;
  626. unsigned int start;
  627. percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
  628. do {
  629. start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
  630. local_stats = *percpu_stats;
  631. } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
  632. stats->n_hit += local_stats.n_hit;
  633. stats->n_missed += local_stats.n_missed;
  634. stats->n_lost += local_stats.n_lost;
  635. }
  636. }
  637. static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
  638. [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
  639. [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
  640. [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
  641. };
  642. static struct genl_family dp_flow_genl_family = {
  643. .id = GENL_ID_GENERATE,
  644. .hdrsize = sizeof(struct ovs_header),
  645. .name = OVS_FLOW_FAMILY,
  646. .version = OVS_FLOW_VERSION,
  647. .maxattr = OVS_FLOW_ATTR_MAX,
  648. .netnsok = true
  649. };
  650. static struct genl_multicast_group ovs_dp_flow_multicast_group = {
  651. .name = OVS_FLOW_MCGROUP
  652. };
  653. /* Called with genl_lock. */
  654. static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
  655. struct sk_buff *skb, u32 portid,
  656. u32 seq, u32 flags, u8 cmd)
  657. {
  658. const int skb_orig_len = skb->len;
  659. const struct sw_flow_actions *sf_acts;
  660. struct ovs_flow_stats stats;
  661. struct ovs_header *ovs_header;
  662. struct nlattr *nla;
  663. unsigned long used;
  664. u8 tcp_flags;
  665. int err;
  666. sf_acts = rcu_dereference_protected(flow->sf_acts,
  667. lockdep_genl_is_held());
  668. ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
  669. if (!ovs_header)
  670. return -EMSGSIZE;
  671. ovs_header->dp_ifindex = get_dpifindex(dp);
  672. nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
  673. if (!nla)
  674. goto nla_put_failure;
  675. err = ovs_flow_to_nlattrs(&flow->key, skb);
  676. if (err)
  677. goto error;
  678. nla_nest_end(skb, nla);
  679. spin_lock_bh(&flow->lock);
  680. used = flow->used;
  681. stats.n_packets = flow->packet_count;
  682. stats.n_bytes = flow->byte_count;
  683. tcp_flags = flow->tcp_flags;
  684. spin_unlock_bh(&flow->lock);
  685. if (used &&
  686. nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
  687. goto nla_put_failure;
  688. if (stats.n_packets &&
  689. nla_put(skb, OVS_FLOW_ATTR_STATS,
  690. sizeof(struct ovs_flow_stats), &stats))
  691. goto nla_put_failure;
  692. if (tcp_flags &&
  693. nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
  694. goto nla_put_failure;
  695. /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
  696. * this is the first flow to be dumped into 'skb'. This is unusual for
  697. * Netlink but individual action lists can be longer than
  698. * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
  699. * The userspace caller can always fetch the actions separately if it
  700. * really wants them. (Most userspace callers in fact don't care.)
  701. *
  702. * This can only fail for dump operations because the skb is always
  703. * properly sized for single flows.
  704. */
  705. err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
  706. sf_acts->actions);
  707. if (err < 0 && skb_orig_len)
  708. goto error;
  709. return genlmsg_end(skb, ovs_header);
  710. nla_put_failure:
  711. err = -EMSGSIZE;
  712. error:
  713. genlmsg_cancel(skb, ovs_header);
  714. return err;
  715. }
  716. static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
  717. {
  718. const struct sw_flow_actions *sf_acts;
  719. int len;
  720. sf_acts = rcu_dereference_protected(flow->sf_acts,
  721. lockdep_genl_is_held());
  722. /* OVS_FLOW_ATTR_KEY */
  723. len = nla_total_size(FLOW_BUFSIZE);
  724. /* OVS_FLOW_ATTR_ACTIONS */
  725. len += nla_total_size(sf_acts->actions_len);
  726. /* OVS_FLOW_ATTR_STATS */
  727. len += nla_total_size(sizeof(struct ovs_flow_stats));
  728. /* OVS_FLOW_ATTR_TCP_FLAGS */
  729. len += nla_total_size(1);
  730. /* OVS_FLOW_ATTR_USED */
  731. len += nla_total_size(8);
  732. len += NLMSG_ALIGN(sizeof(struct ovs_header));
  733. return genlmsg_new(len, GFP_KERNEL);
  734. }
  735. static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
  736. struct datapath *dp,
  737. u32 portid, u32 seq, u8 cmd)
  738. {
  739. struct sk_buff *skb;
  740. int retval;
  741. skb = ovs_flow_cmd_alloc_info(flow);
  742. if (!skb)
  743. return ERR_PTR(-ENOMEM);
  744. retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
  745. BUG_ON(retval < 0);
  746. return skb;
  747. }
  748. static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
  749. {
  750. struct nlattr **a = info->attrs;
  751. struct ovs_header *ovs_header = info->userhdr;
  752. struct sw_flow_key key;
  753. struct sw_flow *flow;
  754. struct sk_buff *reply;
  755. struct datapath *dp;
  756. struct flow_table *table;
  757. int error;
  758. int key_len;
  759. /* Extract key. */
  760. error = -EINVAL;
  761. if (!a[OVS_FLOW_ATTR_KEY])
  762. goto error;
  763. error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  764. if (error)
  765. goto error;
  766. /* Validate actions. */
  767. if (a[OVS_FLOW_ATTR_ACTIONS]) {
  768. error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0);
  769. if (error)
  770. goto error;
  771. } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
  772. error = -EINVAL;
  773. goto error;
  774. }
  775. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  776. error = -ENODEV;
  777. if (!dp)
  778. goto error;
  779. table = genl_dereference(dp->table);
  780. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  781. if (!flow) {
  782. struct sw_flow_actions *acts;
  783. /* Bail out if we're not allowed to create a new flow. */
  784. error = -ENOENT;
  785. if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
  786. goto error;
  787. /* Expand table, if necessary, to make room. */
  788. if (ovs_flow_tbl_need_to_expand(table)) {
  789. struct flow_table *new_table;
  790. new_table = ovs_flow_tbl_expand(table);
  791. if (!IS_ERR(new_table)) {
  792. rcu_assign_pointer(dp->table, new_table);
  793. ovs_flow_tbl_deferred_destroy(table);
  794. table = genl_dereference(dp->table);
  795. }
  796. }
  797. /* Allocate flow. */
  798. flow = ovs_flow_alloc();
  799. if (IS_ERR(flow)) {
  800. error = PTR_ERR(flow);
  801. goto error;
  802. }
  803. flow->key = key;
  804. clear_stats(flow);
  805. /* Obtain actions. */
  806. acts = ovs_flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
  807. error = PTR_ERR(acts);
  808. if (IS_ERR(acts))
  809. goto error_free_flow;
  810. rcu_assign_pointer(flow->sf_acts, acts);
  811. /* Put flow in bucket. */
  812. flow->hash = ovs_flow_hash(&key, key_len);
  813. ovs_flow_tbl_insert(table, flow);
  814. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
  815. info->snd_seq,
  816. OVS_FLOW_CMD_NEW);
  817. } else {
  818. /* We found a matching flow. */
  819. struct sw_flow_actions *old_acts;
  820. struct nlattr *acts_attrs;
  821. /* Bail out if we're not allowed to modify an existing flow.
  822. * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
  823. * because Generic Netlink treats the latter as a dump
  824. * request. We also accept NLM_F_EXCL in case that bug ever
  825. * gets fixed.
  826. */
  827. error = -EEXIST;
  828. if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
  829. info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
  830. goto error;
  831. /* Update actions. */
  832. old_acts = rcu_dereference_protected(flow->sf_acts,
  833. lockdep_genl_is_held());
  834. acts_attrs = a[OVS_FLOW_ATTR_ACTIONS];
  835. if (acts_attrs &&
  836. (old_acts->actions_len != nla_len(acts_attrs) ||
  837. memcmp(old_acts->actions, nla_data(acts_attrs),
  838. old_acts->actions_len))) {
  839. struct sw_flow_actions *new_acts;
  840. new_acts = ovs_flow_actions_alloc(acts_attrs);
  841. error = PTR_ERR(new_acts);
  842. if (IS_ERR(new_acts))
  843. goto error;
  844. rcu_assign_pointer(flow->sf_acts, new_acts);
  845. ovs_flow_deferred_free_acts(old_acts);
  846. }
  847. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
  848. info->snd_seq, OVS_FLOW_CMD_NEW);
  849. /* Clear stats. */
  850. if (a[OVS_FLOW_ATTR_CLEAR]) {
  851. spin_lock_bh(&flow->lock);
  852. clear_stats(flow);
  853. spin_unlock_bh(&flow->lock);
  854. }
  855. }
  856. if (!IS_ERR(reply))
  857. genl_notify(reply, genl_info_net(info), info->snd_portid,
  858. ovs_dp_flow_multicast_group.id, info->nlhdr,
  859. GFP_KERNEL);
  860. else
  861. netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
  862. ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
  863. return 0;
  864. error_free_flow:
  865. ovs_flow_free(flow);
  866. error:
  867. return error;
  868. }
  869. static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
  870. {
  871. struct nlattr **a = info->attrs;
  872. struct ovs_header *ovs_header = info->userhdr;
  873. struct sw_flow_key key;
  874. struct sk_buff *reply;
  875. struct sw_flow *flow;
  876. struct datapath *dp;
  877. struct flow_table *table;
  878. int err;
  879. int key_len;
  880. if (!a[OVS_FLOW_ATTR_KEY])
  881. return -EINVAL;
  882. err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  883. if (err)
  884. return err;
  885. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  886. if (!dp)
  887. return -ENODEV;
  888. table = genl_dereference(dp->table);
  889. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  890. if (!flow)
  891. return -ENOENT;
  892. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
  893. info->snd_seq, OVS_FLOW_CMD_NEW);
  894. if (IS_ERR(reply))
  895. return PTR_ERR(reply);
  896. return genlmsg_reply(reply, info);
  897. }
  898. static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
  899. {
  900. struct nlattr **a = info->attrs;
  901. struct ovs_header *ovs_header = info->userhdr;
  902. struct sw_flow_key key;
  903. struct sk_buff *reply;
  904. struct sw_flow *flow;
  905. struct datapath *dp;
  906. struct flow_table *table;
  907. int err;
  908. int key_len;
  909. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  910. if (!dp)
  911. return -ENODEV;
  912. if (!a[OVS_FLOW_ATTR_KEY])
  913. return flush_flows(dp);
  914. err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  915. if (err)
  916. return err;
  917. table = genl_dereference(dp->table);
  918. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  919. if (!flow)
  920. return -ENOENT;
  921. reply = ovs_flow_cmd_alloc_info(flow);
  922. if (!reply)
  923. return -ENOMEM;
  924. ovs_flow_tbl_remove(table, flow);
  925. err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
  926. info->snd_seq, 0, OVS_FLOW_CMD_DEL);
  927. BUG_ON(err < 0);
  928. ovs_flow_deferred_free(flow);
  929. genl_notify(reply, genl_info_net(info), info->snd_portid,
  930. ovs_dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
  931. return 0;
  932. }
  933. static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  934. {
  935. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  936. struct datapath *dp;
  937. struct flow_table *table;
  938. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  939. if (!dp)
  940. return -ENODEV;
  941. table = genl_dereference(dp->table);
  942. for (;;) {
  943. struct sw_flow *flow;
  944. u32 bucket, obj;
  945. bucket = cb->args[0];
  946. obj = cb->args[1];
  947. flow = ovs_flow_tbl_next(table, &bucket, &obj);
  948. if (!flow)
  949. break;
  950. if (ovs_flow_cmd_fill_info(flow, dp, skb,
  951. NETLINK_CB(cb->skb).portid,
  952. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  953. OVS_FLOW_CMD_NEW) < 0)
  954. break;
  955. cb->args[0] = bucket;
  956. cb->args[1] = obj;
  957. }
  958. return skb->len;
  959. }
  960. static struct genl_ops dp_flow_genl_ops[] = {
  961. { .cmd = OVS_FLOW_CMD_NEW,
  962. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  963. .policy = flow_policy,
  964. .doit = ovs_flow_cmd_new_or_set
  965. },
  966. { .cmd = OVS_FLOW_CMD_DEL,
  967. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  968. .policy = flow_policy,
  969. .doit = ovs_flow_cmd_del
  970. },
  971. { .cmd = OVS_FLOW_CMD_GET,
  972. .flags = 0, /* OK for unprivileged users. */
  973. .policy = flow_policy,
  974. .doit = ovs_flow_cmd_get,
  975. .dumpit = ovs_flow_cmd_dump
  976. },
  977. { .cmd = OVS_FLOW_CMD_SET,
  978. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  979. .policy = flow_policy,
  980. .doit = ovs_flow_cmd_new_or_set,
  981. },
  982. };
  983. static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
  984. [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  985. [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  986. };
  987. static struct genl_family dp_datapath_genl_family = {
  988. .id = GENL_ID_GENERATE,
  989. .hdrsize = sizeof(struct ovs_header),
  990. .name = OVS_DATAPATH_FAMILY,
  991. .version = OVS_DATAPATH_VERSION,
  992. .maxattr = OVS_DP_ATTR_MAX,
  993. .netnsok = true
  994. };
  995. static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
  996. .name = OVS_DATAPATH_MCGROUP
  997. };
  998. static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
  999. u32 portid, u32 seq, u32 flags, u8 cmd)
  1000. {
  1001. struct ovs_header *ovs_header;
  1002. struct ovs_dp_stats dp_stats;
  1003. int err;
  1004. ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
  1005. flags, cmd);
  1006. if (!ovs_header)
  1007. goto error;
  1008. ovs_header->dp_ifindex = get_dpifindex(dp);
  1009. rcu_read_lock();
  1010. err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
  1011. rcu_read_unlock();
  1012. if (err)
  1013. goto nla_put_failure;
  1014. get_dp_stats(dp, &dp_stats);
  1015. if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
  1016. goto nla_put_failure;
  1017. return genlmsg_end(skb, ovs_header);
  1018. nla_put_failure:
  1019. genlmsg_cancel(skb, ovs_header);
  1020. error:
  1021. return -EMSGSIZE;
  1022. }
  1023. static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
  1024. u32 seq, u8 cmd)
  1025. {
  1026. struct sk_buff *skb;
  1027. int retval;
  1028. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1029. if (!skb)
  1030. return ERR_PTR(-ENOMEM);
  1031. retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
  1032. if (retval < 0) {
  1033. kfree_skb(skb);
  1034. return ERR_PTR(retval);
  1035. }
  1036. return skb;
  1037. }
  1038. /* Called with genl_mutex and optionally with RTNL lock also. */
  1039. static struct datapath *lookup_datapath(struct net *net,
  1040. struct ovs_header *ovs_header,
  1041. struct nlattr *a[OVS_DP_ATTR_MAX + 1])
  1042. {
  1043. struct datapath *dp;
  1044. if (!a[OVS_DP_ATTR_NAME])
  1045. dp = get_dp(net, ovs_header->dp_ifindex);
  1046. else {
  1047. struct vport *vport;
  1048. rcu_read_lock();
  1049. vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
  1050. dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
  1051. rcu_read_unlock();
  1052. }
  1053. return dp ? dp : ERR_PTR(-ENODEV);
  1054. }
  1055. static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1056. {
  1057. struct nlattr **a = info->attrs;
  1058. struct vport_parms parms;
  1059. struct sk_buff *reply;
  1060. struct datapath *dp;
  1061. struct vport *vport;
  1062. struct ovs_net *ovs_net;
  1063. int err, i;
  1064. err = -EINVAL;
  1065. if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
  1066. goto err;
  1067. rtnl_lock();
  1068. err = -ENOMEM;
  1069. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  1070. if (dp == NULL)
  1071. goto err_unlock_rtnl;
  1072. ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
  1073. /* Allocate table. */
  1074. err = -ENOMEM;
  1075. rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
  1076. if (!dp->table)
  1077. goto err_free_dp;
  1078. dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
  1079. if (!dp->stats_percpu) {
  1080. err = -ENOMEM;
  1081. goto err_destroy_table;
  1082. }
  1083. dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
  1084. GFP_KERNEL);
  1085. if (!dp->ports) {
  1086. err = -ENOMEM;
  1087. goto err_destroy_percpu;
  1088. }
  1089. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
  1090. INIT_HLIST_HEAD(&dp->ports[i]);
  1091. /* Set up our datapath device. */
  1092. parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
  1093. parms.type = OVS_VPORT_TYPE_INTERNAL;
  1094. parms.options = NULL;
  1095. parms.dp = dp;
  1096. parms.port_no = OVSP_LOCAL;
  1097. parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
  1098. vport = new_vport(&parms);
  1099. if (IS_ERR(vport)) {
  1100. err = PTR_ERR(vport);
  1101. if (err == -EBUSY)
  1102. err = -EEXIST;
  1103. goto err_destroy_ports_array;
  1104. }
  1105. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1106. info->snd_seq, OVS_DP_CMD_NEW);
  1107. err = PTR_ERR(reply);
  1108. if (IS_ERR(reply))
  1109. goto err_destroy_local_port;
  1110. ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
  1111. list_add_tail(&dp->list_node, &ovs_net->dps);
  1112. rtnl_unlock();
  1113. genl_notify(reply, genl_info_net(info), info->snd_portid,
  1114. ovs_dp_datapath_multicast_group.id, info->nlhdr,
  1115. GFP_KERNEL);
  1116. return 0;
  1117. err_destroy_local_port:
  1118. ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL));
  1119. err_destroy_ports_array:
  1120. kfree(dp->ports);
  1121. err_destroy_percpu:
  1122. free_percpu(dp->stats_percpu);
  1123. err_destroy_table:
  1124. ovs_flow_tbl_destroy(genl_dereference(dp->table));
  1125. err_free_dp:
  1126. release_net(ovs_dp_get_net(dp));
  1127. kfree(dp);
  1128. err_unlock_rtnl:
  1129. rtnl_unlock();
  1130. err:
  1131. return err;
  1132. }
  1133. /* Called with genl_mutex. */
  1134. static void __dp_destroy(struct datapath *dp)
  1135. {
  1136. int i;
  1137. rtnl_lock();
  1138. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1139. struct vport *vport;
  1140. struct hlist_node *node, *n;
  1141. hlist_for_each_entry_safe(vport, node, n, &dp->ports[i], dp_hash_node)
  1142. if (vport->port_no != OVSP_LOCAL)
  1143. ovs_dp_detach_port(vport);
  1144. }
  1145. list_del(&dp->list_node);
  1146. ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL));
  1147. /* rtnl_unlock() will wait until all the references to devices that
  1148. * are pending unregistration have been dropped. We do it here to
  1149. * ensure that any internal devices (which contain DP pointers) are
  1150. * fully destroyed before freeing the datapath.
  1151. */
  1152. rtnl_unlock();
  1153. call_rcu(&dp->rcu, destroy_dp_rcu);
  1154. }
  1155. static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1156. {
  1157. struct sk_buff *reply;
  1158. struct datapath *dp;
  1159. int err;
  1160. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1161. err = PTR_ERR(dp);
  1162. if (IS_ERR(dp))
  1163. return err;
  1164. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1165. info->snd_seq, OVS_DP_CMD_DEL);
  1166. err = PTR_ERR(reply);
  1167. if (IS_ERR(reply))
  1168. return err;
  1169. __dp_destroy(dp);
  1170. genl_notify(reply, genl_info_net(info), info->snd_portid,
  1171. ovs_dp_datapath_multicast_group.id, info->nlhdr,
  1172. GFP_KERNEL);
  1173. return 0;
  1174. }
  1175. static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1176. {
  1177. struct sk_buff *reply;
  1178. struct datapath *dp;
  1179. int err;
  1180. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1181. if (IS_ERR(dp))
  1182. return PTR_ERR(dp);
  1183. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1184. info->snd_seq, OVS_DP_CMD_NEW);
  1185. if (IS_ERR(reply)) {
  1186. err = PTR_ERR(reply);
  1187. netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
  1188. ovs_dp_datapath_multicast_group.id, err);
  1189. return 0;
  1190. }
  1191. genl_notify(reply, genl_info_net(info), info->snd_portid,
  1192. ovs_dp_datapath_multicast_group.id, info->nlhdr,
  1193. GFP_KERNEL);
  1194. return 0;
  1195. }
  1196. static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1197. {
  1198. struct sk_buff *reply;
  1199. struct datapath *dp;
  1200. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1201. if (IS_ERR(dp))
  1202. return PTR_ERR(dp);
  1203. reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
  1204. info->snd_seq, OVS_DP_CMD_NEW);
  1205. if (IS_ERR(reply))
  1206. return PTR_ERR(reply);
  1207. return genlmsg_reply(reply, info);
  1208. }
  1209. static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1210. {
  1211. struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
  1212. struct datapath *dp;
  1213. int skip = cb->args[0];
  1214. int i = 0;
  1215. list_for_each_entry(dp, &ovs_net->dps, list_node) {
  1216. if (i >= skip &&
  1217. ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
  1218. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1219. OVS_DP_CMD_NEW) < 0)
  1220. break;
  1221. i++;
  1222. }
  1223. cb->args[0] = i;
  1224. return skb->len;
  1225. }
  1226. static struct genl_ops dp_datapath_genl_ops[] = {
  1227. { .cmd = OVS_DP_CMD_NEW,
  1228. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1229. .policy = datapath_policy,
  1230. .doit = ovs_dp_cmd_new
  1231. },
  1232. { .cmd = OVS_DP_CMD_DEL,
  1233. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1234. .policy = datapath_policy,
  1235. .doit = ovs_dp_cmd_del
  1236. },
  1237. { .cmd = OVS_DP_CMD_GET,
  1238. .flags = 0, /* OK for unprivileged users. */
  1239. .policy = datapath_policy,
  1240. .doit = ovs_dp_cmd_get,
  1241. .dumpit = ovs_dp_cmd_dump
  1242. },
  1243. { .cmd = OVS_DP_CMD_SET,
  1244. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1245. .policy = datapath_policy,
  1246. .doit = ovs_dp_cmd_set,
  1247. },
  1248. };
  1249. static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
  1250. [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1251. [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
  1252. [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
  1253. [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
  1254. [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1255. [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
  1256. };
  1257. static struct genl_family dp_vport_genl_family = {
  1258. .id = GENL_ID_GENERATE,
  1259. .hdrsize = sizeof(struct ovs_header),
  1260. .name = OVS_VPORT_FAMILY,
  1261. .version = OVS_VPORT_VERSION,
  1262. .maxattr = OVS_VPORT_ATTR_MAX,
  1263. .netnsok = true
  1264. };
  1265. struct genl_multicast_group ovs_dp_vport_multicast_group = {
  1266. .name = OVS_VPORT_MCGROUP
  1267. };
  1268. /* Called with RTNL lock or RCU read lock. */
  1269. static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
  1270. u32 portid, u32 seq, u32 flags, u8 cmd)
  1271. {
  1272. struct ovs_header *ovs_header;
  1273. struct ovs_vport_stats vport_stats;
  1274. int err;
  1275. ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
  1276. flags, cmd);
  1277. if (!ovs_header)
  1278. return -EMSGSIZE;
  1279. ovs_header->dp_ifindex = get_dpifindex(vport->dp);
  1280. if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
  1281. nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
  1282. nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
  1283. nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
  1284. goto nla_put_failure;
  1285. ovs_vport_get_stats(vport, &vport_stats);
  1286. if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
  1287. &vport_stats))
  1288. goto nla_put_failure;
  1289. err = ovs_vport_get_options(vport, skb);
  1290. if (err == -EMSGSIZE)
  1291. goto error;
  1292. return genlmsg_end(skb, ovs_header);
  1293. nla_put_failure:
  1294. err = -EMSGSIZE;
  1295. error:
  1296. genlmsg_cancel(skb, ovs_header);
  1297. return err;
  1298. }
  1299. /* Called with RTNL lock or RCU read lock. */
  1300. struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
  1301. u32 seq, u8 cmd)
  1302. {
  1303. struct sk_buff *skb;
  1304. int retval;
  1305. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1306. if (!skb)
  1307. return ERR_PTR(-ENOMEM);
  1308. retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
  1309. if (retval < 0) {
  1310. kfree_skb(skb);
  1311. return ERR_PTR(retval);
  1312. }
  1313. return skb;
  1314. }
  1315. /* Called with RTNL lock or RCU read lock. */
  1316. static struct vport *lookup_vport(struct net *net,
  1317. struct ovs_header *ovs_header,
  1318. struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
  1319. {
  1320. struct datapath *dp;
  1321. struct vport *vport;
  1322. if (a[OVS_VPORT_ATTR_NAME]) {
  1323. vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
  1324. if (!vport)
  1325. return ERR_PTR(-ENODEV);
  1326. if (ovs_header->dp_ifindex &&
  1327. ovs_header->dp_ifindex != get_dpifindex(vport->dp))
  1328. return ERR_PTR(-ENODEV);
  1329. return vport;
  1330. } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1331. u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1332. if (port_no >= DP_MAX_PORTS)
  1333. return ERR_PTR(-EFBIG);
  1334. dp = get_dp(net, ovs_header->dp_ifindex);
  1335. if (!dp)
  1336. return ERR_PTR(-ENODEV);
  1337. vport = ovs_vport_rtnl_rcu(dp, port_no);
  1338. if (!vport)
  1339. return ERR_PTR(-ENOENT);
  1340. return vport;
  1341. } else
  1342. return ERR_PTR(-EINVAL);
  1343. }
  1344. static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1345. {
  1346. struct nlattr **a = info->attrs;
  1347. struct ovs_header *ovs_header = info->userhdr;
  1348. struct vport_parms parms;
  1349. struct sk_buff *reply;
  1350. struct vport *vport;
  1351. struct datapath *dp;
  1352. u32 port_no;
  1353. int err;
  1354. err = -EINVAL;
  1355. if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
  1356. !a[OVS_VPORT_ATTR_UPCALL_PID])
  1357. goto exit;
  1358. rtnl_lock();
  1359. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1360. err = -ENODEV;
  1361. if (!dp)
  1362. goto exit_unlock;
  1363. if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1364. port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1365. err = -EFBIG;
  1366. if (port_no >= DP_MAX_PORTS)
  1367. goto exit_unlock;
  1368. vport = ovs_vport_rtnl_rcu(dp, port_no);
  1369. err = -EBUSY;
  1370. if (vport)
  1371. goto exit_unlock;
  1372. } else {
  1373. for (port_no = 1; ; port_no++) {
  1374. if (port_no >= DP_MAX_PORTS) {
  1375. err = -EFBIG;
  1376. goto exit_unlock;
  1377. }
  1378. vport = ovs_vport_rtnl(dp, port_no);
  1379. if (!vport)
  1380. break;
  1381. }
  1382. }
  1383. parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
  1384. parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
  1385. parms.options = a[OVS_VPORT_ATTR_OPTIONS];
  1386. parms.dp = dp;
  1387. parms.port_no = port_no;
  1388. parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1389. vport = new_vport(&parms);
  1390. err = PTR_ERR(vport);
  1391. if (IS_ERR(vport))
  1392. goto exit_unlock;
  1393. reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
  1394. OVS_VPORT_CMD_NEW);
  1395. if (IS_ERR(reply)) {
  1396. err = PTR_ERR(reply);
  1397. ovs_dp_detach_port(vport);
  1398. goto exit_unlock;
  1399. }
  1400. genl_notify(reply, genl_info_net(info), info->snd_portid,
  1401. ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
  1402. exit_unlock:
  1403. rtnl_unlock();
  1404. exit:
  1405. return err;
  1406. }
  1407. static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1408. {
  1409. struct nlattr **a = info->attrs;
  1410. struct sk_buff *reply;
  1411. struct vport *vport;
  1412. int err;
  1413. rtnl_lock();
  1414. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1415. err = PTR_ERR(vport);
  1416. if (IS_ERR(vport))
  1417. goto exit_unlock;
  1418. err = 0;
  1419. if (a[OVS_VPORT_ATTR_TYPE] &&
  1420. nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
  1421. err = -EINVAL;
  1422. if (!err && a[OVS_VPORT_ATTR_OPTIONS])
  1423. err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
  1424. if (err)
  1425. goto exit_unlock;
  1426. if (a[OVS_VPORT_ATTR_UPCALL_PID])
  1427. vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1428. reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
  1429. OVS_VPORT_CMD_NEW);
  1430. if (IS_ERR(reply)) {
  1431. netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
  1432. ovs_dp_vport_multicast_group.id, PTR_ERR(reply));
  1433. goto exit_unlock;
  1434. }
  1435. genl_notify(reply, genl_info_net(info), info->snd_portid,
  1436. ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
  1437. exit_unlock:
  1438. rtnl_unlock();
  1439. return err;
  1440. }
  1441. static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1442. {
  1443. struct nlattr **a = info->attrs;
  1444. struct sk_buff *reply;
  1445. struct vport *vport;
  1446. int err;
  1447. rtnl_lock();
  1448. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1449. err = PTR_ERR(vport);
  1450. if (IS_ERR(vport))
  1451. goto exit_unlock;
  1452. if (vport->port_no == OVSP_LOCAL) {
  1453. err = -EINVAL;
  1454. goto exit_unlock;
  1455. }
  1456. reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
  1457. OVS_VPORT_CMD_DEL);
  1458. err = PTR_ERR(reply);
  1459. if (IS_ERR(reply))
  1460. goto exit_unlock;
  1461. ovs_dp_detach_port(vport);
  1462. genl_notify(reply, genl_info_net(info), info->snd_portid,
  1463. ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
  1464. exit_unlock:
  1465. rtnl_unlock();
  1466. return err;
  1467. }
  1468. static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1469. {
  1470. struct nlattr **a = info->attrs;
  1471. struct ovs_header *ovs_header = info->userhdr;
  1472. struct sk_buff *reply;
  1473. struct vport *vport;
  1474. int err;
  1475. rcu_read_lock();
  1476. vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
  1477. err = PTR_ERR(vport);
  1478. if (IS_ERR(vport))
  1479. goto exit_unlock;
  1480. reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
  1481. OVS_VPORT_CMD_NEW);
  1482. err = PTR_ERR(reply);
  1483. if (IS_ERR(reply))
  1484. goto exit_unlock;
  1485. rcu_read_unlock();
  1486. return genlmsg_reply(reply, info);
  1487. exit_unlock:
  1488. rcu_read_unlock();
  1489. return err;
  1490. }
  1491. static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1492. {
  1493. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1494. struct datapath *dp;
  1495. int bucket = cb->args[0], skip = cb->args[1];
  1496. int i, j = 0;
  1497. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1498. if (!dp)
  1499. return -ENODEV;
  1500. rcu_read_lock();
  1501. for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
  1502. struct vport *vport;
  1503. struct hlist_node *n;
  1504. j = 0;
  1505. hlist_for_each_entry_rcu(vport, n, &dp->ports[i], dp_hash_node) {
  1506. if (j >= skip &&
  1507. ovs_vport_cmd_fill_info(vport, skb,
  1508. NETLINK_CB(cb->skb).portid,
  1509. cb->nlh->nlmsg_seq,
  1510. NLM_F_MULTI,
  1511. OVS_VPORT_CMD_NEW) < 0)
  1512. goto out;
  1513. j++;
  1514. }
  1515. skip = 0;
  1516. }
  1517. out:
  1518. rcu_read_unlock();
  1519. cb->args[0] = i;
  1520. cb->args[1] = j;
  1521. return skb->len;
  1522. }
  1523. static struct genl_ops dp_vport_genl_ops[] = {
  1524. { .cmd = OVS_VPORT_CMD_NEW,
  1525. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1526. .policy = vport_policy,
  1527. .doit = ovs_vport_cmd_new
  1528. },
  1529. { .cmd = OVS_VPORT_CMD_DEL,
  1530. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1531. .policy = vport_policy,
  1532. .doit = ovs_vport_cmd_del
  1533. },
  1534. { .cmd = OVS_VPORT_CMD_GET,
  1535. .flags = 0, /* OK for unprivileged users. */
  1536. .policy = vport_policy,
  1537. .doit = ovs_vport_cmd_get,
  1538. .dumpit = ovs_vport_cmd_dump
  1539. },
  1540. { .cmd = OVS_VPORT_CMD_SET,
  1541. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1542. .policy = vport_policy,
  1543. .doit = ovs_vport_cmd_set,
  1544. },
  1545. };
  1546. struct genl_family_and_ops {
  1547. struct genl_family *family;
  1548. struct genl_ops *ops;
  1549. int n_ops;
  1550. struct genl_multicast_group *group;
  1551. };
  1552. static const struct genl_family_and_ops dp_genl_families[] = {
  1553. { &dp_datapath_genl_family,
  1554. dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
  1555. &ovs_dp_datapath_multicast_group },
  1556. { &dp_vport_genl_family,
  1557. dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
  1558. &ovs_dp_vport_multicast_group },
  1559. { &dp_flow_genl_family,
  1560. dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
  1561. &ovs_dp_flow_multicast_group },
  1562. { &dp_packet_genl_family,
  1563. dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
  1564. NULL },
  1565. };
  1566. static void dp_unregister_genl(int n_families)
  1567. {
  1568. int i;
  1569. for (i = 0; i < n_families; i++)
  1570. genl_unregister_family(dp_genl_families[i].family);
  1571. }
  1572. static int dp_register_genl(void)
  1573. {
  1574. int n_registered;
  1575. int err;
  1576. int i;
  1577. n_registered = 0;
  1578. for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
  1579. const struct genl_family_and_ops *f = &dp_genl_families[i];
  1580. err = genl_register_family_with_ops(f->family, f->ops,
  1581. f->n_ops);
  1582. if (err)
  1583. goto error;
  1584. n_registered++;
  1585. if (f->group) {
  1586. err = genl_register_mc_group(f->family, f->group);
  1587. if (err)
  1588. goto error;
  1589. }
  1590. }
  1591. return 0;
  1592. error:
  1593. dp_unregister_genl(n_registered);
  1594. return err;
  1595. }
  1596. static void rehash_flow_table(struct work_struct *work)
  1597. {
  1598. struct datapath *dp;
  1599. struct net *net;
  1600. genl_lock();
  1601. rtnl_lock();
  1602. for_each_net(net) {
  1603. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1604. list_for_each_entry(dp, &ovs_net->dps, list_node) {
  1605. struct flow_table *old_table = genl_dereference(dp->table);
  1606. struct flow_table *new_table;
  1607. new_table = ovs_flow_tbl_rehash(old_table);
  1608. if (!IS_ERR(new_table)) {
  1609. rcu_assign_pointer(dp->table, new_table);
  1610. ovs_flow_tbl_deferred_destroy(old_table);
  1611. }
  1612. }
  1613. }
  1614. rtnl_unlock();
  1615. genl_unlock();
  1616. schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
  1617. }
  1618. static int __net_init ovs_init_net(struct net *net)
  1619. {
  1620. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1621. INIT_LIST_HEAD(&ovs_net->dps);
  1622. return 0;
  1623. }
  1624. static void __net_exit ovs_exit_net(struct net *net)
  1625. {
  1626. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1627. struct datapath *dp, *dp_next;
  1628. genl_lock();
  1629. list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
  1630. __dp_destroy(dp);
  1631. genl_unlock();
  1632. }
  1633. static struct pernet_operations ovs_net_ops = {
  1634. .init = ovs_init_net,
  1635. .exit = ovs_exit_net,
  1636. .id = &ovs_net_id,
  1637. .size = sizeof(struct ovs_net),
  1638. };
  1639. static int __init dp_init(void)
  1640. {
  1641. struct sk_buff *dummy_skb;
  1642. int err;
  1643. BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
  1644. pr_info("Open vSwitch switching datapath\n");
  1645. err = ovs_flow_init();
  1646. if (err)
  1647. goto error;
  1648. err = ovs_vport_init();
  1649. if (err)
  1650. goto error_flow_exit;
  1651. err = register_pernet_device(&ovs_net_ops);
  1652. if (err)
  1653. goto error_vport_exit;
  1654. err = register_netdevice_notifier(&ovs_dp_device_notifier);
  1655. if (err)
  1656. goto error_netns_exit;
  1657. err = dp_register_genl();
  1658. if (err < 0)
  1659. goto error_unreg_notifier;
  1660. schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
  1661. return 0;
  1662. error_unreg_notifier:
  1663. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1664. error_netns_exit:
  1665. unregister_pernet_device(&ovs_net_ops);
  1666. error_vport_exit:
  1667. ovs_vport_exit();
  1668. error_flow_exit:
  1669. ovs_flow_exit();
  1670. error:
  1671. return err;
  1672. }
  1673. static void dp_cleanup(void)
  1674. {
  1675. cancel_delayed_work_sync(&rehash_flow_wq);
  1676. dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
  1677. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1678. unregister_pernet_device(&ovs_net_ops);
  1679. rcu_barrier();
  1680. ovs_vport_exit();
  1681. ovs_flow_exit();
  1682. }
  1683. module_init(dp_init);
  1684. module_exit(dp_cleanup);
  1685. MODULE_DESCRIPTION("Open vSwitch switching datapath");
  1686. MODULE_LICENSE("GPL");