datapath.c 50 KB

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